Skip to content

Commit df2856e

Browse files
committed
Tests for example implementation of CollectionInterface and AbstractCollection.
1 parent a74a335 commit df2856e

File tree

2 files changed

+325
-0
lines changed

2 files changed

+325
-0
lines changed

tests/CollectionTest.php

+318
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
<?php
2+
3+
namespace TwoDotsTwice\Collection;
4+
5+
use TwoDotsTwice\Collection\Exception\CollectionItemNotFoundException;
6+
use TwoDotsTwice\Collection\Exception\CollectionKeyNotFoundException;
7+
use TwoDotsTwice\Collection\Mock\Foo;
8+
use TwoDotsTwice\Collection\Mock\FooCollection;
9+
use TwoDotsTwice\Collection\Mock\FooExtended;
10+
11+
class CollectionTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var Foo
15+
*/
16+
protected $foo1;
17+
18+
/**
19+
* @var Foo
20+
*/
21+
protected $foo2;
22+
23+
/**
24+
* @var FooExtended
25+
*/
26+
protected $fooExtended;
27+
28+
/**
29+
* @var \stdClass
30+
*/
31+
protected $notFoo;
32+
33+
public function setUp()
34+
{
35+
$this->foo1 = new Foo(1, 'Foo 1');
36+
$this->foo2 = new Foo(2, 'Foo 2');
37+
38+
$this->fooExtended = new FooExtended(3, 'Foo 3 (extended)');
39+
40+
$this->notFoo = new \stdClass();
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function it_can_create_a_copy_with_a_new_item_for_a_specific_key()
47+
{
48+
$collection = (new FooCollection())
49+
->with($this->foo1, 'foo1')
50+
->with($this->foo2, 'foo2');
51+
52+
$expected = [
53+
'foo1' => $this->foo1,
54+
'foo2' => $this->foo2,
55+
];
56+
57+
$this->assertEquals($expected, $collection->toArray());
58+
}
59+
60+
/**
61+
* @test
62+
*/
63+
public function it_can_assign_a_key_automatically_and_return_it_when_necessary()
64+
{
65+
$collection = (new FooCollection())
66+
->with($this->foo1)
67+
->with($this->foo2);
68+
69+
$expectedArray = [
70+
0 => $this->foo1,
71+
1 => $this->foo2,
72+
];
73+
74+
$this->assertEquals($expectedArray, $collection->toArray());
75+
76+
$this->assertEquals(0, $collection->getKeyFor($this->foo1));
77+
$this->assertEquals(1, $collection->getKeyFor($this->foo2));
78+
}
79+
80+
/**
81+
* @test
82+
*/
83+
public function it_guards_the_object_type_of_new_items()
84+
{
85+
$this->setExpectedException(\InvalidArgumentException::class);
86+
87+
(new FooCollection())
88+
->with($this->notFoo);
89+
}
90+
91+
/**
92+
* @test
93+
*/
94+
public function it_guards_the_object_type_of_items_when_initializing_from_array()
95+
{
96+
$this->setExpectedException(\InvalidArgumentException::class);
97+
98+
FooCollection::fromArray(
99+
[
100+
$this->foo1,
101+
$this->notFoo,
102+
$this->foo2,
103+
]
104+
);
105+
}
106+
107+
/**
108+
* @test
109+
*/
110+
public function it_allows_sub_classes_when_guarding_the_object_type_of_new_items()
111+
{
112+
$collection = (new FooCollection())
113+
->with($this->fooExtended, 'fooExtended');
114+
115+
$expected = [
116+
'fooExtended' => $this->fooExtended,
117+
];
118+
119+
$this->assertEquals($expected, $collection->toArray());
120+
}
121+
122+
/**
123+
* @test
124+
*/
125+
public function it_can_remove_an_item()
126+
{
127+
$collection = (new FooCollection())
128+
->with($this->foo1)
129+
->with($this->foo2);
130+
131+
$collection = $collection->without($this->foo1);
132+
133+
$expected = [
134+
1 => $this->foo2,
135+
];
136+
137+
$this->assertEquals($expected, $collection->toArray());
138+
}
139+
140+
/**
141+
* @test
142+
*/
143+
public function it_throws_an_exception_when_removing_an_unknown_item()
144+
{
145+
$collection = (new FooCollection())
146+
->with($this->foo1, 'foo1');
147+
148+
$this->setExpectedException(CollectionItemNotFoundException::class);
149+
$collection->without($this->foo2);
150+
}
151+
152+
/**
153+
* @test
154+
*/
155+
public function it_can_remove_an_item_by_key()
156+
{
157+
$collection = (new FooCollection())
158+
->with($this->foo1, 'foo1')
159+
->with($this->foo2, 'foo2');
160+
161+
$collection = $collection->withoutKey('foo1');
162+
163+
$expected = [
164+
'foo2' => $this->foo2,
165+
];
166+
167+
$this->assertEquals($expected, $collection->toArray());
168+
}
169+
170+
/**
171+
* @test
172+
*/
173+
public function it_throws_an_exception_when_removing_by_unknown_key()
174+
{
175+
$collection = (new FooCollection())
176+
->with($this->foo1, 'foo1')
177+
->with($this->foo2, 'foo2');
178+
179+
$this->setExpectedException(CollectionKeyNotFoundException::class);
180+
$collection->withoutKey('foo3');
181+
}
182+
183+
/**
184+
* @test
185+
*/
186+
public function it_can_return_an_item_by_key()
187+
{
188+
$collection = (new FooCollection())
189+
->with($this->foo1, 'foo1')
190+
->with($this->foo2, 'foo2');
191+
192+
$this->assertEquals(
193+
$this->foo1,
194+
$collection->getByKey('foo1')
195+
);
196+
197+
$this->assertEquals(
198+
$this->foo2,
199+
$collection->getByKey('foo2')
200+
);
201+
}
202+
203+
/**
204+
* @test
205+
*/
206+
public function it_throws_an_exception_when_looking_for_an_item_with_an_unknown_key()
207+
{
208+
$collection = (new FooCollection())
209+
->with($this->foo1, 'foo1');
210+
211+
$this->setExpectedException(CollectionKeyNotFoundException::class);
212+
$collection->getByKey('foo2');
213+
}
214+
215+
/**
216+
* @test
217+
*/
218+
public function it_can_return_the_key_for_an_item()
219+
{
220+
$collection = (new FooCollection())
221+
->with($this->foo1, 'foo1')
222+
->with($this->foo2, 'foo2');
223+
224+
$this->assertEquals(
225+
'foo1',
226+
$collection->getKeyFor($this->foo1)
227+
);
228+
229+
$this->assertEquals(
230+
'foo2',
231+
$collection->getKeyFor($this->foo2)
232+
);
233+
}
234+
235+
/**
236+
* @test
237+
*/
238+
public function it_throws_an_exception_when_looking_for_the_key_of_an_unknown_item()
239+
{
240+
$collection = (new FooCollection())
241+
->with($this->foo1, 'foo1');
242+
243+
$this->setExpectedException(CollectionItemNotFoundException::class);
244+
$collection->getKeyFor($this->foo2);
245+
}
246+
247+
/**
248+
* @test
249+
*/
250+
public function it_can_return_a_list_of_keys()
251+
{
252+
$collection = (new FooCollection())
253+
->with($this->foo1, 'foo1')
254+
->with($this->foo2, 'foo2');
255+
256+
$expected = [
257+
'foo1',
258+
'foo2',
259+
];
260+
261+
$this->assertEquals($expected, $collection->getKeys());
262+
}
263+
264+
/**
265+
* @test
266+
*/
267+
public function it_can_return_the_number_of_items()
268+
{
269+
$collection = new FooCollection();
270+
$this->assertEquals(0, $collection->length());
271+
272+
/* @var FooCollection $collection */
273+
$collection = $collection->with($this->foo1);
274+
$collection = $collection->with($this->foo2);
275+
276+
$this->assertEquals(2, $collection->length());
277+
}
278+
279+
/**
280+
* @test
281+
*/
282+
public function it_can_be_converted_from_and_to_an_array()
283+
{
284+
$original = [
285+
$this->foo1,
286+
$this->foo2,
287+
];
288+
289+
$collection = FooCollection::fromArray($original);
290+
291+
$this->assertEquals($original, $collection->toArray());
292+
}
293+
294+
/**
295+
* @test
296+
*/
297+
public function it_can_be_looped_over_like_an_array()
298+
{
299+
$collection = (new FooCollection())
300+
->with($this->foo1, 'foo1')
301+
->with($this->foo2, 'foo2')
302+
->with($this->fooExtended, 'fooExtended');
303+
304+
$expected = [
305+
'foo1' => $this->foo1,
306+
'foo2' => $this->foo2,
307+
'fooExtended' => $this->fooExtended,
308+
];
309+
310+
$actual = [];
311+
312+
foreach ($collection as $key => $item) {
313+
$actual[$key] = $item;
314+
}
315+
316+
$this->assertEquals($expected, $actual);
317+
}
318+
}

tests/Mock/FooExtended.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace TwoDotsTwice\Collection\Mock;
4+
5+
class FooExtended extends Foo
6+
{
7+
}

0 commit comments

Comments
 (0)