/** * @test */ function iteration() { $reader = new XMLReaderStub('<!-- comment --><root><child></child></root>'); $it = new XMLChildElementIterator($reader); $this->assertEquals(false, $it->valid()); $this->assertSame(null, $it->valid()); $it->rewind(); $this->assertEquals(true, $it->valid()); $this->assertEquals('child', $it->current()->getName()); $it->next(); $this->assertEquals(false, $it->valid()); $reader = new XMLReaderStub('<root><none></none><one><child></child></one><none></none></root>'); $base = new XMLElementIterator($reader); $base->rewind(); $root = $base->current(); $this->assertEquals('root', $root->getName()); $children = $root->getChildElements(); $this->assertEquals('root', $reader->name); $children->rewind(); $this->assertEquals('none', $reader->name); $children->next(); $this->assertEquals('one', $reader->name); $childChildren = new XMLChildElementIterator($reader); $this->assertEquals('child', $childChildren->current()->getName()); $childChildren->next(); $this->assertEquals(false, $childChildren->valid()); $this->assertEquals('none', $reader->name); $childChildren->next(); $this->assertEquals('none', $reader->name); $this->assertEquals(true, $children->valid()); $children->next(); $this->assertEquals(false, $children->valid()); // children w/o descendants $reader->rewind(); $expected = array('none', 'one', 'none'); $root = $base->current(); $this->assertEquals('root', $root->getName()); $index = 0; $count = 0; foreach ($root->getChildElements() as $index => $child) { $this->assertSame($count++, $index); $this->assertEquals($expected[$index], $reader->name); } $this->assertEquals(count($expected), $count); // children w/ descendants $reader->rewind(); $expected = array('none', 'one', 'child', 'none'); $root = $base->current(); $this->assertEquals('root', $root->getName()); $index = 0; $count = 0; foreach ($root->getChildElements(null, true) as $index => $child) { $this->assertSame($count++, $index); $this->assertEquals($expected[$index], $reader->name); } $this->assertEquals(count($expected), $count); }
/** @test */ public function readerAttributeHandling() { $reader = new XMLReaderStub("<root pos=\"first\" plue=\"a b  c\t	d\">node value</root>"); $reader->next(); $this->assertSame("first", $reader->getAttribute('pos')); $this->assertSame("a\r\nb c \td", $reader->getAttribute('plue'), 'entity handling'); $element = new XMLReaderElement($reader); $xml = $element->getXMLElementAround(); $this->assertSame("<root pos=\"first\" plue=\"a b c 	d\"/>", $xml, 'XML generation'); }
/** * @test * @dataProvider provideAllFiles * * @param string $xml */ function nextBehavior($xml) { $reader = new XMLReaderStub($xml); $it = new XMLReaderNextIteration($reader); $expected = array(); while ($reader->next()) { $expected[] = XMLReaderNode::dump($reader, true); } $reader->rewind(); $index = 0; foreach ($it as $index => $node) { $this->assertEquals($expected[$index], XMLReaderNode::dump($reader, true)); } $this->assertCount($index + 1, $expected); }
/** * some XMLReaderNode can not be turned into a SimpleXMLElement, this tests how robust XMLReaderNode * is for the job. * * @test */ function asSimpleXMLforElementAndSignificantWhitespace() { $reader = new XMLReaderStub('<root> <!-- <3 <3 love XMLReader::SIGNIFICANT_WHITESPACE (14) <3 <3 --> </root>'); $reader->read(); // (#1) <root> // test asSimpleXML() for XMLReader::ELEMENT $this->assertSame(XMLReader::ELEMENT, $reader->nodeType); $node = new XMLReaderNode($reader); $sxml = $node->getSimpleXMLElement(); $this->assertInstanceOf('SimpleXMLElement', $sxml); $reader->read(); // (#14) SIGNIFICANT_WHITESPACE // test asSimpleXML() for XMLReader::SIGNIFICANT_WHITESPACE $this->assertSame(XMLReader::SIGNIFICANT_WHITESPACE, $reader->nodeType); $node = new XMLReaderNode($reader); $sxml = $node->getSimpleXMLElement(); $this->assertNull($sxml); }
/** * @test */ function expand() { $reader = new XMLReaderStub('<products> <!--suppress HtmlUnknownAttribute --> <product category="Desktop"> <name> Desktop 1 (d)</name> <price>499.99</price> </product> <!--suppress HtmlUnknownAttribute --> <product category="Tablet"> <name>Tablet 1 (t)</name> <price>1099.99</price> </product> </products>'); $products = new XMLElementIterator($reader, 'product'); $doc = new DOMDocument(); $xpath = new DOMXpath($doc); foreach ($products as $product) { $node = $product->expand($doc); $this->assertInstanceOf('DOMNode', $node); $this->assertSame($node->ownerDocument, $doc); $this->assertEquals('product', $xpath->evaluate('local-name(.)', $node)); $this->addToAssertionCount(1); } $this->assertGreaterThan(0, $previous = $this->getNumAssertions()); unset($doc); $reader->rewind(); foreach ($products as $product) { $node = $product->expand(); $this->assertInstanceOf('DOMNode', $node); $this->assertInstanceOf('DOMDocument', $node->ownerDocument); $doc = $node->ownerDocument; $xpath = new DOMXpath($doc); $this->assertSame($node->ownerDocument, $doc); $this->assertEquals('product', $xpath->evaluate('local-name(.)', $node)); $this->addToAssertionCount(1); } $this->assertGreaterThan($previous, $this->getNumAssertions()); }