1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Collections\Tests\Typed; |
4
|
|
|
|
5
|
|
|
use Nip\Collections\Exceptions\InvalidTypeException; |
6
|
|
|
use Nip\Collections\Typed\AbstractTypedCollection; |
7
|
|
|
use function PHPUnit\Framework\assertEquals; |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait AbstractTypeTraitTest |
11
|
|
|
* @package Nip\Collections\Tests\Typed |
12
|
|
|
*/ |
13
|
|
|
trait AbstractTypeTraitTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @dataProvider data_invalidValues |
17
|
|
|
* @param $value |
18
|
|
|
*/ |
19
|
|
|
public function test_add_invalidValues($value) |
20
|
|
|
{ |
21
|
|
|
$collection = $this->newCollection(); |
22
|
|
|
$exceptions = 0; |
23
|
|
|
|
24
|
|
|
try { |
25
|
|
|
$collection->add($value); |
26
|
|
|
} catch (InvalidTypeException $exception) { |
27
|
|
|
$exceptions++; |
28
|
|
|
} |
29
|
|
|
try { |
30
|
|
|
$collection->set(1, $value); |
31
|
|
|
} catch (InvalidTypeException $exception) { |
32
|
|
|
$exceptions++; |
33
|
|
|
} |
34
|
|
|
try { |
35
|
|
|
$collection[] = $value; |
36
|
|
|
} catch (InvalidTypeException $exception) { |
37
|
|
|
$exceptions++; |
38
|
|
|
} |
39
|
|
|
try { |
40
|
|
|
$collection[1] = $value; |
41
|
|
|
} catch (InvalidTypeException $exception) { |
42
|
|
|
$exceptions++; |
43
|
|
|
} |
44
|
|
|
try { |
45
|
|
|
$collection->unshift($value); |
46
|
|
|
} catch (InvalidTypeException $exception) { |
47
|
|
|
$exceptions++; |
48
|
|
|
} |
49
|
|
|
try { |
50
|
|
|
$collection->unshift($value, 3); |
51
|
|
|
} catch (InvalidTypeException $exception) { |
52
|
|
|
$exceptions++; |
53
|
|
|
} |
54
|
|
|
try { |
55
|
|
|
$collection->push($value); |
56
|
|
|
} catch (InvalidTypeException $exception) { |
57
|
|
|
$exceptions++; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
self::assertSame(7, $exceptions); |
61
|
|
|
self::assertSame(0, $collection->count()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @dataProvider data_validValues |
66
|
|
|
* @param $value |
67
|
|
|
*/ |
68
|
|
|
public function test_add_validValues($value) |
69
|
|
|
{ |
70
|
|
|
$collection = $this->newCollection(); |
71
|
|
|
|
72
|
|
|
$collection->add($value); |
73
|
|
|
$collection->set(1, $value); |
74
|
|
|
$collection[] = $value; |
75
|
|
|
$collection[9] = $value; |
76
|
|
|
$collection->unshift($value); |
77
|
|
|
$collection->push($value); |
78
|
|
|
|
79
|
|
|
self::assertCount(6, $collection); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function data_invalidValues() |
86
|
|
|
{ |
87
|
|
|
$return = []; |
88
|
|
|
foreach ($this->typeInvalidValues() as $value) { |
89
|
|
|
$return[] = [$value]; |
90
|
|
|
} |
91
|
|
|
return $return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function data_validValues() |
98
|
|
|
{ |
99
|
|
|
$return = []; |
100
|
|
|
foreach ($this->typeValidValues() as $value) { |
101
|
|
|
$return[] = [$value]; |
102
|
|
|
} |
103
|
|
|
return $return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function newCollection(): AbstractTypedCollection |
107
|
|
|
{ |
108
|
|
|
$type = $this->typeClass(); |
109
|
|
|
$collection = new $type(); |
110
|
|
|
return $collection; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
abstract protected function typeClass(); |
114
|
|
|
|
115
|
|
|
abstract protected function typeInvalidValues(): array; |
116
|
|
|
|
117
|
|
|
abstract protected function typeValidValues(): array; |
118
|
|
|
} |
119
|
|
|
|