1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StraTDeS\VO\Tests\Collection; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use StraTDeS\VO\Collection\StringCollection; |
8
|
|
|
use TypeError; |
9
|
|
|
|
10
|
|
|
class StringCollectionTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testGivenValidNamesAValidNameCollectionIsReturned(): void |
13
|
|
|
{ |
14
|
|
|
// Arrange |
15
|
|
|
$item1 = 'a'; |
16
|
|
|
$item2 = 'b'; |
17
|
|
|
$item3 = 'c'; |
18
|
|
|
$array = [$item1, $item2, $item3]; |
19
|
|
|
|
20
|
|
|
// Act |
21
|
|
|
$collection = StringCollection::create(); |
22
|
|
|
$collection->add($item1); |
23
|
|
|
$collection->add($item2); |
24
|
|
|
$collection->add($item3); |
25
|
|
|
|
26
|
|
|
// Assert |
27
|
|
|
$this->assertInstanceOf(StringCollection::class, $collection); |
28
|
|
|
$this->assertEquals($item1, $collection[0]); |
29
|
|
|
$this->assertEquals($item2, $collection[1]); |
30
|
|
|
$this->assertEquals($item3, $collection[2]); |
31
|
|
|
$this->assertEquals( |
32
|
|
|
$array, |
33
|
|
|
$collection->items() |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$this->assertCount(3, $collection); |
37
|
|
|
|
38
|
|
|
for ($i = 0; $i < 3; $i++) { |
39
|
|
|
$this->assertEquals($array[$i], $collection[$i]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
foreach ($collection as $key => $value) { |
43
|
|
|
$this->assertTrue(is_string($value)); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGivenAValueByOffsetReturnsAValidValue(): void |
48
|
|
|
{ |
49
|
|
|
// Arrange |
50
|
|
|
$item1 = 'a'; |
51
|
|
|
|
52
|
|
|
// Act |
53
|
|
|
$collection = StringCollection::create(); |
54
|
|
|
$collection[0] = $item1; |
55
|
|
|
|
56
|
|
|
// Assert |
57
|
|
|
$this->assertEquals($item1, $collection[0]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testGivenAnInvalidValueItThrowsAnException(): void |
61
|
|
|
{ |
62
|
|
|
$this->expectException(TypeError::class); |
63
|
|
|
$this->expectExceptionMessage('Provided value has to be string'); |
64
|
|
|
|
65
|
|
|
$item = 1; |
66
|
|
|
|
67
|
|
|
$collection = StringCollection::create(); |
68
|
|
|
$collection->add($item); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGivenAnInvalidOffsetItThrowsAnException(): void |
72
|
|
|
{ |
73
|
|
|
$this->expectException(InvalidArgumentException::class); |
74
|
|
|
$this->expectExceptionMessage('Provided offset has to be greater than -1'); |
75
|
|
|
|
76
|
|
|
$item = 'a'; |
77
|
|
|
|
78
|
|
|
$collection = StringCollection::create(); |
79
|
|
|
$collection->offsetSet(-1, $item); |
80
|
|
|
|
81
|
|
|
$this->expectException(InvalidArgumentException::class); |
82
|
|
|
$this->expectExceptionMessage('Provided offset has to be greater than -1'); |
83
|
|
|
|
84
|
|
|
$item = 'a'; |
85
|
|
|
|
86
|
|
|
$collection = StringCollection::create(); |
87
|
|
|
$collection->offsetSet("a", $item); |
88
|
|
|
|
89
|
|
|
$this->expectException(InvalidArgumentException::class); |
90
|
|
|
$this->expectExceptionMessage('Provided offset has to be greater than -1'); |
91
|
|
|
|
92
|
|
|
$item = 'a'; |
93
|
|
|
|
94
|
|
|
$collection = StringCollection::create(); |
95
|
|
|
$collection->offsetSet(null, $item); |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
$this->expectException(TypeError::class); |
99
|
|
|
$this->expectExceptionMessage('Provided value has to be string'); |
100
|
|
|
|
101
|
|
|
$item = null; |
102
|
|
|
|
103
|
|
|
$collection = StringCollection::create(); |
104
|
|
|
$collection->offsetSet(0, $item); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|