diff --git a/.gitignore b/.gitignore index accb586..51fc188 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ vendor composer.lock tests/cov .*.swp +.php_cs.cache # Composer binaries bin/phpunit diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..8d61ee2 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,12 @@ +getFinder() + ->exclude('vendor') + ->in(__DIR__); +$config->setRules([ + '@PSR1' => true, + '@Symfony' =>true +]); + +return $config; \ No newline at end of file diff --git a/composer.json b/composer.json index 2603aad..c782dbc 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ } }, "require-dev": { - "phpunit/phpunit" : "*" + "phpunit/phpunit" : "^6" }, "config" : { "bin-dir" : "bin/" diff --git a/lib/ContextStackTrait.php b/lib/ContextStackTrait.php index 23ce93a..bc770ff 100644 --- a/lib/ContextStackTrait.php +++ b/lib/ContextStackTrait.php @@ -1,9 +1,11 @@ -contextStack[] = [ $this->elementMap, $this->contextUri, $this->namespaceMap, - $this->classMap + $this->classMap, ]; - } /** * Restore the previous "context". - * - * @return void */ - function popContext() { - + public function popContext() + { list( $this->elementMap, $this->contextUri, $this->namespaceMap, $this->classMap ) = array_pop($this->contextStack); - } - } diff --git a/lib/Deserializer/functions.php b/lib/Deserializer/functions.php index eec8d68..0eff4b7 100644 --- a/lib/Deserializer/functions.php +++ b/lib/Deserializer/functions.php @@ -1,4 +1,6 @@ -isEmptyElement) { $reader->next(); + return []; } @@ -76,9 +79,8 @@ function keyValue(Reader $reader, string $namespace = null) : array { $values = []; do { - - if ($reader->nodeType === Reader::ELEMENT) { - if ($namespace !== null && $reader->namespaceURI === $namespace) { + if (Reader::ELEMENT === $reader->nodeType) { + if (null !== $namespace && $reader->namespaceURI === $namespace) { $values[$reader->localName] = $reader->parseCurrentElement()['value']; } else { $clark = $reader->getClark(); @@ -89,12 +91,11 @@ function keyValue(Reader $reader, string $namespace = null) : array { break; } } - } while ($reader->nodeType !== Reader::END_ELEMENT); + } while (Reader::END_ELEMENT !== $reader->nodeType); $reader->read(); return $values; - } /** @@ -143,11 +144,12 @@ function keyValue(Reader $reader, string $namespace = null) : array { * * @return string[] */ -function enum(Reader $reader, string $namespace = null) : array { - +function enum(Reader $reader, string $namespace = null): array +{ // If there's no children, we don't do anything. if ($reader->isEmptyElement) { $reader->next(); + return []; } if (!$reader->read()) { @@ -165,8 +167,7 @@ function enum(Reader $reader, string $namespace = null) : array { $values = []; do { - - if ($reader->nodeType !== Reader::ELEMENT) { + if (Reader::ELEMENT !== $reader->nodeType) { continue; } if (!is_null($namespace) && $namespace === $reader->namespaceURI) { @@ -174,12 +175,11 @@ function enum(Reader $reader, string $namespace = null) : array { } else { $values[] = $reader->getClark(); } - } while ($reader->depth >= $currentDepth && $reader->next()); $reader->next(); - return $values; + return $values; } /** @@ -191,11 +191,12 @@ function enum(Reader $reader, string $namespace = null) : array { * * @return object */ -function valueObject(Reader $reader, string $className, string $namespace) { - +function valueObject(Reader $reader, string $className, string $namespace) +{ $valueObject = new $className(); if ($reader->isEmptyElement) { $reader->next(); + return $valueObject; } @@ -203,9 +204,7 @@ function valueObject(Reader $reader, string $className, string $namespace) { $reader->read(); do { - - if ($reader->nodeType === Reader::ELEMENT && $reader->namespaceURI == $namespace) { - + if (Reader::ELEMENT === $reader->nodeType && $reader->namespaceURI == $namespace) { if (property_exists($valueObject, $reader->localName)) { if (is_array($defaultProperties[$reader->localName])) { $valueObject->{$reader->localName}[] = $reader->parseCurrentElement()['value']; @@ -221,16 +220,16 @@ function valueObject(Reader $reader, string $className, string $namespace) { break; } } - } while ($reader->nodeType !== Reader::END_ELEMENT); + } while (Reader::END_ELEMENT !== $reader->nodeType); $reader->read(); - return $valueObject; + return $valueObject; } /** * This deserializer helps you deserialize xml structures that look like - * this: + * this:. * * * ... @@ -252,28 +251,25 @@ function valueObject(Reader $reader, string $className, string $namespace) { * $childElementName must either be a a clark-notation element name, or if no * namespace is used, the bare element name. */ -function repeatingElements(Reader $reader, string $childElementName) : array { - - if ($childElementName[0] !== '{') { - $childElementName = '{}' . $childElementName; +function repeatingElements(Reader $reader, string $childElementName): array +{ + if ('{' !== $childElementName[0]) { + $childElementName = '{}'.$childElementName; } $result = []; foreach ($reader->parseGetElements() as $element) { - if ($element['name'] === $childElementName) { $result[] = $element['value']; } - } return $result; - } /** - * This deserializer helps you to deserialize structures which contain mixed content like this: - * + * This deserializer helps you to deserialize structures which contain mixed content like this:. + * *

some text and a inline tagand even more text

