1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Atrapalo\PHPTools\Tester\Collection; |
4
|
|
|
|
5
|
|
|
use Atrapalo\PHPTools\Collection\EntityStrictCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class EntityCollectionTester |
9
|
|
|
* @package Atrapalo\PHPTools\Collection |
10
|
|
|
* |
11
|
|
|
* @author Guillermo González <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
trait EntityStrictCollectionTester |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
abstract protected function entityCollectionClass(): string; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @test |
22
|
|
|
*/ |
23
|
|
|
public function emptyConstructor() |
24
|
|
|
{ |
25
|
|
|
/** @var EntityStrictCollection $entityCollectionClass */ |
26
|
|
|
$entityCollectionClass = $this->entityCollectionClass(); |
27
|
|
|
$exception = $entityCollectionClass::customEmptyException(); |
28
|
|
|
|
29
|
|
|
$this->expectException(get_class($exception)); |
|
|
|
|
30
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
|
|
|
|
31
|
|
|
|
32
|
|
|
new $entityCollectionClass([]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
* @dataProvider invalidEntities |
38
|
|
|
* @param array $elements |
39
|
|
|
*/ |
40
|
|
|
public function invalidElementByConstructor(array $elements) |
41
|
|
|
{ |
42
|
|
|
/** @var EntityStrictCollection $entityCollectionClass */ |
43
|
|
|
$entityCollectionClass = $this->entityCollectionClass(); |
44
|
|
|
$exception = $entityCollectionClass::customInvalidEntityException(); |
45
|
|
|
|
46
|
|
|
$this->expectException(get_class($exception)); |
|
|
|
|
47
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
|
|
|
|
48
|
|
|
|
49
|
|
|
new $entityCollectionClass($elements); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function invalidEntities() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
[[1]], |
59
|
|
|
[['element']], |
60
|
|
|
[[new \stdClass()]], |
61
|
|
|
[[new \stdClass(), new \stdClass()]], |
62
|
|
|
[[null]], |
63
|
|
|
[[false]] |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
|
public function invalidAddElement() |
71
|
|
|
{ |
72
|
|
|
/** @var EntityStrictCollection $entityCollectionClass */ |
73
|
|
|
$entityCollectionClass = $this->entityCollectionClass(); |
74
|
|
|
$exception = $entityCollectionClass::customInvalidEntityException(); |
75
|
|
|
|
76
|
|
|
$this->expectException(get_class($exception)); |
|
|
|
|
77
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
|
|
|
|
78
|
|
|
|
79
|
|
|
/** @var EntityStrictCollection $collection */ |
80
|
|
|
$collection = new $entityCollectionClass([$this->prophesize($entityCollectionClass::entityClass())->reveal()], true); |
|
|
|
|
81
|
|
|
$collection->add([]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
*/ |
87
|
|
|
public function validChildrenElementByConstructor() |
88
|
|
|
{ |
89
|
|
|
/** @var EntityStrictCollection $entityCollectionClass */ |
90
|
|
|
$entityCollectionClass = $this->entityCollectionClass(); |
91
|
|
|
$exception = $entityCollectionClass::customInvalidEntityException(); |
92
|
|
|
|
93
|
|
|
$this->expectException(get_class($exception)); |
|
|
|
|
94
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
|
|
|
|
95
|
|
|
|
96
|
|
|
new $entityCollectionClass([$this->prophesize($entityCollectionClass::entityClass())->reveal()]); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
*/ |
102
|
|
|
public function validElementByConstructor() |
103
|
|
|
{ |
104
|
|
|
/** @var EntityStrictCollection $entityCollectionClass */ |
105
|
|
|
$entityCollectionClass = $this->entityCollectionClass(); |
106
|
|
|
|
107
|
|
|
/** @var EntityStrictCollection $collection */ |
108
|
|
|
$collection = new $entityCollectionClass([$this->prophesize($entityCollectionClass::entityClass())->reveal()], true); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$this->assertSame(1, $collection->count()); |
|
|
|
|
111
|
|
|
$this->assertInstanceOf($entityCollectionClass::entityClass(), $collection->current()); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @test |
116
|
|
|
*/ |
117
|
|
|
public function addTwoValidElement() |
118
|
|
|
{ |
119
|
|
|
/** @var EntityStrictCollection $entityCollectionClass */ |
120
|
|
|
$entityCollectionClass = $this->entityCollectionClass(); |
121
|
|
|
|
122
|
|
|
/** @var EntityStrictCollection $collection */ |
123
|
|
|
$collection = new $entityCollectionClass([$this->prophesize($entityCollectionClass::entityClass())->reveal()], true); |
|
|
|
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** @var \Prophecy\Prophecy\ObjectProphecy $entity */ |
127
|
|
|
$entity = $this->prophesize($entityCollectionClass::entityClass()); |
|
|
|
|
128
|
|
|
$collection->add($entity->reveal()); |
129
|
|
|
$collection->add($entity->reveal()); |
130
|
|
|
|
131
|
|
|
$this->assertSame(3, $collection->count()); |
|
|
|
|
132
|
|
|
foreach ($collection as $element) { |
133
|
|
|
$this->assertInstanceOf($entityCollectionClass::entityClass(), $element); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.