|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/data-node |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/data-node/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/data-node |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DataNode\Test\Unit; |
|
15
|
|
|
|
|
16
|
|
|
use ArrayIterator; |
|
17
|
|
|
use Graze\DataNode\IteratorNode; |
|
18
|
|
|
use Graze\DataNode\IteratorNodeInterface; |
|
19
|
|
|
use Graze\DataNode\Test\AbstractTestCase; |
|
20
|
|
|
use Iterator; |
|
21
|
|
|
use Traversable; |
|
22
|
|
|
|
|
23
|
|
|
class IteratorNodeText extends AbstractTestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testImplements() |
|
26
|
|
|
{ |
|
27
|
|
|
$node = new IteratorNode([]); |
|
28
|
|
|
static::assertInstanceOf(Iterator::class, $node); |
|
29
|
|
|
static::assertInstanceOf(IteratorNodeInterface::class, $node); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testInstantiateWithArray() |
|
33
|
|
|
{ |
|
34
|
|
|
$node = new IteratorNode(['first', 'second']); |
|
35
|
|
|
static::assertEquals(['first', 'second'], iterator_to_array($node)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testInstantiateWithArrayAndKeys() |
|
39
|
|
|
{ |
|
40
|
|
|
$node = new IteratorNode(['first' => 'value1', 'second' => 'value2']); |
|
41
|
|
|
static::assertEquals(['first' => 'value1', 'second' => 'value2'], iterator_to_array($node, true)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testInstantiateWithIterator() |
|
45
|
|
|
{ |
|
46
|
|
|
$node = new IteratorNode(new ArrayIterator(['first', 'second'])); |
|
47
|
|
|
static::assertEquals(['first', 'second'], iterator_to_array($node)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testFetchWillReturnAnIterator() |
|
51
|
|
|
{ |
|
52
|
|
|
$node = new IteratorNode(['first', 'second']); |
|
53
|
|
|
$iterator = $node->fetch(); |
|
54
|
|
|
static::assertInstanceOf(Iterator::class, $iterator); |
|
55
|
|
|
static::assertInstanceOf(Traversable::class, $iterator); |
|
56
|
|
|
static::assertEquals(['first', 'second'], iterator_to_array($iterator)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testFetchWillFilterOnCallable() |
|
60
|
|
|
{ |
|
61
|
|
|
$node = new IteratorNode(['first', 'second']); |
|
62
|
|
|
$iterator = $node->fetch(function ($value) { |
|
63
|
|
|
return $value == 'first'; |
|
64
|
|
|
}); |
|
65
|
|
|
static::assertEquals(['first'], iterator_to_array($iterator)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testClone() |
|
69
|
|
|
{ |
|
70
|
|
|
$iterator = new ArrayIterator(['first', 'second']); |
|
71
|
|
|
$node = new IteratorNode($iterator); |
|
72
|
|
|
$clone = $node->getClone(); |
|
73
|
|
|
|
|
74
|
|
|
static::assertNotSame($clone, $node); |
|
75
|
|
|
|
|
76
|
|
|
$iterator->append('third'); |
|
77
|
|
|
|
|
78
|
|
|
static::assertEquals(['first', 'second', 'third'], iterator_to_array($node)); |
|
79
|
|
|
static::assertEquals(['first', 'second'], iterator_to_array($clone)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testToString() |
|
83
|
|
|
{ |
|
84
|
|
|
$node = new IteratorNode([]); |
|
85
|
|
|
static::assertEquals(IteratorNode::class, $node->__toString()); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|