$actual = $r[0]->nodeName; $expected = 'g'; \assert($actual === $expected, __($actual, $expected)); }); }); describe('.each', function () { it('should be fluid', function () { assert_is_fluid('each', function () { }); }); it('should iterate the nodes inside the context', function () { $xml = new FluidXml(); $xml->each(function ($i, $n) { assert_is_a($this, FluidContext::class); assert_is_a($n, \DOMNode::class); $actual = $i; $expected = 0; \assert($actual === $expected, __($actual, $expected)); }); function eachassert($cx, $i, $n) { assert_is_a($cx, FluidContext::class); assert_is_a($n, \DOMNode::class); $actual = $i; $expected = 0; \assert($actual === $expected, __($actual, $expected)); } $xml->each('eachassert'); $xml->appendChild('child1')->appendChild('child2'); $nodes = []; $index = 0;
$xml->filter(function ($i, $n) { assert_is_a($this, FluidContext::class); assert_is_a($n, \DOMNode::class); $actual = $i; $expected = 0; \assert($actual === $expected, __($actual, $expected)); }); function filterassert($cx, $i, $n) { assert_is_a($cx, FluidContext::class); assert_is_a($n, \DOMNode::class); $actual = $i; $expected = 0; \assert($actual === $expected, __($actual, $expected)); } $xml->each('filterassert'); $xml->times(4)->addChild('child'); $index = 0; $children = $xml->query('//child'); $cx = $children->filter(function ($i, $n) use(&$index) { $actual = $i; $expected = $index; \assert($actual === $expected, __($actual, $expected)); ++$index; if ($i === 0) { return true; } if ($i % 2 === 0) { return false; } });