* * The above example will return @@ -290,11 +286,12 @@ function repeatingElements(Reader $reader, string $childElementName) : array { * * In strict XML documents you wont find this kind of markup but in html this is a quite common pattern. */ -function mixedContent(Reader $reader) : array { - +function mixedContent(Reader $reader): array +{ // If there's no children, we don't do anything. if ($reader->isEmptyElement) { $reader->next(); + return []; } @@ -303,12 +300,12 @@ function mixedContent(Reader $reader) : array { $content = []; $reader->read(); while (true) { - if ($reader->nodeType == Reader::ELEMENT) { + if (Reader::ELEMENT == $reader->nodeType) { $content[] = $reader->parseCurrentElement(); } elseif ($reader->depth >= $previousDepth && in_array($reader->nodeType, [Reader::TEXT, Reader::CDATA, Reader::WHITESPACE])) { $content[] = $reader->value; $reader->read(); - } elseif ($reader->nodeType == Reader::END_ELEMENT) { + } elseif (Reader::END_ELEMENT == $reader->nodeType) { // Ensuring we are moving the cursor after the end element. $reader->read(); break; @@ -318,5 +315,4 @@ function mixedContent(Reader $reader) : array { } return $content; - } diff --git a/lib/Element.php b/lib/Element.php index 42c3a6c..559eb54 100644 --- a/lib/Element.php +++ b/lib/Element.php @@ -1,4 +1,6 @@ -value = $value; - } /** @@ -48,13 +49,10 @@ function __construct($value = null) { * This allows serializers to be re-used for different element names. * * If you are opening new elements, you must also close them again. - * - * @return void */ - function xmlSerialize(Xml\Writer $writer) { - + public function xmlSerialize(Xml\Writer $writer) + { $writer->write($this->value); - } /** @@ -77,11 +75,10 @@ function xmlSerialize(Xml\Writer $writer) { * * @return mixed */ - static function xmlDeserialize(Xml\Reader $reader) { - + public static function xmlDeserialize(Xml\Reader $reader) + { $subTree = $reader->parseInnerTree(); - return $subTree; + return $subTree; } - } diff --git a/lib/Element/Cdata.php b/lib/Element/Cdata.php index 1193e76..61d3213 100644 --- a/lib/Element/Cdata.php +++ b/lib/Element/Cdata.php @@ -1,4 +1,6 @@ -value = $value; } @@ -48,13 +51,9 @@ function __construct(string $value) { * This allows serializers to be re-used for different element names. * * If you are opening new elements, you must also close them again. - * - * @return void */ - function xmlSerialize(Xml\Writer $writer) { - + public function xmlSerialize(Xml\Writer $writer) + { $writer->writeCData($this->value); - } - } diff --git a/lib/Element/Elements.php b/lib/Element/Elements.php index 17e69e6..e511798 100644 --- a/lib/Element/Elements.php +++ b/lib/Element/Elements.php @@ -1,4 +1,6 @@ - * @@ -33,22 +35,21 @@ * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -class Elements implements Xml\Element { - +class Elements implements Xml\Element +{ /** - * Value to serialize + * Value to serialize. * * @var array */ protected $value; /** - * Constructor + * Constructor. */ - function __construct(array $value = []) { - + public function __construct(array $value = []) + { $this->value = $value; - } /** @@ -66,13 +67,10 @@ function __construct(array $value = []) { * This allows serializers to be re-used for different element names. * * If you are opening new elements, you must also close them again. - * - * @return void */ - function xmlSerialize(Xml\Writer $writer) { - + public function xmlSerialize(Xml\Writer $writer) + { Serializer\enum($writer, $this->value); - } /** @@ -95,10 +93,8 @@ function xmlSerialize(Xml\Writer $writer) { * * @return mixed */ - static function xmlDeserialize(Xml\Reader $reader) { - + public static function xmlDeserialize(Xml\Reader $reader) + { return Deserializer\enum($reader); - } - } diff --git a/lib/Element/KeyValue.php b/lib/Element/KeyValue.php index f4088fb..dacee00 100644 --- a/lib/Element/KeyValue.php +++ b/lib/Element/KeyValue.php @@ -1,4 +1,6 @@ -value = $value; - } /** @@ -66,13 +67,10 @@ function __construct(array $value = []) { * This allows serializers to be re-used for different element names. * * If you are opening new elements, you must also close them again. - * - * @return void */ - function xmlSerialize(Xml\Writer $writer) { - + public function xmlSerialize(Xml\Writer $writer) + { $writer->write($this->value); - } /** @@ -95,10 +93,8 @@ function xmlSerialize(Xml\Writer $writer) { * * @return mixed */ - static function xmlDeserialize(Xml\Reader $reader) { - + public static function xmlDeserialize(Xml\Reader $reader) + { return Deserializer\keyValue($reader); - } - } diff --git a/lib/Element/Uri.php b/lib/Element/Uri.php index 7ccdd33..898a264 100644 --- a/lib/Element/Uri.php +++ b/lib/Element/Uri.php @@ -1,4 +1,6 @@ -value = $value; } @@ -53,18 +56,15 @@ function __construct($value) { * This allows serializers to be re-used for different element names. * * If you are opening new elements, you must also close them again. - * - * @return void */ - function xmlSerialize(Xml\Writer $writer) { - + public function xmlSerialize(Xml\Writer $writer) + { $writer->text( \Sabre\Uri\resolve( $writer->contextUri, $this->value ) ); - } /** @@ -87,15 +87,13 @@ function xmlSerialize(Xml\Writer $writer) { * * @return mixed */ - static function xmlDeserialize(Xml\Reader $reader) { - + public static function xmlDeserialize(Xml\Reader $reader) + { return new self( \Sabre\Uri\resolve( $reader->contextUri, $reader->readText() ) ); - } - } diff --git a/lib/Element/XmlFragment.php b/lib/Element/XmlFragment.php index fd2bda9..413e0f1 100644 --- a/lib/Element/XmlFragment.php +++ b/lib/Element/XmlFragment.php @@ -1,4 +1,6 @@ -xml = $xml; - } /** * Returns the inner XML document. */ - function getXml() : string { - + public function getXml(): string + { return $this->xml; - } /** @@ -62,11 +62,9 @@ function getXml() : string { * This allows serializers to be re-used for different element names. * * If you are opening new elements, you must also close them again. - * - * @return void */ - function xmlSerialize(Writer $writer) { - + public function xmlSerialize(Writer $writer) + { $reader = new Reader(); // Wrapping the xml in a container, so root-less values can still be @@ -79,28 +77,26 @@ function xmlSerialize(Writer $writer) { $reader->xml($xml); while ($reader->read()) { - if ($reader->depth < 1) { // Skipping the root node. continue; } switch ($reader->nodeType) { - - case Reader::ELEMENT : + case Reader::ELEMENT: $writer->startElement( $reader->getClark() ); $empty = $reader->isEmptyElement; while ($reader->moveToNextAttribute()) { switch ($reader->namespaceURI) { - case '' : + case '': $writer->writeAttribute($reader->localName, $reader->value); break; - case 'http://www.w3.org/2000/xmlns/' : + case 'http://www.w3.org/2000/xmlns/': // Skip namespace declarations break; - default : + default: $writer->writeAttribute($reader->getClark(), $reader->value); break; } @@ -109,20 +105,17 @@ function xmlSerialize(Writer $writer) { $writer->endElement(); } break; - case Reader::CDATA : - case Reader::TEXT : + case Reader::CDATA: + case Reader::TEXT: $writer->text( $reader->value ); break; - case Reader::END_ELEMENT : + case Reader::END_ELEMENT: $writer->endElement(); break; - } - } - } /** @@ -145,12 +138,11 @@ function xmlSerialize(Writer $writer) { * * @return mixed */ - static function xmlDeserialize(Reader $reader) { - + public static function xmlDeserialize(Reader $reader) + { $result = new self($reader->readInnerXml()); $reader->next(); - return $result; + return $result; } - } diff --git a/lib/LibXMLException.php b/lib/LibXMLException.php index af47a9f..4701c30 100644 --- a/lib/LibXMLException.php +++ b/lib/LibXMLException.php @@ -1,4 +1,6 @@ -errors = $errors; - parent::__construct($errors[0]->message . ' on line ' . $errors[0]->line . ', column ' . $errors[0]->column, $code, $previousException); - + parent::__construct($errors[0]->message.' on line '.$errors[0]->line.', column '.$errors[0]->column, $code, $previousException); } /** - * Returns the LibXML errors + * Returns the LibXML errors. */ - function getErrors() : array { - + public function getErrors(): array + { return $this->errors; - } - } diff --git a/lib/ParseException.php b/lib/ParseException.php index 56e60ca..e237b87 100644 --- a/lib/ParseException.php +++ b/lib/ParseException.php @@ -1,9 +1,10 @@ -localName) { + public function getClark() + { + if (!$this->localName) { return null; } - return '{' . $this->namespaceURI . '}' . $this->localName; - + return '{'.$this->namespaceURI.'}'.$this->localName; } /** @@ -52,14 +53,13 @@ function getClark() { * This function will also disable the standard libxml error handler (which * usually just results in PHP errors), and throw exceptions instead. */ - function parse() : array { - + public function parse(): array + { $previousEntityState = libxml_disable_entity_loader(true); $previousSetting = libxml_use_internal_errors(true); try { - - while ($this->nodeType !== self::ELEMENT) { + while (self::ELEMENT !== $this->nodeType) { if (!$this->read()) { $errors = libxml_get_errors(); libxml_clear_errors(); @@ -76,7 +76,6 @@ function parse() : array { if ($errors) { throw new LibXMLException($errors); } - } finally { libxml_use_internal_errors($previousSetting); libxml_disable_entity_loader($previousEntityState); @@ -85,8 +84,6 @@ function parse() : array { return $result; } - - /** * parseGetElements parses everything in the current sub-tree, * and returns a an array of elements. @@ -100,14 +97,14 @@ function parse() : array { * If the $elementMap argument is specified, the existing elementMap will * be overridden while parsing the tree, and restored after this process. */ - function parseGetElements(array $elementMap = null) : array { - + public function parseGetElements(array $elementMap = null): array + { $result = $this->parseInnerTree($elementMap); if (!is_array($result)) { return []; } - return $result; + return $result; } /** @@ -123,14 +120,15 @@ function parseGetElements(array $elementMap = null) : array { * * @return array|string */ - function parseInnerTree(array $elementMap = null) { - + public function parseInnerTree(array $elementMap = null) + { $text = null; $elements = []; - if ($this->nodeType === self::ELEMENT && $this->isEmptyElement) { + if (self::ELEMENT === $this->nodeType && $this->isEmptyElement) { // Easy! $this->next(); + return null; } @@ -140,7 +138,6 @@ function parseInnerTree(array $elementMap = null) { } try { - if (!$this->read()) { $errors = libxml_get_errors(); libxml_clear_errors(); @@ -151,9 +148,7 @@ function parseInnerTree(array $elementMap = null) { } while (true) { - if (!$this->isValid()) { - $errors = libxml_get_errors(); if ($errors) { @@ -163,44 +158,40 @@ function parseInnerTree(array $elementMap = null) { } switch ($this->nodeType) { - case self::ELEMENT : + case self::ELEMENT: $elements[] = $this->parseCurrentElement(); break; - case self::TEXT : - case self::CDATA : + case self::TEXT: + case self::CDATA: $text .= $this->value; $this->read(); break; - case self::END_ELEMENT : + case self::END_ELEMENT: // Ensuring we are moving the cursor after the end element. $this->read(); break 2; - case self::NONE : + case self::NONE: throw new ParseException('We hit the end of the document prematurely. This likely means that some parser "eats" too many elements. Do not attempt to continue parsing.'); - default : + default: // Advance to the next element $this->read(); break; } - } - } finally { - if (!is_null($elementMap)) { $this->popContext(); } - } - return ($elements ? $elements : $text); + return $elements ? $elements : $text; } /** * Reads all text below the current element, and returns this as a string. */ - function readText() : string { - + public function readText(): string + { $result = ''; $previousDepth = $this->depth; @@ -209,8 +200,8 @@ function readText() : string { $result .= $this->value; } } - return $result; + return $result; } /** @@ -221,8 +212,8 @@ function readText() : string { * * value - The parsed value. * * attributes - A key-value list of attributes. */ - function parseCurrentElement() : array { - + public function parseCurrentElement(): array + { $name = $this->getClark(); $attributes = []; @@ -237,13 +228,12 @@ function parseCurrentElement() : array { ); return [ - 'name' => $name, - 'value' => $value, + 'name' => $name, + 'value' => $value, 'attributes' => $attributes, ]; } - /** * Grabs all the attributes from the current element, and returns them as a * key-value array. @@ -252,21 +242,19 @@ function parseCurrentElement() : array { * short keys. If they are defined on a different namespace, the attribute * name will be retured in clark-notation. */ - function parseAttributes() : array { - + public function parseAttributes(): array + { $attributes = []; while ($this->moveToNextAttribute()) { if ($this->namespaceURI) { - // Ignoring 'xmlns', it doesn't make any sense. - if ($this->namespaceURI === 'http://www.w3.org/2000/xmlns/') { + if ('http://www.w3.org/2000/xmlns/' === $this->namespaceURI) { continue; } $name = $this->getClark(); $attributes[$name] = $this->value; - } else { $attributes[$this->localName] = $this->value; } @@ -274,17 +262,16 @@ function parseAttributes() : array { $this->moveToElement(); return $attributes; - } /** * Returns the function that should be used to parse the element identified * by it's clark-notation name. */ - function getDeserializerForElementName(string $name) : callable { - + public function getDeserializerForElementName(string $name): callable + { if (!array_key_exists($name, $this->elementMap)) { - if (substr($name, 0, 2) == '{}' && array_key_exists(substr($name, 2), $this->elementMap)) { + if ('{}' == substr($name, 0, 2) && array_key_exists(substr($name, 2), $this->elementMap)) { $name = substr($name, 2); } else { return ['Sabre\\Xml\\Element\\Base', 'xmlDeserialize']; @@ -301,13 +288,11 @@ function getDeserializerForElementName(string $name) : callable { } $type = gettype($deserializer); - if ($type === 'string') { - $type .= ' (' . $deserializer . ')'; - } elseif ($type === 'object') { - $type .= ' (' . get_class($deserializer) . ')'; + if ('string' === $type) { + $type .= ' ('.$deserializer.')'; + } elseif ('object' === $type) { + $type .= ' ('.get_class($deserializer).')'; } - throw new \LogicException('Could not use this type as a deserializer: ' . $type . ' for element: ' . $name); - + throw new \LogicException('Could not use this type as a deserializer: '.$type.' for element: '.$name); } - } diff --git a/lib/Serializer/functions.php b/lib/Serializer/functions.php index 7fe499a..3694d97 100644 --- a/lib/Serializer/functions.php +++ b/lib/Serializer/functions.php @@ -1,4 +1,6 @@ - * * @param string[] $values - * @return void */ -function enum(Writer $writer, array $values) { - +function enum(Writer $writer, array $values) +{ foreach ($values as $value) { $writer->writeElement($value); } @@ -54,27 +55,25 @@ function enum(Writer $writer, array $values) { * serialize empty properties, you must specify them as an empty string. * * @param object $valueObject - * @return void */ -function valueObject(Writer $writer, $valueObject, string $namespace) { +function valueObject(Writer $writer, $valueObject, string $namespace) +{ foreach (get_object_vars($valueObject) as $key => $val) { if (is_array($val)) { // If $val is an array, it has a special meaning. We need to // generate one child element for each item in $val foreach ($val as $child) { - $writer->writeElement('{' . $namespace . '}' . $key, $child); + $writer->writeElement('{'.$namespace.'}'.$key, $child); } - - } elseif ($val !== null) { - $writer->writeElement('{' . $namespace . '}' . $key, $val); + } elseif (null !== $val) { + $writer->writeElement('{'.$namespace.'}'.$key, $val); } } } - /** * This serializer helps you serialize xml structures that look like - * this: + * this:. * * * ... @@ -86,15 +85,12 @@ function valueObject(Writer $writer, $valueObject, string $namespace) { * and this could be called like this: * * repeatingElements($writer, $items, '{}item'); - * - * @return void */ -function repeatingElements(Writer $writer, array $items, string $childElementName) { - +function repeatingElements(Writer $writer, array $items, string $childElementName) +{ foreach ($items as $item) { $writer->writeElement($childElementName, $item); } - } /** @@ -153,36 +149,24 @@ function repeatingElements(Writer $writer, array $items, string $childElementNam * You can even mix the two array syntaxes. * * @param string|int|float|bool|array|object - * @return void */ -function standardSerializer(Writer $writer, $value) { - +function standardSerializer(Writer $writer, $value) +{ if (is_scalar($value)) { - // String, integer, float, boolean - $writer->text((string)$value); - + $writer->text((string) $value); } elseif ($value instanceof XmlSerializable) { - // XmlSerializable classes or Element classes. $value->xmlSerialize($writer); - } elseif (is_object($value) && isset($writer->classMap[get_class($value)])) { - // It's an object which class appears in the classmap. $writer->classMap[get_class($value)]($writer, $value); - } elseif (is_callable($value)) { - // A callback $value($writer); - } elseif (is_null($value)) { - // nothing! - } elseif (is_array($value) && array_key_exists('name', $value)) { - // if the array had a 'name' element, we assume that this array // describes a 'name' and optionally 'attributes' and 'value'. @@ -194,19 +178,13 @@ function standardSerializer(Writer $writer, $value) { $writer->writeAttributes($attributes); $writer->write($value); $writer->endElement(); - } elseif (is_array($value)) { - foreach ($value as $name => $item) { - if (is_int($name)) { - // This item has a numeric index. We just loop through the // array and throw it back in the writer. standardSerializer($writer, $item); - } elseif (is_string($name) && is_array($item) && isset($item['attributes'])) { - // The key is used for a name, but $item has 'attributes' and // possibly 'value' $writer->startElement($name); @@ -215,29 +193,18 @@ function standardSerializer(Writer $writer, $value) { $writer->write($item['value']); } $writer->endElement(); - } elseif (is_string($name)) { - // This was a plain key-value array. $writer->startElement($name); $writer->write($item); $writer->endElement(); - } else { - - throw new InvalidArgumentException('The writer does not know how to serialize arrays with keys of type: ' . gettype($name)); - + throw new InvalidArgumentException('The writer does not know how to serialize arrays with keys of type: '.gettype($name)); } } - } elseif (is_object($value)) { - - throw new InvalidArgumentException('The writer cannot serialize objects of class: ' . get_class($value)); - + throw new InvalidArgumentException('The writer cannot serialize objects of class: '.get_class($value)); } else { - - throw new InvalidArgumentException('The writer cannot serialize values of type: ' . gettype($value)); - + throw new InvalidArgumentException('The writer cannot serialize values of type: '.gettype($value)); } - } diff --git a/lib/Service.php b/lib/Service.php index 3f492dc..a175dc9 100644 --- a/lib/Service.php +++ b/lib/Service.php @@ -1,4 +1,6 @@ -elementMap = $this->elementMap; - return $r; + return $r; } /** - * Returns a fresh xml writer + * Returns a fresh xml writer. */ - function getWriter() : Writer { - + public function getWriter(): Writer + { $w = new Writer(); $w->namespaceMap = $this->namespaceMap; $w->classMap = $this->classMap; - return $w; + return $w; } /** @@ -95,11 +97,13 @@ function getWriter() : Writer { * with the root element name of the document. * * @param string|resource $input + * * @throws ParseException + * * @return array|object|string */ - function parse($input, string $contextUri = null, string &$rootElementName = null) { - + public function parse($input, string $contextUri = null, string &$rootElementName = null) + { if (is_resource($input)) { // Unfortunately the XMLReader doesn't support streams. When it // does, we can optimize this. @@ -111,8 +115,8 @@ function parse($input, string $contextUri = null, string &$rootElementName = nul $result = $r->parse(); $rootElementName = $result['name']; - return $result['value']; + return $result['value']; } /** @@ -131,12 +135,14 @@ function parse($input, string $contextUri = null, string &$rootElementName = nul * * @param string|string[] $rootElementName * @param string|resource $input - * @param string|null $contextUri + * @param string|null $contextUri + * * @throws ParseException + * * @return array|object|string */ - function expect($rootElementName, $input, string $contextUri = null) { - + public function expect($rootElementName, $input, string $contextUri = null) + { if (is_resource($input)) { // Unfortunately the XMLReader doesn't support streams. When it // does, we can optimize this. @@ -146,18 +152,20 @@ function expect($rootElementName, $input, string $contextUri = null) { $r->contextUri = $contextUri; $r->xml($input); - $rootElementName = (array)$rootElementName; + $rootElementName = (array) $rootElementName; foreach ($rootElementName as &$rEl) { - if ($rEl[0] !== '{') $rEl = '{}' . $rEl; + if ('{' !== $rEl[0]) { + $rEl = '{}'.$rEl; + } } $result = $r->parse(); if (!in_array($result['name'], $rootElementName, true)) { - throw new ParseException('Expected ' . implode(' or ', (array)$rootElementName) . ' but received ' . $result['name'] . ' as the root element'); + throw new ParseException('Expected '.implode(' or ', (array) $rootElementName).' but received '.$result['name'].' as the root element'); } - return $result['value']; + return $result['value']; } /** @@ -175,18 +183,19 @@ function expect($rootElementName, $input, string $contextUri = null) { * of the domain. * * @param string|array|XmlSerializable $value + * * @return string */ - function write(string $rootElementName, $value, string $contextUri = null) { - + public function write(string $rootElementName, $value, string $contextUri = null) + { $w = $this->getWriter(); $w->openMemory(); $w->contextUri = $contextUri; $w->setIndent(true); $w->startDocument(); $w->writeElement($rootElementName, $value); - return $w->outputMemory(); + return $w->outputMemory(); } /** @@ -212,16 +221,15 @@ function write(string $rootElementName, $value, string $contextUri = null) { * These can easily be mapped by calling: * * $service->mapValueObject('{http://example.org}author', 'Author'); - * - * @return void */ - function mapValueObject(string $elementName, string $className) { + public function mapValueObject(string $elementName, string $className) + { list($namespace) = self::parseClarkNotation($elementName); - $this->elementMap[$elementName] = function(Reader $reader) use ($className, $namespace) { + $this->elementMap[$elementName] = function (Reader $reader) use ($className, $namespace) { return \Sabre\Xml\Deserializer\valueObject($reader, $className, $namespace); }; - $this->classMap[$className] = function(Writer $writer, $valueObject) use ($namespace) { + $this->classMap[$className] = function (Writer $writer, $valueObject) use ($namespace) { return \Sabre\Xml\Serializer\valueObject($writer, $valueObject, $namespace); }; $this->valueObjectMap[$className] = $elementName; @@ -237,20 +245,20 @@ function mapValueObject(string $elementName, string $className) { * mapValueObject(). * * @param object $object + * * @throws \InvalidArgumentException - * @return void */ - function writeValueObject($object, string $contextUri = null) { - + public function writeValueObject($object, string $contextUri = null) + { if (!isset($this->valueObjectMap[get_class($object)])) { - throw new \InvalidArgumentException('"' . get_class($object) . '" is not a registered value object class. Register your class with mapValueObject.'); + throw new \InvalidArgumentException('"'.get_class($object).'" is not a registered value object class. Register your class with mapValueObject.'); } + return $this->write( $this->valueObjectMap[get_class($object)], $object, $contextUri ); - } /** @@ -261,18 +269,18 @@ function writeValueObject($object, string $contextUri = null) { * * @throws \InvalidArgumentException */ - static function parseClarkNotation(string $str) : array { + public static function parseClarkNotation(string $str): array + { static $cache = []; if (!isset($cache[$str])) { - if (!preg_match('/^{([^}]*)}(.*)$/', $str, $matches)) { - throw new \InvalidArgumentException('\'' . $str . '\' is not a valid clark-notation formatted string'); + throw new \InvalidArgumentException('\''.$str.'\' is not a valid clark-notation formatted string'); } $cache[$str] = [ $matches[1], - $matches[2] + $matches[2], ]; } @@ -283,5 +291,4 @@ static function parseClarkNotation(string $str) : array { * A list of classes and which XML elements they map to. */ protected $valueObjectMap = []; - } diff --git a/lib/Version.php b/lib/Version.php index d904de0..65706ec 100644 --- a/lib/Version.php +++ b/lib/Version.php @@ -1,4 +1,6 @@ -namespaceMap)) { $result = $this->startElementNS( - $this->namespaceMap[$namespace] === '' ? null : $this->namespaceMap[$namespace], + '' === $this->namespaceMap[$namespace] ? null : $this->namespaceMap[$namespace], $localName, null ); } else { - // An empty namespace means it's the global namespace. This is // allowed, but it mustn't get a prefix. - if ($namespace === "" || $namespace === null) { + if ('' === $namespace || null === $namespace) { $result = $this->startElement($localName); $this->writeAttribute('xmlns', ''); } else { if (!isset($this->adhocNamespaces[$namespace])) { - $this->adhocNamespaces[$namespace] = 'x' . (count($this->adhocNamespaces) + 1); + $this->adhocNamespaces[$namespace] = 'x'.(count($this->adhocNamespaces) + 1); } $result = $this->startElementNS($this->adhocNamespaces[$namespace], $localName, $namespace); } } - } else { $result = parent::startElement($name); } if (!$this->namespacesWritten) { - foreach ($this->namespaceMap as $namespace => $prefix) { - $this->writeAttribute(($prefix ? 'xmlns:' . $prefix : 'xmlns'), $namespace); + $this->writeAttribute(($prefix ? 'xmlns:'.$prefix : 'xmlns'), $namespace); } $this->namespacesWritten = true; - } return $result; - } /** @@ -188,10 +182,11 @@ function startElement($name) : bool { * XMLWriter::startElement doesn't either. * * @param array|string|object|null $content + * * @return bool */ - function writeElement($name, $content = null) : bool { - + public function writeElement($name, $content = null): bool + { $this->startElement($name); if (!is_null($content)) { $this->write($content); @@ -199,7 +194,6 @@ function writeElement($name, $content = null) : bool { $this->endElement(); return true; - } /** @@ -210,15 +204,12 @@ function writeElement($name, $content = null) : bool { * The key is an attribute name. If the key is a 'localName', the current * xml namespace is assumed. If it's a 'clark notation key', this namespace * will be used instead. - * - * @return void */ - function writeAttributes(array $attributes) { - + public function writeAttributes(array $attributes) + { foreach ($attributes as $name => $value) { $this->writeAttribute($name, $value); } - } /** @@ -234,9 +225,9 @@ function writeAttributes(array $attributes) { * @param string $name * @param string $value */ - function writeAttribute($name, $value) : bool { - - if ($name[0] !== '{') { + public function writeAttribute($name, $value): bool + { + if ('{' !== $name[0]) { return parent::writeAttribute($name, $value); } @@ -248,23 +239,21 @@ function writeAttribute($name, $value) : bool { if (array_key_exists($namespace, $this->namespaceMap)) { // It's an attribute with a namespace we know return $this->writeAttribute( - $this->namespaceMap[$namespace] . ':' . $localName, + $this->namespaceMap[$namespace].':'.$localName, $value ); - } // We don't know the namespace, we must add it in-line if (!isset($this->adhocNamespaces[$namespace])) { - $this->adhocNamespaces[$namespace] = 'x' . (count($this->adhocNamespaces) + 1); + $this->adhocNamespaces[$namespace] = 'x'.(count($this->adhocNamespaces) + 1); } + return $this->writeAttributeNS( $this->adhocNamespaces[$namespace], $localName, $namespace, $value ); - } - } diff --git a/lib/XmlDeserializable.php b/lib/XmlDeserializable.php index b08b9ec..83f33db 100644 --- a/lib/XmlDeserializable.php +++ b/lib/XmlDeserializable.php @@ -1,4 +1,6 @@ -stack = $this->getMockForTrait('Sabre\\Xml\\ContextStackTrait'); - } - function testPushAndPull() { - + public function testPushAndPull() + { $this->stack->contextUri = '/foo/bar'; $this->stack->elementMap['{DAV:}foo'] = 'Bar'; $this->stack->namespaceMap['DAV:'] = 'd'; @@ -38,7 +39,5 @@ function testPushAndPull() { $this->assertEquals('/foo/bar', $this->stack->contextUri); $this->assertEquals('Bar', $this->stack->elementMap['{DAV:}foo']); $this->assertEquals('d', $this->stack->namespaceMap['DAV:']); - } - } diff --git a/tests/Sabre/Xml/Deserializer/EnumTest.php b/tests/Sabre/Xml/Deserializer/EnumTest.php index 3639195..c56ec0b 100644 --- a/tests/Sabre/Xml/Deserializer/EnumTest.php +++ b/tests/Sabre/Xml/Deserializer/EnumTest.php @@ -1,13 +1,15 @@ -elementMap['{urn:test}root'] = 'Sabre\Xml\Deserializer\enum'; @@ -26,16 +28,13 @@ function testDeserialize() { '{urn:test}foo2', ]; - $this->assertEquals($expected, $result); - - } - function testDeserializeDefaultNamespace() { - + public function testDeserializeDefaultNamespace() + { $service = new Service(); - $service->elementMap['{urn:test}root'] = function($reader) { + $service->elementMap['{urn:test}root'] = function ($reader) { return enum($reader, 'urn:test'); }; @@ -54,12 +53,10 @@ function testDeserializeDefaultNamespace() { 'foo2', ]; - $this->assertEquals($expected, $result); - } - function testEmptyEnum() + public function testEmptyEnum() { $service = new Service(); $service->elementMap['{urn:test}enum'] = 'Sabre\Xml\Deserializer\enum'; @@ -76,10 +73,10 @@ function testEmptyEnum() $result = $service->parse($xml); $this->assertEquals([[ - 'name' => '{urn:test}inner', + 'name' => '{urn:test}inner', 'value' => [[ - 'name' => '{urn:test}enum', - 'value' => [], + 'name' => '{urn:test}enum', + 'value' => [], 'attributes' => [], ]], 'attributes' => [], diff --git a/tests/Sabre/Xml/Deserializer/KeyValueTest.php b/tests/Sabre/Xml/Deserializer/KeyValueTest.php index 7a57839..a6c41bc 100644 --- a/tests/Sabre/Xml/Deserializer/KeyValueTest.php +++ b/tests/Sabre/Xml/Deserializer/KeyValueTest.php @@ -1,14 +1,15 @@ - @@ -26,37 +27,37 @@ function testKeyValue() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}struct' => function(Reader $reader) { + '{http://sabredav.org/ns}struct' => function (Reader $reader) { return keyValue($reader, 'http://sabredav.org/ns'); - } + }, ]; $reader->xml($input); $output = $reader->parse(); $this->assertEquals([ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}struct', + 'name' => '{http://sabredav.org/ns}struct', 'value' => [ - 'elem1' => null, - 'elem2' => 'hi', + 'elem1' => null, + 'elem2' => 'hi', '{http://sabredav.org/another-ns}elem3' => [ [ - 'name' => '{http://sabredav.org/another-ns}elem4', - 'value' => 'foo', + 'name' => '{http://sabredav.org/another-ns}elem4', + 'value' => 'foo', 'attributes' => [], ], [ - 'name' => '{http://sabredav.org/another-ns}elem5', - 'value' => 'foo & bar', + 'name' => '{http://sabredav.org/another-ns}elem5', + 'value' => 'foo & bar', 'attributes' => [], ], ], 'elem6' => null, ], 'attributes' => [], - ] + ], ], 'attributes' => [], ], $output); @@ -65,8 +66,8 @@ function testKeyValue() { /** * @expectedException \Sabre\Xml\LibXMLException */ - function testKeyValueLoop() { - + public function testKeyValueLoop() + { /** * This bug is a weird one, because it triggers an infinite loop, but * only if the XML document is a certain size (in bytes). Removing one @@ -92,13 +93,12 @@ function testKeyValueLoop() { $reader->xml($invalid_xml); $reader->elementMap = [ - - '{}Package' => function($reader) { + '{}Package' => function ($reader) { $recipient = []; // Borrowing a parser from the KeyValue class. $keyValue = keyValue($reader); - if (isset($keyValue['{}WeightOz'])){ + if (isset($keyValue['{}WeightOz'])) { $recipient['referenceId'] = $keyValue['{}WeightOz']; } @@ -107,11 +107,9 @@ function testKeyValueLoop() { ]; $reader->parse(); - - } - function testEmptyKeyValue() + public function testEmptyKeyValue() { // the nested structure below is necessary to detect if one of the deserialization functions eats to much elements $input = <<elementMap = [ - '{http://sabredav.org/ns}struct' => function(Reader $reader) { + '{http://sabredav.org/ns}struct' => function (Reader $reader) { return keyValue($reader, 'http://sabredav.org/ns'); }, ]; @@ -133,14 +131,14 @@ function testEmptyKeyValue() $output = $reader->parse(); $this->assertEquals([ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}inner', + 'name' => '{http://sabredav.org/ns}inner', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}struct', - 'value' => [], + 'name' => '{http://sabredav.org/ns}struct', + 'value' => [], 'attributes' => [], ], ], diff --git a/tests/Sabre/Xml/Deserializer/MixedContentTest.php b/tests/Sabre/Xml/Deserializer/MixedContentTest.php index 31216d4..67f6973 100644 --- a/tests/Sabre/Xml/Deserializer/MixedContentTest.php +++ b/tests/Sabre/Xml/Deserializer/MixedContentTest.php @@ -1,12 +1,15 @@ -elementMap['{}p'] = 'Sabre\Xml\Deserializer\mixedContent'; @@ -20,11 +23,11 @@ function testDeserialize() { $expected = [ 'This is some text ', [ - 'name' => '{}extref', - 'value' => 'and a inline tag', - 'attributes' => [] + 'name' => '{}extref', + 'value' => 'and a inline tag', + 'attributes' => [], ], - 'and even more text' + 'and even more text', ]; $this->assertEquals($expected, $result); diff --git a/tests/Sabre/Xml/Deserializer/RepeatingElementsTest.php b/tests/Sabre/Xml/Deserializer/RepeatingElementsTest.php index 46670b1..d5f96b3 100644 --- a/tests/Sabre/Xml/Deserializer/RepeatingElementsTest.php +++ b/tests/Sabre/Xml/Deserializer/RepeatingElementsTest.php @@ -1,15 +1,17 @@ -elementMap['{urn:test}collection'] = function($reader) { + $service->elementMap['{urn:test}collection'] = function ($reader) { return repeatingElements($reader, '{urn:test}item'); }; @@ -29,7 +31,5 @@ function testRead() { ]; $this->assertEquals($expected, $result); - } - } diff --git a/tests/Sabre/Xml/Deserializer/ValueObjectTest.php b/tests/Sabre/Xml/Deserializer/ValueObjectTest.php index 99c4beb..17472a9 100644 --- a/tests/Sabre/Xml/Deserializer/ValueObjectTest.php +++ b/tests/Sabre/Xml/Deserializer/ValueObjectTest.php @@ -1,14 +1,15 @@ - @@ -20,9 +21,9 @@ function testDeserializeValueObject() { $reader = new Reader(); $reader->xml($input); $reader->elementMap = [ - '{urn:foo}foo' => function(Reader $reader) { + '{urn:foo}foo' => function (Reader $reader) { return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo'); - } + }, ]; $output = $reader->parse(); @@ -32,20 +33,19 @@ function testDeserializeValueObject() { $vo->lastName = 'Turtle'; $expected = [ - 'name' => '{urn:foo}foo', - 'value' => $vo, - 'attributes' => [] + 'name' => '{urn:foo}foo', + 'value' => $vo, + 'attributes' => [], ]; $this->assertEquals( $expected, $output ); - } - function testDeserializeValueObjectIgnoredElement() { - + public function testDeserializeValueObjectIgnoredElement() + { $input = << @@ -58,9 +58,9 @@ function testDeserializeValueObjectIgnoredElement() { $reader = new Reader(); $reader->xml($input); $reader->elementMap = [ - '{urn:foo}foo' => function(Reader $reader) { + '{urn:foo}foo' => function (Reader $reader) { return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo'); - } + }, ]; $output = $reader->parse(); @@ -70,20 +70,19 @@ function testDeserializeValueObjectIgnoredElement() { $vo->lastName = 'Turtle'; $expected = [ - 'name' => '{urn:foo}foo', - 'value' => $vo, - 'attributes' => [] + 'name' => '{urn:foo}foo', + 'value' => $vo, + 'attributes' => [], ]; $this->assertEquals( $expected, $output ); - } - function testDeserializeValueObjectAutoArray() { - + public function testDeserializeValueObjectAutoArray() + { $input = << @@ -97,9 +96,9 @@ function testDeserializeValueObjectAutoArray() { $reader = new Reader(); $reader->xml($input); $reader->elementMap = [ - '{urn:foo}foo' => function(Reader $reader) { + '{urn:foo}foo' => function (Reader $reader) { return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo'); - } + }, ]; $output = $reader->parse(); @@ -112,21 +111,20 @@ function testDeserializeValueObjectAutoArray() { 'http://example.net/', ]; - $expected = [ - 'name' => '{urn:foo}foo', - 'value' => $vo, - 'attributes' => [] + 'name' => '{urn:foo}foo', + 'value' => $vo, + 'attributes' => [], ]; $this->assertEquals( $expected, $output ); - } - function testDeserializeValueObjectEmpty() { + public function testDeserializeValueObjectEmpty() + { $input = << @@ -135,9 +133,9 @@ function testDeserializeValueObjectEmpty() { $reader = new Reader(); $reader->xml($input); $reader->elementMap = [ - '{urn:foo}foo' => function(Reader $reader) { + '{urn:foo}foo' => function (Reader $reader) { return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo'); - } + }, ]; $output = $reader->parse(); @@ -145,25 +143,22 @@ function testDeserializeValueObjectEmpty() { $vo = new TestVo(); $expected = [ - 'name' => '{urn:foo}foo', - 'value' => $vo, - 'attributes' => [] + 'name' => '{urn:foo}foo', + 'value' => $vo, + 'attributes' => [], ]; $this->assertEquals( $expected, $output ); - } - } -class TestVo { - +class TestVo +{ public $firstName; public $lastName; public $link = []; - } diff --git a/tests/Sabre/Xml/Element/CDataTest.php b/tests/Sabre/Xml/Element/CDataTest.php index deb4a70..dd3aa90 100644 --- a/tests/Sabre/Xml/Element/CDataTest.php +++ b/tests/Sabre/Xml/Element/CDataTest.php @@ -1,17 +1,19 @@ - @@ -26,14 +28,13 @@ function testDeserialize() { $reader->xml($input); $output = $reader->parse(); - } - function testSerialize() { - + public function testSerialize() + { $writer = new Writer(); $writer->namespaceMap = [ - 'http://sabredav.org/ns' => null + 'http://sabredav.org/ns' => null, ]; $writer->openMemory(); $writer->startDocument('1.0'); @@ -51,8 +52,5 @@ function testSerialize() { XML; $this->assertEquals($expected, $output); - - } - } diff --git a/tests/Sabre/Xml/Element/Eater.php b/tests/Sabre/Xml/Element/Eater.php index 4e419ff..3a79c21 100644 --- a/tests/Sabre/Xml/Element/Eater.php +++ b/tests/Sabre/Xml/Element/Eater.php @@ -1,4 +1,6 @@ -startElement('{http://sabredav.org/ns}elem1'); $writer->write('hiiii!'); $writer->endElement(); - } /** @@ -56,23 +56,20 @@ function xmlSerialize(Xml\Writer $writer) { * the next element. * * @param Xml\Reader $reader + * * @return mixed */ - static function xmlDeserialize(Xml\Reader $reader) { - + public static function xmlDeserialize(Xml\Reader $reader) + { $reader->next(); $count = 1; while ($count) { - $reader->read(); if ($reader->nodeType === $reader::END_ELEMENT) { - $count--; + --$count; } - } $reader->read(); - } - } diff --git a/tests/Sabre/Xml/Element/ElementsTest.php b/tests/Sabre/Xml/Element/ElementsTest.php index 660a9e3..d50b7b4 100644 --- a/tests/Sabre/Xml/Element/ElementsTest.php +++ b/tests/Sabre/Xml/Element/ElementsTest.php @@ -1,14 +1,16 @@ - @@ -38,10 +40,10 @@ function testDeserialize() { $output = $reader->parse(); $this->assertEquals([ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}listThingy', + 'name' => '{http://sabredav.org/ns}listThingy', 'value' => [ '{http://sabredav.org/ns}elem1', '{http://sabredav.org/ns}elem2', @@ -53,26 +55,26 @@ function testDeserialize() { 'attributes' => [], ], [ - 'name' => '{http://sabredav.org/ns}listThingy', - 'value' => [], + 'name' => '{http://sabredav.org/ns}listThingy', + 'value' => [], 'attributes' => [], ], [ - 'name' => '{http://sabredav.org/ns}otherThing', + 'name' => '{http://sabredav.org/ns}otherThing', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => null, + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => null, 'attributes' => [], ], [ - 'name' => '{http://sabredav.org/ns}elem2', - 'value' => null, + 'name' => '{http://sabredav.org/ns}elem2', + 'value' => null, 'attributes' => [], ], [ - 'name' => '{http://sabredav.org/ns}elem3', - 'value' => null, + 'name' => '{http://sabredav.org/ns}elem3', + 'value' => null, 'attributes' => [], ], ], @@ -81,11 +83,10 @@ function testDeserialize() { ], 'attributes' => [], ], $output); - } - function testSerialize() { - + public function testSerialize() + { $value = [ '{http://sabredav.org/ns}elem1', '{http://sabredav.org/ns}elem2', @@ -97,7 +98,7 @@ function testSerialize() { $writer = new Writer(); $writer->namespaceMap = [ - 'http://sabredav.org/ns' => null + 'http://sabredav.org/ns' => null, ]; $writer->openMemory(); $writer->startDocument('1.0'); @@ -122,8 +123,5 @@ function testSerialize() { XML; $this->assertEquals($expected, $output); - - } - } diff --git a/tests/Sabre/Xml/Element/KeyValueTest.php b/tests/Sabre/Xml/Element/KeyValueTest.php index 6ff3a7f..dc6085a 100644 --- a/tests/Sabre/Xml/Element/KeyValueTest.php +++ b/tests/Sabre/Xml/Element/KeyValueTest.php @@ -1,14 +1,16 @@ - @@ -37,22 +39,22 @@ function testDeserialize() { $output = $reader->parse(); $this->assertEquals([ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}struct', + 'name' => '{http://sabredav.org/ns}struct', 'value' => [ '{http://sabredav.org/ns}elem1' => null, '{http://sabredav.org/ns}elem2' => 'hi', '{http://sabredav.org/ns}elem3' => [ [ - 'name' => '{http://sabredav.org/ns}elem4', - 'value' => 'foo', + 'name' => '{http://sabredav.org/ns}elem4', + 'value' => 'foo', 'attributes' => [], ], [ - 'name' => '{http://sabredav.org/ns}elem5', - 'value' => 'foo & bar', + 'name' => '{http://sabredav.org/ns}elem5', + 'value' => 'foo & bar', 'attributes' => [], ], ], @@ -61,16 +63,16 @@ function testDeserialize() { 'attributes' => [], ], [ - 'name' => '{http://sabredav.org/ns}struct', - 'value' => [], + 'name' => '{http://sabredav.org/ns}struct', + 'value' => [], 'attributes' => [], ], [ - 'name' => '{http://sabredav.org/ns}otherThing', + 'name' => '{http://sabredav.org/ns}otherThing', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => null, + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => null, 'attributes' => [], ], ], @@ -79,15 +81,14 @@ function testDeserialize() { ], 'attributes' => [], ], $output); - } /** * This test was added to find out why an element gets eaten by the * SabreDAV MKCOL parser. */ - function testElementEater() { - + public function testElementEater() + { $input = << @@ -102,17 +103,17 @@ function testElementEater() { $reader = new Reader(); $reader->elementMap = [ - '{DAV:}set' => 'Sabre\\Xml\\Element\\KeyValue', - '{DAV:}prop' => 'Sabre\\Xml\\Element\\KeyValue', + '{DAV:}set' => 'Sabre\\Xml\\Element\\KeyValue', + '{DAV:}prop' => 'Sabre\\Xml\\Element\\KeyValue', '{DAV:}resourcetype' => 'Sabre\\Xml\\Element\\Elements', ]; $reader->xml($input); $expected = [ - 'name' => '{DAV:}mkcol', + 'name' => '{DAV:}mkcol', 'value' => [ [ - 'name' => '{DAV:}set', + 'name' => '{DAV:}set', 'value' => [ '{DAV:}prop' => [ '{DAV:}resourcetype' => [ @@ -128,12 +129,10 @@ function testElementEater() { ]; $this->assertEquals($expected, $reader->parse()); - } - - function testSerialize() { - + public function testSerialize() + { $value = [ '{http://sabredav.org/ns}elem1' => null, '{http://sabredav.org/ns}elem2' => 'textValue', @@ -146,7 +145,7 @@ function testSerialize() { $writer = new Writer(); $writer->namespaceMap = [ - 'http://sabredav.org/ns' => null + 'http://sabredav.org/ns' => null, ]; $writer->openMemory(); $writer->startDocument('1.0'); @@ -172,15 +171,14 @@ function testSerialize() { XML; $this->assertEquals($expected, $output); - } /** * I discovered that when there's no whitespace between elements, elements * can get skipped. */ - function testElementSkipProblem() { - + public function testElementSkipProblem() + { $input = << @@ -196,7 +194,7 @@ function testElementSkipProblem() { $output = $reader->parse(); $this->assertEquals([ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ '{http://sabredav.org/ns}elem3' => 'val3', '{http://sabredav.org/ns}elem4' => 'val4', @@ -204,7 +202,5 @@ function testElementSkipProblem() { ], 'attributes' => [], ], $output); - } - } diff --git a/tests/Sabre/Xml/Element/Mock.php b/tests/Sabre/Xml/Element/Mock.php index 920e45a..cde7d7e 100644 --- a/tests/Sabre/Xml/Element/Mock.php +++ b/tests/Sabre/Xml/Element/Mock.php @@ -1,11 +1,13 @@ -startElement('{http://sabredav.org/ns}elem1'); $writer->write('hiiii!'); $writer->endElement(); - } /** @@ -48,13 +48,13 @@ function xmlSerialize(Xml\Writer $writer) { * the next element. * * @param Xml\Reader $reader + * * @return mixed */ - static function xmlDeserialize(Xml\Reader $reader) { - + public static function xmlDeserialize(Xml\Reader $reader) + { $reader->next(); - return 'foobar'; + return 'foobar'; } - } diff --git a/tests/Sabre/Xml/Element/UriTest.php b/tests/Sabre/Xml/Element/UriTest.php index f44f6c3..3c51318 100644 --- a/tests/Sabre/Xml/Element/UriTest.php +++ b/tests/Sabre/Xml/Element/UriTest.php @@ -1,14 +1,16 @@ - @@ -27,26 +29,25 @@ function testDeserialize() { $this->assertEquals( [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}uri', - 'value' => new Uri('http://example.org/foo/bar'), + 'name' => '{http://sabredav.org/ns}uri', + 'value' => new Uri('http://example.org/foo/bar'), 'attributes' => [], - ] + ], ], 'attributes' => [], ], $output ); - } - function testSerialize() { - + public function testSerialize() + { $writer = new Writer(); $writer->namespaceMap = [ - 'http://sabredav.org/ns' => null + 'http://sabredav.org/ns' => null, ]; $writer->openMemory(); $writer->startDocument('1.0'); @@ -55,7 +56,7 @@ function testSerialize() { $writer->write([ '{http://sabredav.org/ns}root' => [ '{http://sabredav.org/ns}uri' => new Uri('/foo/bar'), - ] + ], ]); $output = $writer->outputMemory(); @@ -69,8 +70,5 @@ function testSerialize() { XML; $this->assertEquals($expected, $output); - - } - } diff --git a/tests/Sabre/Xml/Element/XmlFragmentTest.php b/tests/Sabre/Xml/Element/XmlFragmentTest.php index 9a46752..bb803f9 100644 --- a/tests/Sabre/Xml/Element/XmlFragmentTest.php +++ b/tests/Sabre/Xml/Element/XmlFragmentTest.php @@ -1,17 +1,19 @@ - @@ -28,17 +30,16 @@ function testDeserialize($input, $expected) { $output = $reader->parse(); $this->assertEquals([ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}fragment', - 'value' => new XmlFragment($expected), + 'name' => '{http://sabredav.org/ns}fragment', + 'value' => new XmlFragment($expected), 'attributes' => [], ], ], 'attributes' => [], ], $output); - } /** @@ -51,11 +52,9 @@ function testDeserialize($input, $expected) { * 3. Expected output after serializing that value again. * * If 3 is not set, use 1 for 3. - * - * @return void */ - function xmlProvider() { - + public function xmlProvider() + { return [ [ 'hello', @@ -63,15 +62,15 @@ function xmlProvider() { ], [ 'hello', - 'hello' + 'hello', ], [ 'hello', - 'hello' + 'hello', ], [ 'hello', - 'hello' + 'hello', ], [ 'hello', @@ -104,21 +103,20 @@ function xmlProvider() { '', ], ]; - } /** * @dataProvider xmlProvider */ - function testSerialize($expectedFallback, $input, $expected = null) { - + public function testSerialize($expectedFallback, $input, $expected = null) + { if (is_null($expected)) { $expected = $expectedFallback; } $writer = new Writer(); $writer->namespaceMap = [ - 'http://sabredav.org/ns' => null + 'http://sabredav.org/ns' => null, ]; $writer->openMemory(); $writer->startDocument('1.0'); @@ -137,7 +135,5 @@ function testSerialize($expectedFallback, $input, $expected = null) { XML; $this->assertEquals($expected, $output); - } - } diff --git a/tests/Sabre/Xml/InfiteLoopTest.php b/tests/Sabre/Xml/InfiteLoopTest.php index 23fee57..404eaae 100644 --- a/tests/Sabre/Xml/InfiteLoopTest.php +++ b/tests/Sabre/Xml/InfiteLoopTest.php @@ -1,15 +1,17 @@ - @@ -25,17 +27,17 @@ function testDeserialize() { $output = $reader->parse(); $this->assertEquals([ - 'name' => '{DAV:}propertyupdate', + 'name' => '{DAV:}propertyupdate', 'value' => [ [ - 'name' => '{DAV:}set', + 'name' => '{DAV:}set', 'value' => [ '{DAV:}prop' => null, ], 'attributes' => [], ], [ - 'name' => '{DAV:}set', + 'name' => '{DAV:}set', 'value' => [ '{DAV:}prop' => null, ], @@ -44,7 +46,5 @@ function testDeserialize() { ], 'attributes' => [], ], $output); - } - } diff --git a/tests/Sabre/Xml/ReaderTest.php b/tests/Sabre/Xml/ReaderTest.php index 8d3e2cf..42bb57a 100644 --- a/tests/Sabre/Xml/ReaderTest.php +++ b/tests/Sabre/Xml/ReaderTest.php @@ -1,11 +1,13 @@ - @@ -16,11 +18,10 @@ function testGetClark() { $reader->next(); $this->assertEquals('{http://sabredav.org/ns}root', $reader->getClark()); - } - function testGetClarkNoNS() { - + public function testGetClarkNoNS() + { $input = << @@ -31,11 +32,10 @@ function testGetClarkNoNS() { $reader->next(); $this->assertEquals('{}root', $reader->getClark()); - } - function testGetClarkNotOnAnElement() { - + public function testGetClarkNotOnAnElement() + { $input = << @@ -46,8 +46,8 @@ function testGetClarkNotOnAnElement() { $this->assertNull($reader->getClark()); } - function testSimple() { - + public function testSimple() + { $input = << @@ -64,38 +64,35 @@ function testSimple() { $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => null, + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => null, 'attributes' => [ 'attr' => 'val', ], ], [ - 'name' => '{http://sabredav.org/ns}elem2', + 'name' => '{http://sabredav.org/ns}elem2', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem3', - 'value' => 'Hi!', + 'name' => '{http://sabredav.org/ns}elem3', + 'value' => 'Hi!', 'attributes' => [], ], ], 'attributes' => [], ], - ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } - function testCDATA() { - + public function testCDATA() + { $input = << @@ -109,25 +106,22 @@ function testCDATA() { $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}foo', - 'value' => 'bar', + 'name' => '{http://sabredav.org/ns}foo', + 'value' => 'bar', 'attributes' => [], ], - ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } - function testSimpleNamespacedAttribute() { - + public function testSimpleNamespacedAttribute() + { $input = << @@ -141,11 +135,11 @@ function testSimpleNamespacedAttribute() { $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => null, + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => null, 'attributes' => [ '{urn:foo}attr' => 'val', ], @@ -155,11 +149,10 @@ function testSimpleNamespacedAttribute() { ]; $this->assertEquals($expected, $output); - } - function testMappedElement() { - + public function testMappedElement() + { $input = << @@ -169,34 +162,32 @@ function testMappedElement() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Mock' + '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Mock', ]; $reader->xml($input); $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => 'foobar', + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => 'foobar', 'attributes' => [], ], ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } /** * @expectedException \LogicException */ - function testMappedElementBadClass() { - + public function testMappedElementBadClass() + { $input = << @@ -206,7 +197,7 @@ function testMappedElementBadClass() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => new \StdClass() + '{http://sabredav.org/ns}elem1' => new \StdClass(), ]; $reader->xml($input); @@ -216,8 +207,8 @@ function testMappedElementBadClass() { /** * @depends testMappedElement */ - function testMappedElementCallBack() { - + public function testMappedElementCallBack() + { $input = << @@ -227,37 +218,36 @@ function testMappedElementCallBack() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => function(Reader $reader) { + '{http://sabredav.org/ns}elem1' => function (Reader $reader) { $reader->next(); + return 'foobar'; - } + }, ]; $reader->xml($input); $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => 'foobar', + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => 'foobar', 'attributes' => [], ], ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } /** * @depends testMappedElementCallBack */ - function testMappedElementCallBackNoNamespace() { - + public function testMappedElementCallBackNoNamespace() + { $input = << @@ -267,37 +257,36 @@ function testMappedElementCallBackNoNamespace() { $reader = new Reader(); $reader->elementMap = [ - 'elem1' => function(Reader $reader) { + 'elem1' => function (Reader $reader) { $reader->next(); + return 'foobar'; - } + }, ]; $reader->xml($input); $output = $reader->parse(); $expected = [ - 'name' => '{}root', + 'name' => '{}root', 'value' => [ [ - 'name' => '{}elem1', - 'value' => 'foobar', + 'name' => '{}elem1', + 'value' => 'foobar', 'attributes' => [], ], ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } /** * @depends testMappedElementCallBack */ - function testReadText() { - + public function testReadText() + { $input = << @@ -310,33 +299,31 @@ function testReadText() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => function(Reader $reader) { + '{http://sabredav.org/ns}elem1' => function (Reader $reader) { return $reader->readText(); - } + }, ]; $reader->xml($input); $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => 'hello world', + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => 'hello world', 'attributes' => [], ], ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } - function testParseProblem() { - + public function testParseProblem() + { $input = << @@ -344,7 +331,7 @@ function testParseProblem() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Mock' + '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Mock', ]; $reader->xml($input); @@ -352,18 +339,15 @@ function testParseProblem() { $output = $reader->parse(); $this->fail('We expected a ParseException to be thrown'); } catch (LibXMLException $e) { - $this->assertInternalType('array', $e->getErrors()); - } - } /** * @expectedException \Sabre\Xml\ParseException */ - function testBrokenParserClass() { - + public function testBrokenParserClass() + { $input = << @@ -373,21 +357,19 @@ function testBrokenParserClass() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Eater' + '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Eater', ]; $reader->xml($input); $reader->parse(); - - } /** * Test was added for Issue #10. * - * @expectedException Sabre\Xml\LibXMLException + * @expectedException \Sabre\Xml\LibXMLException */ - function testBrokenXml() { - + public function testBrokenXml() + { $input = << @@ -398,16 +380,15 @@ function testBrokenXml() { $reader = new Reader(); $reader->xml($input); $reader->parse(); - } /** * Test was added for Issue #45. * - * @expectedException Sabre\Xml\LibXMLException + * @expectedException \Sabre\Xml\LibXMLException */ - function testBrokenXml2() { - + public function testBrokenXml2() + { $input = << @@ -424,15 +405,13 @@ function testBrokenXml2() { $reader = new Reader(); $reader->xml($input); $reader->parse(); - } - /** * @depends testMappedElement */ - function testParseInnerTree() { - + public function testParseInnerTree() + { $input = << @@ -444,48 +423,46 @@ function testParseInnerTree() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => function(Reader $reader) { - - $innerTree = $reader->parseInnerTree(['{http://sabredav.org/ns}elem1' => function(Reader $reader) { + '{http://sabredav.org/ns}elem1' => function (Reader $reader) { + $innerTree = $reader->parseInnerTree(['{http://sabredav.org/ns}elem1' => function (Reader $reader) { $reader->next(); - return "foobar"; + + return 'foobar'; }]); return $innerTree; - } + }, ]; $reader->xml($input); $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', + 'name' => '{http://sabredav.org/ns}elem1', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => 'foobar', + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => 'foobar', 'attributes' => [], - ] + ], ], 'attributes' => [], ], ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } /** * @depends testParseInnerTree */ - function testParseGetElements() { - + public function testParseGetElements() + { $input = << @@ -497,48 +474,46 @@ function testParseGetElements() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => function(Reader $reader) { - - $innerTree = $reader->parseGetElements(['{http://sabredav.org/ns}elem1' => function(Reader $reader) { + '{http://sabredav.org/ns}elem1' => function (Reader $reader) { + $innerTree = $reader->parseGetElements(['{http://sabredav.org/ns}elem1' => function (Reader $reader) { $reader->next(); - return "foobar"; + + return 'foobar'; }]); return $innerTree; - } + }, ]; $reader->xml($input); $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', + 'name' => '{http://sabredav.org/ns}elem1', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => 'foobar', + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => 'foobar', 'attributes' => [], - ] + ], ], 'attributes' => [], ], ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } /** * @depends testParseInnerTree */ - function testParseGetElementsNoElements() { - + public function testParseGetElementsNoElements() + { $input = << @@ -550,36 +525,32 @@ function testParseGetElementsNoElements() { $reader = new Reader(); $reader->elementMap = [ - '{http://sabredav.org/ns}elem1' => function(Reader $reader) { - - $innerTree = $reader->parseGetElements(['{http://sabredav.org/ns}elem1' => function(Reader $reader) { + '{http://sabredav.org/ns}elem1' => function (Reader $reader) { + $innerTree = $reader->parseGetElements(['{http://sabredav.org/ns}elem1' => function (Reader $reader) { $reader->next(); - return "foobar"; + + return 'foobar'; }]); return $innerTree; - } + }, ]; $reader->xml($input); $output = $reader->parse(); $expected = [ - 'name' => '{http://sabredav.org/ns}root', + 'name' => '{http://sabredav.org/ns}root', 'value' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => [], + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => [], 'attributes' => [], ], ], 'attributes' => [], - ]; $this->assertEquals($expected, $output); - } - - } diff --git a/tests/Sabre/Xml/Serializer/EnumTest.php b/tests/Sabre/Xml/Serializer/EnumTest.php index 10e71d3..3852495 100644 --- a/tests/Sabre/Xml/Serializer/EnumTest.php +++ b/tests/Sabre/Xml/Serializer/EnumTest.php @@ -1,17 +1,19 @@ -namespaceMap['urn:test'] = null; - $xml = $service->write('{urn:test}root', function($writer) { + $xml = $service->write('{urn:test}root', function ($writer) { enum($writer, [ '{urn:test}foo1', '{urn:test}foo2', @@ -26,11 +28,6 @@ enum($writer, [ XML; - $this->assertXmlStringEqualsXmlString($expected, $xml); - - } - - } diff --git a/tests/Sabre/Xml/Serializer/RepeatingElementsTest.php b/tests/Sabre/Xml/Serializer/RepeatingElementsTest.php index 153d349..6cf5495 100644 --- a/tests/Sabre/Xml/Serializer/RepeatingElementsTest.php +++ b/tests/Sabre/Xml/Serializer/RepeatingElementsTest.php @@ -1,16 +1,18 @@ -namespaceMap['urn:test'] = null; - $xml = $service->write('{urn:test}collection', function($writer) { + $xml = $service->write('{urn:test}collection', function ($writer) { repeatingElements($writer, [ 'foo', 'bar', @@ -25,11 +27,6 @@ function testSerialize() { XML; - $this->assertXmlStringEqualsXmlString($expected, $xml); - - } - - } diff --git a/tests/Sabre/Xml/ServiceTest.php b/tests/Sabre/Xml/ServiceTest.php index 641e916..fa83c35 100644 --- a/tests/Sabre/Xml/ServiceTest.php +++ b/tests/Sabre/Xml/ServiceTest.php @@ -1,13 +1,15 @@ - 'Test!', ]; @@ -18,11 +20,10 @@ function testGetReader() { $reader = $util->getReader(); $this->assertInstanceOf('Sabre\\Xml\\Reader', $reader); $this->assertEquals($elems, $reader->elementMap); - } - function testGetWriter() { - + public function testGetWriter() + { $ns = [ 'http://sabre.io/ns' => 's', ]; @@ -33,14 +34,13 @@ function testGetWriter() { $writer = $util->getWriter(); $this->assertInstanceOf('Sabre\\Xml\\Writer', $writer); $this->assertEquals($ns, $writer->namespaceMap); - } /** * @depends testGetReader */ - function testParse() { - + public function testParse() + { $xml = << value @@ -52,24 +52,23 @@ function testParse() { $expected = [ [ - 'name' => '{http://sabre.io/ns}child', - 'value' => 'value', + 'name' => '{http://sabre.io/ns}child', + 'value' => 'value', 'attributes' => [], - ] + ], ]; $this->assertEquals( $expected, $result ); - } /** * @depends testGetReader */ - function testParseStream() { - + public function testParseStream() + { $xml = << value @@ -85,24 +84,23 @@ function testParseStream() { $expected = [ [ - 'name' => '{http://sabre.io/ns}child', - 'value' => 'value', + 'name' => '{http://sabre.io/ns}child', + 'value' => 'value', 'attributes' => [], - ] + ], ]; $this->assertEquals( $expected, $result ); - } /** * @depends testGetReader */ - function testExpect() { - + public function testExpect() + { $xml = << value @@ -113,10 +111,10 @@ function testExpect() { $expected = [ [ - 'name' => '{http://sabre.io/ns}child', - 'value' => 'value', + 'name' => '{http://sabre.io/ns}child', + 'value' => 'value', 'attributes' => [], - ] + ], ]; $this->assertEquals( @@ -128,8 +126,8 @@ function testExpect() { /** * @expectedException \Sabre\Xml\LibXMLException */ - function testInvalidNameSpace() { - + public function testInvalidNameSpace() + { $xml = ''; $util = new Service(); @@ -145,7 +143,7 @@ function testInvalidNameSpace() { /** * @dataProvider providesEmptyPropfinds */ - function testEmptyPropfind($xml) + public function testEmptyPropfind($xml) { $util = new Service(); $util->elementMap = [ @@ -162,8 +160,8 @@ function testEmptyPropfind($xml) /** * @depends testGetReader */ - function testExpectStream() { - + public function testExpectStream() + { $xml = << value @@ -179,10 +177,10 @@ function testExpectStream() { $expected = [ [ - 'name' => '{http://sabre.io/ns}child', - 'value' => 'value', + 'name' => '{http://sabre.io/ns}child', + 'value' => 'value', 'attributes' => [], - ] + ], ]; $this->assertEquals( @@ -195,8 +193,8 @@ function testExpectStream() { * @depends testGetReader * @expectedException \Sabre\Xml\ParseException */ - function testExpectWrong() { - + public function testExpectWrong() + { $xml = << value @@ -204,14 +202,13 @@ function testExpectWrong() { XML; $util = new Service(); $util->expect('{http://sabre.io/ns}error', $xml); - } /** * @depends testGetWriter */ - function testWrite() { - + public function testWrite() + { $util = new Service(); $util->namespaceMap = [ 'http://sabre.io/ns' => 's', @@ -231,11 +228,10 @@ function testWrite() { $expected, $result ); - } - function testMapValueObject() { - + public function testMapValueObject() + { $input = << @@ -252,8 +248,8 @@ function testMapValueObject() { $ns = 'http://sabredav.org/ns'; $orderService = new \Sabre\Xml\Service(); - $orderService->mapValueObject('{' . $ns . '}order', 'Sabre\Xml\Order'); - $orderService->mapValueObject('{' . $ns . '}status', 'Sabre\Xml\OrderStatus'); + $orderService->mapValueObject('{'.$ns.'}order', 'Sabre\Xml\Order'); + $orderService->mapValueObject('{'.$ns.'}status', 'Sabre\Xml\OrderStatus'); $orderService->namespaceMap[$ns] = null; $order = $orderService->parse($input); @@ -271,8 +267,8 @@ function testMapValueObject() { $this->assertEquals($input, $writtenXml); } - function testMapValueObjectArrayProperty() { - + public function testMapValueObjectArrayProperty() + { $input = << @@ -291,8 +287,8 @@ function testMapValueObjectArrayProperty() { $ns = 'http://sabredav.org/ns'; $orderService = new \Sabre\Xml\Service(); - $orderService->mapValueObject('{' . $ns . '}order', 'Sabre\Xml\Order'); - $orderService->mapValueObject('{' . $ns . '}status', 'Sabre\Xml\OrderStatus'); + $orderService->mapValueObject('{'.$ns.'}order', 'Sabre\Xml\Order'); + $orderService->mapValueObject('{'.$ns.'}status', 'Sabre\Xml\OrderStatus'); $orderService->namespaceMap[$ns] = null; $order = $orderService->parse($input); @@ -314,32 +310,29 @@ function testMapValueObjectArrayProperty() { /** * @expectedException \InvalidArgumentException */ - function testWriteVoNotFound() { - + public function testWriteVoNotFound() + { $service = new Service(); $service->writeValueObject(new \StdClass()); - } - function testParseClarkNotation() { - + public function testParseClarkNotation() + { $this->assertEquals([ 'http://sabredav.org/ns', 'elem', ], Service::parseClarkNotation('{http://sabredav.org/ns}elem')); - } /** * @expectedException \InvalidArgumentException */ - function testParseClarkNotationFail() { - + public function testParseClarkNotationFail() + { Service::parseClarkNotation('http://sabredav.org/ns}elem'); - } - function providesEmptyPropfinds() + public function providesEmptyPropfinds() { return [ [''], @@ -352,10 +345,12 @@ function providesEmptyPropfinds() } /** - * asset for testMapValueObject() + * asset for testMapValueObject(). + * * @internal */ -class Order { +class Order +{ public $id; public $amount; public $description; @@ -365,48 +360,46 @@ class Order { } /** - * asset for testMapValueObject() + * asset for testMapValueObject(). + * * @internal */ -class OrderStatus { +class OrderStatus +{ public $id; public $label; } /** - * asset for testInvalidNameSpace + * asset for testInvalidNameSpace. + * * @internal */ -class PropFindTestAsset implements XmlDeserializable { - +class PropFindTestAsset implements XmlDeserializable +{ public $allProp = false; public $properties; - static function xmlDeserialize(Reader $reader) { - + public static function xmlDeserialize(Reader $reader) + { $self = new self(); $reader->pushContext(); $reader->elementMap['{DAV:}prop'] = 'Sabre\Xml\Element\Elements'; foreach (KeyValue::xmlDeserialize($reader) as $k => $v) { - switch ($k) { - case '{DAV:}prop' : + case '{DAV:}prop': $self->properties = $v; break; - case '{DAV:}allprop' : + case '{DAV:}allprop': $self->allProp = true; - } - } $reader->popContext(); return $self; - } - } diff --git a/tests/Sabre/Xml/WriterTest.php b/tests/Sabre/Xml/WriterTest.php index 651dd08..938e722 100644 --- a/tests/Sabre/Xml/WriterTest.php +++ b/tests/Sabre/Xml/WriterTest.php @@ -1,13 +1,15 @@ -writer = new Writer(); $this->writer->namespaceMap = [ 'http://sabredav.org/ns' => 's', @@ -15,19 +17,16 @@ function setUp() { $this->writer->openMemory(); $this->writer->setIndent(true); $this->writer->startDocument(); - } - function compare($input, $output) { - + public function compare($input, $output) + { $this->writer->write($input); $this->assertEquals($output, $this->writer->outputMemory()); - } - - function testSimple() { - + public function testSimple() + { $this->compare([ '{http://sabredav.org/ns}root' => 'text', ], <<compare([ '{http://sabredav.org/ns}root' => '"text"', ], <<compare([ '{http://sabredav.org/ns}root' => [ - 'value' => 'text', + 'value' => 'text', 'attributes' => [ 'attr1' => 'attribute value', ], @@ -70,32 +67,33 @@ function testSimpleAttributes() { HI ); - } - function testMixedSyntax() { + + public function testMixedSyntax() + { $this->compare([ '{http://sabredav.org/ns}root' => [ - '{http://sabredav.org/ns}single' => 'value', + '{http://sabredav.org/ns}single' => 'value', '{http://sabredav.org/ns}multiple' => [ [ - 'name' => '{http://sabredav.org/ns}foo', + 'name' => '{http://sabredav.org/ns}foo', 'value' => 'bar', ], [ - 'name' => '{http://sabredav.org/ns}foo', + 'name' => '{http://sabredav.org/ns}foo', 'value' => 'foobar', ], ], [ - 'name' => '{http://sabredav.org/ns}attributes', - 'value' => null, + 'name' => '{http://sabredav.org/ns}attributes', + 'value' => null, 'attributes' => [ 'foo' => 'bar', ], ], [ - 'name' => '{http://sabredav.org/ns}verbose', - 'value' => 'syntax', + 'name' => '{http://sabredav.org/ns}verbose', + 'value' => 'syntax', 'attributes' => [ 'foo' => 'bar', ], @@ -117,8 +115,8 @@ function testMixedSyntax() { ); } - function testNull() { - + public function testNull() + { $this->compare([ '{http://sabredav.org/ns}root' => null, ], <<compare([ '{http://sabredav.org/ns}root' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => 'text', + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => 'text', 'attributes' => [ 'attr1' => 'attribute value', ], @@ -150,15 +147,14 @@ function testArrayFormat2() { HI ); - } - function testArrayOfValues() { - + public function testArrayOfValues() + { $this->compare([ '{http://sabredav.org/ns}root' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', + 'name' => '{http://sabredav.org/ns}elem1', 'value' => [ 'foo', 'bar', @@ -174,18 +170,17 @@ function testArrayOfValues() { HI ); - } /** * @depends testArrayFormat2 */ - function testArrayFormat2NoValue() { - + public function testArrayFormat2NoValue() + { $this->compare([ '{http://sabredav.org/ns}root' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', + 'name' => '{http://sabredav.org/ns}elem1', 'attributes' => [ 'attr1' => 'attribute value', ], @@ -199,11 +194,10 @@ function testArrayFormat2NoValue() { HI ); - } - function testCustomNamespace() { - + public function testCustomNamespace() + { $this->compare([ '{http://sabredav.org/ns}root' => [ '{urn:foo}elem1' => 'bar', @@ -216,11 +210,10 @@ function testCustomNamespace() { HI ); - } - function testEmptyNamespace() { - + public function testEmptyNamespace() + { // Empty namespaces are allowed, so we should support this. $this->compare([ '{http://sabredav.org/ns}root' => [ @@ -234,20 +227,19 @@ function testEmptyNamespace() { HI ); - } - function testAttributes() { - + public function testAttributes() + { $this->compare([ '{http://sabredav.org/ns}root' => [ [ - 'name' => '{http://sabredav.org/ns}elem1', - 'value' => 'text', + 'name' => '{http://sabredav.org/ns}elem1', + 'value' => 'text', 'attributes' => [ - 'attr1' => 'val1', + 'attr1' => 'val1', '{http://sabredav.org/ns}attr2' => 'val2', - '{urn:foo}attr3' => 'val3', + '{urn:foo}attr3' => 'val3', ], ], ], @@ -259,26 +251,24 @@ function testAttributes() { HI ); - } - function testBaseElement() { - + public function testBaseElement() + { $this->compare([ - '{http://sabredav.org/ns}root' => new Element\Base('hello') + '{http://sabredav.org/ns}root' => new Element\Base('hello'), ], << hello HI ); - } - function testElementObj() { - + public function testElementObj() + { $this->compare([ - '{http://sabredav.org/ns}root' => new Element\Mock() + '{http://sabredav.org/ns}root' => new Element\Mock(), ], << @@ -287,14 +277,13 @@ function testElementObj() { HI ); - } - function testEmptyNamespacePrefix() { - + public function testEmptyNamespacePrefix() + { $this->writer->namespaceMap['http://sabredav.org/ns'] = null; $this->compare([ - '{http://sabredav.org/ns}root' => new Element\Mock() + '{http://sabredav.org/ns}root' => new Element\Mock(), ], << @@ -303,14 +292,13 @@ function testEmptyNamespacePrefix() { HI ); - } - function testEmptyNamespacePrefixEmptyString() { - + public function testEmptyNamespacePrefixEmptyString() + { $this->writer->namespaceMap['http://sabredav.org/ns'] = ''; $this->compare([ - '{http://sabredav.org/ns}root' => new Element\Mock() + '{http://sabredav.org/ns}root' => new Element\Mock(), ], << @@ -319,12 +307,11 @@ function testEmptyNamespacePrefixEmptyString() { HI ); - } - function testWriteElement() { - - $this->writer->writeElement("{http://sabredav.org/ns}foo", 'content'); + public function testWriteElement() + { + $this->writer->writeElement('{http://sabredav.org/ns}foo', 'content'); $output = << @@ -333,13 +320,11 @@ function testWriteElement() { HI; $this->assertEquals($output, $this->writer->outputMemory()); - - } - function testWriteElementComplex() { - - $this->writer->writeElement("{http://sabredav.org/ns}foo", new Element\KeyValue(['{http://sabredav.org/ns}bar' => 'test'])); + public function testWriteElementComplex() + { + $this->writer->writeElement('{http://sabredav.org/ns}foo', new Element\KeyValue(['{http://sabredav.org/ns}bar' => 'test'])); $output = << @@ -350,21 +335,19 @@ function testWriteElementComplex() { HI; $this->assertEquals($output, $this->writer->outputMemory()); - } /** * @expectedException \InvalidArgumentException */ - function testWriteBadObject() { - + public function testWriteBadObject() + { $this->writer->write(new \StdClass()); - } - function testStartElementSimple() { - - $this->writer->startElement("foo"); + public function testStartElementSimple() + { + $this->writer->startElement('foo'); $this->writer->endElement(); $output = <<assertEquals($output, $this->writer->outputMemory()); - } - function testCallback() { - + public function testCallback() + { $this->compare([ - '{http://sabredav.org/ns}root' => function(Writer $writer) { + '{http://sabredav.org/ns}root' => function (Writer $writer) { $writer->text('deferred writer'); }, ], <<compare([ '{http://sabredav.org/ns}root' => fopen('php://memory', 'r'), ], << 'value1', 'key2' => 'value2', ]; - $this->writer->classMap['stdClass'] = function(Writer $writer, $value) { - + $this->writer->classMap['stdClass'] = function (Writer $writer, $value) { foreach (get_object_vars($value) as $key => $val) { - $writer->writeElement('{http://sabredav.org/ns}' . $key, $val); + $writer->writeElement('{http://sabredav.org/ns}'.$key, $val); } - }; $this->compare([ - '{http://sabredav.org/ns}root' => $obj + '{http://sabredav.org/ns}root' => $obj, ], << @@ -434,6 +412,5 @@ function testClassMap() { HI ); - } }