/** * @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 */ 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()); }