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 Graze\DataNode\NodeCollection; |
17
|
|
|
use Graze\DataNode\NodeInterface; |
18
|
|
|
use Graze\DataNode\Test\AbstractTestCase; |
19
|
|
|
use Graze\DataStructure\Collection\Collection; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
use Mockery as m; |
22
|
|
|
|
23
|
|
|
class NodeCollectionTest extends AbstractTestCase |
24
|
|
|
{ |
25
|
|
|
public function testIsCollection() |
26
|
|
|
{ |
27
|
|
|
static::assertInstanceOf(Collection::class, new NodeCollection()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testCanAddADataNode() |
31
|
|
|
{ |
32
|
|
|
$collection = new NodeCollection(); |
33
|
|
|
$node = m::mock(NodeInterface::class); |
34
|
|
|
static::assertSame($collection, $collection->add($node)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testAddingANonDataNodeWillThrowAnException() |
38
|
|
|
{ |
39
|
|
|
$node = m::mock('Graze\Extensible\ExtensibleInterface'); |
40
|
|
|
|
41
|
|
|
$this->expectException(InvalidArgumentException::class); |
42
|
|
|
|
43
|
|
|
$collection = new NodeCollection(); |
44
|
|
|
$collection->add($node); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testCallingApplyWillModifyTheContentsUsingReference() |
48
|
|
|
{ |
49
|
|
|
$node = m::mock(NodeInterface::class); |
50
|
|
|
$node->shouldReceive('someMethod') |
|
|
|
|
51
|
|
|
->once() |
52
|
|
|
->andReturn(null); |
53
|
|
|
|
54
|
|
|
$collection = new NodeCollection(); |
55
|
|
|
$collection->add($node); |
56
|
|
|
|
57
|
|
|
$collection->apply(function ($item) { |
58
|
|
|
$item->someMethod(); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$item = $collection->getAll()[0]; |
62
|
|
|
static::assertSame($node, $item); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testCallingApplyWillModifyTheContentsUsingReturnValue() |
66
|
|
|
{ |
67
|
|
|
$node = m::mock(NodeInterface::class); |
68
|
|
|
$node->shouldReceive('someMethod') |
|
|
|
|
69
|
|
|
->once() |
70
|
|
|
->andReturn(null); |
71
|
|
|
|
72
|
|
|
$collection = new NodeCollection(); |
73
|
|
|
$collection->add($node); |
74
|
|
|
|
75
|
|
|
$collection->apply(function ($item) { |
76
|
|
|
$item->someMethod(); |
77
|
|
|
return $item; |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
$item = $collection->getAll()[0]; |
81
|
|
|
static::assertSame($node, $item); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testFirstWithNoCallbackWillReturnTheFirstEntry() |
85
|
|
|
{ |
86
|
|
|
$first = m::mock(NodeInterface::class); |
87
|
|
|
$second = m::mock(NodeInterface::class); |
88
|
|
|
|
89
|
|
|
$collection = new NodeCollection([$first, $second]); |
90
|
|
|
|
91
|
|
|
static::assertSame($first, $collection->first()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testLastWithNoCallbackWillReturnTheFirstEntry() |
95
|
|
|
{ |
96
|
|
|
$first = m::mock(NodeInterface::class); |
97
|
|
|
$second = m::mock(NodeInterface::class); |
98
|
|
|
|
99
|
|
|
$collection = new NodeCollection([$first, $second]); |
100
|
|
|
|
101
|
|
|
static::assertSame($second, $collection->last()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testFirstWithCallbackWillReturnTheFirstThatMatches() |
105
|
|
|
{ |
106
|
|
|
$first = m::mock(NodeInterface::class); |
107
|
|
|
$second = m::mock(NodeInterface::class); |
108
|
|
|
$third = m::mock(NodeInterface::class); |
109
|
|
|
|
110
|
|
|
$first->shouldReceive('thisOne') |
|
|
|
|
111
|
|
|
->andReturn(false); |
112
|
|
|
$second->shouldReceive('thisOne') |
113
|
|
|
->andReturn(true); |
114
|
|
|
$third->shouldReceive('thosOne') |
115
|
|
|
->andReturn(true); |
116
|
|
|
|
117
|
|
|
$collection = new NodeCollection([$first, $second, $third]); |
118
|
|
|
|
119
|
|
|
static::assertSame($second, $collection->first(function ($item) { |
120
|
|
|
return $item->thisOne(); |
121
|
|
|
})); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testLastWithCallbackWillReturnTheFirstThatMatches() |
125
|
|
|
{ |
126
|
|
|
$first = m::mock(NodeInterface::class); |
127
|
|
|
$second = m::mock(NodeInterface::class); |
128
|
|
|
$third = m::mock(NodeInterface::class); |
129
|
|
|
|
130
|
|
|
$first->shouldReceive('thisOne') |
|
|
|
|
131
|
|
|
->andReturn(true); |
132
|
|
|
$second->shouldReceive('thisOne') |
133
|
|
|
->andReturn(true); |
134
|
|
|
$third->shouldReceive('thisOne') |
135
|
|
|
->andReturn(false); |
136
|
|
|
|
137
|
|
|
$collection = new NodeCollection([$first, $second, $third]); |
138
|
|
|
|
139
|
|
|
static::assertSame($second, $collection->last(function ($item) { |
140
|
|
|
return $item->thisOne(); |
141
|
|
|
})); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testFirstWithCallbackWillReturnDefaultIfNoMatchesAreFound() |
145
|
|
|
{ |
146
|
|
|
$first = m::mock(NodeInterface::class); |
147
|
|
|
$second = m::mock(NodeInterface::class); |
148
|
|
|
$default = m::mock(NodeInterface::class); |
149
|
|
|
|
150
|
|
|
$first->shouldReceive('thisOne') |
|
|
|
|
151
|
|
|
->andReturn(false); |
152
|
|
|
$second->shouldReceive('thisOne') |
153
|
|
|
->andReturn(false); |
154
|
|
|
|
155
|
|
|
$collection = new NodeCollection([$first, $second]); |
156
|
|
|
|
157
|
|
|
static::assertSame($default, $collection->first(function ($item) { |
158
|
|
|
return $item->thisOne(); |
159
|
|
|
}, $default)); |
160
|
|
|
static::assertNull($collection->first(function ($item) { |
161
|
|
|
return $item->thisOne(); |
162
|
|
|
})); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testLastWithCallbackWillReturnDefaultIfNoMatchesAreFound() |
166
|
|
|
{ |
167
|
|
|
$first = m::mock(NodeInterface::class); |
168
|
|
|
$second = m::mock(NodeInterface::class); |
169
|
|
|
$default = m::mock(NodeInterface::class); |
170
|
|
|
|
171
|
|
|
$first->shouldReceive('thisOne') |
|
|
|
|
172
|
|
|
->andReturn(false); |
173
|
|
|
$second->shouldReceive('thisOne') |
174
|
|
|
->andReturn(false); |
175
|
|
|
|
176
|
|
|
$collection = new NodeCollection([$first, $second]); |
177
|
|
|
|
178
|
|
|
static::assertSame($default, $collection->last(function ($item) { |
179
|
|
|
return $item->thisOne(); |
180
|
|
|
}, $default)); |
181
|
|
|
static::assertNull($collection->last(function ($item) { |
182
|
|
|
return $item->thisOne(); |
183
|
|
|
})); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testCloneWillCloneTheChildObjects() |
187
|
|
|
{ |
188
|
|
|
$first = m::mock(NodeInterface::class); |
189
|
|
|
$second = m::mock(NodeInterface::class); |
190
|
|
|
|
191
|
|
|
$collection = new NodeCollection([$first, $second]); |
192
|
|
|
$collection2 = $collection->getClone(); |
193
|
|
|
|
194
|
|
|
static::assertNotSame($collection, $collection2); |
195
|
|
|
static::assertEquals($collection->count(), $collection2->count()); |
196
|
|
|
for ($i = 0; $i < $collection->count(); $i++) { |
197
|
|
|
static::assertNotSame($collection->getAll()[$i], $collection2->getAll()[$i]); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testToString() |
202
|
|
|
{ |
203
|
|
|
$collection = new NodeCollection(); |
204
|
|
|
static::assertEquals("NodeCollection", "$collection"); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.