1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File:ObjectArrayTest.php |
6
|
|
|
* |
7
|
|
|
* @author Maciej Sławik <[email protected]> |
8
|
|
|
* Github: https://github.com/maciejslawik |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MSlwk\TypeSafeArray\Test\Unit; |
12
|
|
|
|
13
|
|
|
use ArrayIterator; |
14
|
|
|
use DateTime; |
15
|
|
|
use DateTimeImmutable; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use MSlwk\TypeSafeArray\ObjectArray; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ObjectArrayTest |
22
|
|
|
* @package MSlwk\TypeSafeArray\Test\Unit |
23
|
|
|
*/ |
24
|
|
|
class ObjectArrayTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @dataProvider provideDifferentIterables |
28
|
|
|
* @param iterable $iterable |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function testOperationsOnArray(iterable $iterable): void |
32
|
|
|
{ |
33
|
|
|
$object1 = new DateTime(); |
34
|
|
|
$object2 = new DateTime(); |
35
|
|
|
$object3 = new DateTime(); |
36
|
|
|
$object4 = new DateTime(); |
37
|
|
|
$iterable[] = $object2; |
38
|
|
|
$objectArray = new ObjectArray(DateTime::class, $iterable); |
39
|
|
|
$objectArray[1] = $object3; |
40
|
|
|
$objectArray->offsetSet(2, $object4); |
41
|
|
|
$objectArray->add($object1); |
42
|
|
|
|
43
|
|
|
$this->assertSame($object2, $objectArray->offsetGet(0)); |
44
|
|
|
$this->assertSame($object3, $objectArray[1]); |
45
|
|
|
$this->assertSame($object4, $objectArray[2]); |
46
|
|
|
$this->assertSame($object1, $objectArray[3]); |
47
|
|
|
|
48
|
|
|
$this->assertSame($object2, $objectArray->current()); |
49
|
|
|
$objectArray->next(); |
50
|
|
|
$objectArray->next(); |
51
|
|
|
$this->assertSame($object4, $objectArray->current()); |
52
|
|
|
$objectArray->rewind(); |
53
|
|
|
$this->assertSame($object2, $objectArray->current()); |
54
|
|
|
|
55
|
|
|
$this->assertEquals(4, $objectArray->count()); |
56
|
|
|
$objectArray->offsetUnset(2); |
57
|
|
|
$this->assertEquals(3, $objectArray->count()); |
58
|
|
|
|
59
|
|
|
$this->assertTrue($objectArray->offsetExists(1)); |
60
|
|
|
$this->assertFalse($objectArray->offsetExists(2)); |
61
|
|
|
|
62
|
|
|
$this->assertTrue($objectArray->valid()); |
63
|
|
|
|
64
|
|
|
$this->assertEquals(0, $objectArray->key()); |
65
|
|
|
$objectArray->next(); |
66
|
|
|
$this->assertEquals(1, $objectArray->key()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @dataProvider provideDifferentIterables |
71
|
|
|
* @param iterable $iterable |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function testAddingWrongTypeViaConstructor(iterable $iterable): void |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->expectException(InvalidArgumentException::class); |
77
|
|
|
|
78
|
|
|
$object = new DateTimeImmutable(); |
79
|
|
|
$iterable[] = $object; |
80
|
|
|
new ObjectArray(DateTime::class, $iterable); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function testAddingWrongTypeViaOffsetSet(): void |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this->expectException(InvalidArgumentException::class); |
89
|
|
|
|
90
|
|
|
$object = new DateTimeImmutable(); |
91
|
|
|
$objectArray = new ObjectArray(DateTime::class); |
92
|
|
|
$objectArray->offsetSet(0, $object); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function testAddingWrongTypeViaAdd(): void |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$this->expectException(InvalidArgumentException::class); |
101
|
|
|
|
102
|
|
|
$object = new DateTimeImmutable(); |
103
|
|
|
$objectArray = new ObjectArray(DateTime::class); |
104
|
|
|
$objectArray->add($object); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function testAddingWrongTypeViaArrayAccess(): void |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$this->expectException(InvalidArgumentException::class); |
113
|
|
|
|
114
|
|
|
$object = new DateTimeImmutable(); |
115
|
|
|
$objectArray = new ObjectArray(DateTime::class); |
116
|
|
|
$objectArray[0] = $object; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function provideDifferentIterables(): array |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
'plain_array' => [ |
126
|
|
|
[] |
127
|
|
|
], |
128
|
|
|
'iterator' => [ |
129
|
|
|
new ArrayIterator() |
130
|
|
|
] |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.