Skip to content

Commit 4d9e2ee

Browse files
author
Julian Wundrak
committed
Provide example and tests
1 parent 6d5d613 commit 4d9e2ee

4 files changed

Lines changed: 114 additions & 1 deletion

File tree

examples/child-nested-read.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require('xmlreader-iterators.php'); // require XMLReaderIterator library
4+
5+
$xmlFile = 'data/products.xml';
6+
7+
/** @var \Traversable<int,\XMLReaderNode> $list */
8+
$iterator = new XMLElementIterator(XMLReader::open($xmlFile));
9+
$list = new XMLElementXpathFilter($iterator, '//product');
10+
11+
foreach ($list as $item) {
12+
printf('Found product "%s"' . \PHP_EOL, $item->getAttribute('sku'));
13+
14+
foreach ($item->getChildElements('attributes') as $attributeList) {
15+
foreach ($attributeList->getChildElements() as $attribute) {
16+
printf(' - %s: %s' . \PHP_EOL, $attribute->name, (string)$attribute);
17+
}
18+
}
19+
}

examples/data/products.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<catalog>
3+
<product sku="empty_product"/>
4+
<product sku="empty_attributes">
5+
<attributes/>
6+
</product>
7+
<product sku="no_attributes">
8+
</product>
9+
<product sku="foo">
10+
<attributes>
11+
<name>Test product</name>
12+
</attributes>
13+
<variants>
14+
<product sku="foo_blue">
15+
<attributes>
16+
<name>Test product in blue</name>
17+
<price>15.00</price>
18+
</attributes>
19+
</product>
20+
<product sku="foo_red">
21+
<attributes>
22+
<name>Test product in red</name>
23+
<price>12.00</price>
24+
</attributes>
25+
</product>
26+
</variants>
27+
</product>
28+
<product sku="bar">
29+
<attributes>
30+
<name>Simple test product</name>
31+
<price>10.99</price>
32+
</attributes>
33+
</product>
34+
<product sku="foobar">
35+
<attributes>
36+
<name>Another product</name>
37+
<price>99.99</price>
38+
</attributes>
39+
<variants>
40+
<product sku="foobar_l">
41+
<attributes>
42+
<size>L</size>
43+
</attributes>
44+
</product>
45+
<product sku="foobar_m">
46+
<attributes>
47+
<size>M</size>
48+
</attributes>
49+
</product>
50+
</variants>
51+
</product>
52+
</catalog>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Found product "empty_product"
2+
Found product "empty_attributes"
3+
Found product "no_attributes"
4+
Found product "foo"
5+
- name: Test product
6+
Found product "foo_blue"
7+
- name: Test product in blue
8+
- price: 15.00
9+
Found product "foo_red"
10+
- name: Test product in red
11+
- price: 12.00
12+
Found product "bar"
13+
- name: Simple test product
14+
- price: 10.99
15+
Found product "foobar"
16+
- name: Another product
17+
- price: 99.99
18+
Found product "foobar_l"
19+
- size: L
20+
Found product "foobar_m"
21+
- size: M

tests/unit/XMLChildElementIteratorTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ public function testIteration()
9191
$this->assertEquals($expected[$index], $reader->name);
9292
}
9393
$this->assertEquals(count($expected), $count);
94-
9594
}
9695

9796
/**
@@ -142,4 +141,26 @@ public function testDescendantChildren()
142141
$array = iterator_to_array($children, false);
143142
$this->assertSame(array(), $array, 'all children have been consumed by foreach');
144143
}
144+
145+
/**
146+
* Test child-iterator without calling 'skipNextRead' method
147+
*/
148+
public function testIterationOnChildrenStopBeforeReadingNextElement()
149+
{
150+
$reader = XMLReader::open(__DIR__ . '/../../examples/data/sample-rss-091.xml');
151+
$this->assertTrue(!!$reader, 'fixture document can be opened successfully');
152+
$items = new XMLElementIterator($reader, 'item');
153+
foreach ($items as $idx => $item) {
154+
$children = $items->getChildElements();
155+
$children->rewind();
156+
foreach (['title', 'link', 'description'] as $tagName) {
157+
$this->assertTrue($children->valid());
158+
$this->assertSame($tagName, $children->name);
159+
$children->next();
160+
}
161+
$this->assertFalse($children->valid());
162+
}
163+
$this->assertSame(6, $idx, 'sample-rss-091.xml element has 7 item elements');
164+
$this->assertEmpty(\iterator_to_array($items), 'all children have been consumed by foreach');
165+
}
145166
}

0 commit comments

Comments
 (0)