1 | <?php |
||||||
2 | |||||||
3 | namespace Bdf\Prime\Test; |
||||||
4 | |||||||
5 | use Bdf\Prime\Entity\Hydrator\Exception\UninitializedPropertyException; |
||||||
6 | use Bdf\Prime\Prime; |
||||||
7 | use PHPUnit\Framework\Constraint\Constraint; |
||||||
8 | |||||||
9 | /** |
||||||
10 | * RepositoryAssertion |
||||||
11 | */ |
||||||
12 | trait RepositoryAssertion |
||||||
13 | { |
||||||
14 | /** |
||||||
15 | * @var TestPack |
||||||
16 | */ |
||||||
17 | protected $testPack; |
||||||
18 | |||||||
19 | /** |
||||||
20 | * Get the test pack manager |
||||||
21 | * |
||||||
22 | * @return TestPack |
||||||
23 | */ |
||||||
24 | 196 | public function getTestPack() |
|||||
25 | { |
||||||
26 | 196 | if ($this->testPack === null) { |
|||||
27 | 196 | $this->testPack = TestPack::pack(); |
|||||
28 | } |
||||||
29 | |||||||
30 | 196 | return $this->testPack; |
|||||
31 | } |
||||||
32 | |||||||
33 | /** |
||||||
34 | * Assert that two array of entities are the same |
||||||
35 | * |
||||||
36 | * @param array $expectedEntities |
||||||
37 | * @param array $actualEntities |
||||||
38 | * @param string $message |
||||||
39 | * |
||||||
40 | * @throws \Exception |
||||||
41 | */ |
||||||
42 | 4 | public function assertSameEntities($expectedEntities, $actualEntities, $message = '') |
|||||
43 | { |
||||||
44 | 4 | $this->assertEquals( |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
45 | 4 | count($expectedEntities), |
|||||
46 | 4 | count($actualEntities), |
|||||
47 | 4 | 'Failed asserting that two collection entities are the same.'.($message ? PHP_EOL.$message : '') |
|||||
48 | 4 | ); |
|||||
49 | |||||||
50 | 3 | foreach ($expectedEntities as $index => $expectedEntity) { |
|||||
51 | 3 | $this->assertSameEntity($expectedEntity, $actualEntities[$index], $message); |
|||||
52 | } |
||||||
53 | } |
||||||
54 | |||||||
55 | /** |
||||||
56 | * Assert that two entities are the same |
||||||
57 | * Compare only fields defined in associated mapper |
||||||
58 | * |
||||||
59 | * @param object $expected |
||||||
60 | * @param object $entity |
||||||
61 | * @param string $message |
||||||
62 | * |
||||||
63 | * @throws \Exception |
||||||
64 | */ |
||||||
65 | 16 | public function assertSameEntity($expected, $entity, $message = '') |
|||||
66 | { |
||||||
67 | 16 | $this->assertEntity($expected, $entity, 0, $message); |
|||||
68 | } |
||||||
69 | |||||||
70 | /** |
||||||
71 | * Assert that two array of entities are equal |
||||||
72 | * |
||||||
73 | * @param array $expectedEntities |
||||||
74 | * @param array $actualEntities |
||||||
75 | * @param int $dateTimeDelta |
||||||
76 | * @param string $message |
||||||
77 | * |
||||||
78 | * @throws \Exception |
||||||
79 | */ |
||||||
80 | 12 | public function assertEntities($expectedEntities, $actualEntities, $dateTimeDelta = 5, $message = '') |
|||||
81 | { |
||||||
82 | 12 | if (is_string($dateTimeDelta)) { |
|||||
0 ignored issues
–
show
|
|||||||
83 | $message = $dateTimeDelta; |
||||||
84 | $dateTimeDelta = 5; |
||||||
85 | |||||||
86 | @trigger_error('The assertEntities interface change. Use message as 4th parameter.', E_USER_DEPRECATED); |
||||||
87 | } |
||||||
88 | |||||||
89 | 12 | $this->assertEquals( |
|||||
90 | 12 | count($expectedEntities), |
|||||
91 | 12 | count($actualEntities), |
|||||
92 | 12 | 'Failed asserting that two collection entities are the same.'.($message ? PHP_EOL.$message : '') |
|||||
93 | 12 | ); |
|||||
94 | |||||||
95 | 11 | foreach ($expectedEntities as $index => $expectedEntity) { |
|||||
96 | 11 | $this->assertEntity($expectedEntity, $actualEntities[$index], $dateTimeDelta, $message); |
|||||
97 | } |
||||||
98 | } |
||||||
99 | |||||||
100 | /** |
||||||
101 | * Assert that two entities are equal |
||||||
102 | * Compare only fields defined in associated mapper |
||||||
103 | * Useful for dates : add a time delta |
||||||
104 | * |
||||||
105 | * @param object $expected |
||||||
106 | * @param object $entity |
||||||
107 | * @param int $dateTimeDelta |
||||||
108 | * @param string $message |
||||||
109 | * |
||||||
110 | * @throws \Exception |
||||||
111 | */ |
||||||
112 | 52 | public function assertEntity($expected, $entity, $dateTimeDelta = 5, $message = '') |
|||||
113 | { |
||||||
114 | 52 | if (is_string($dateTimeDelta)) { |
|||||
0 ignored issues
–
show
|
|||||||
115 | $message = $dateTimeDelta; |
||||||
116 | $dateTimeDelta = 5; |
||||||
117 | |||||||
118 | @trigger_error('The assertEntity interface change. Use message as 4th parameter.', E_USER_DEPRECATED); |
||||||
119 | } |
||||||
120 | |||||||
121 | 52 | $this->compareEntity(get_class($expected), $expected, $entity, $dateTimeDelta, $message); |
|||||
0 ignored issues
–
show
$expected of type object is incompatible with the type array expected by parameter $expected of Bdf\Prime\Test\Repositor...ertion::compareEntity() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
122 | } |
||||||
123 | |||||||
124 | /** |
||||||
125 | * Compare entity values with a map of values. |
||||||
126 | * The map can also contain constraints |
||||||
127 | * |
||||||
128 | * @param string $expectedClass |
||||||
129 | * @param array $expected |
||||||
130 | * @param object|object[] $entities |
||||||
131 | * @param int $dateTimeDelta |
||||||
132 | * @param string $message |
||||||
133 | * |
||||||
134 | * @throws \Exception |
||||||
135 | */ |
||||||
136 | 2 | public function assertEntityValues($expectedClass, $expected, $entities, $dateTimeDelta = 5, $message = '') |
|||||
137 | { |
||||||
138 | 2 | if (!is_array($entities)) { |
|||||
139 | 1 | $this->compareEntity($expectedClass, $expected, $entities, $dateTimeDelta, $message); |
|||||
140 | 1 | return; |
|||||
141 | } |
||||||
142 | |||||||
143 | 1 | $this->assertEquals( |
|||||
144 | 1 | count($expected), |
|||||
145 | 1 | count($entities), |
|||||
146 | 1 | 'Failed asserting that two collection entities are the same.'.($message ? PHP_EOL.$message : '') |
|||||
147 | 1 | ); |
|||||
148 | |||||||
149 | 1 | foreach ($entities as $index => $entity) { |
|||||
150 | 1 | $this->compareEntity($expectedClass, $expected[$index], $entity, $dateTimeDelta, $message); |
|||||
151 | } |
||||||
152 | } |
||||||
153 | |||||||
154 | /** |
||||||
155 | * Compare 2 entities |
||||||
156 | * If strict is false, add delta on date comparison |
||||||
157 | * |
||||||
158 | * @param string $expectedClass |
||||||
159 | * @param array $expected |
||||||
160 | * @param object $entity |
||||||
161 | * @param int $dateTimeDelta |
||||||
162 | * @param string $message |
||||||
163 | * |
||||||
164 | * @throws \Exception |
||||||
165 | */ |
||||||
166 | 54 | private function compareEntity($expectedClass, $expected, $entity, $dateTimeDelta = 0, $message = '') |
|||||
167 | { |
||||||
168 | 54 | if (is_string($dateTimeDelta)) { |
|||||
0 ignored issues
–
show
|
|||||||
169 | $message = $dateTimeDelta; |
||||||
170 | $dateTimeDelta = 5; |
||||||
171 | |||||||
172 | @trigger_error('The compareEntity interface change. Use message as 4th parameter.', E_USER_DEPRECATED); |
||||||
173 | } |
||||||
174 | |||||||
175 | 54 | if ($message == '') { |
|||||
176 | 53 | $message = $expectedClass; |
|||||
177 | } |
||||||
178 | |||||||
179 | 54 | $this->assertEquals($expectedClass, get_class($entity), 'Failed asserting that two entities are the same.'); |
|||||
180 | |||||||
181 | 53 | $repository = Prime::repository($entity); |
|||||
182 | |||||||
183 | 53 | foreach ($repository->metadata()->attributes as $attribute => $metadata) { |
|||||
184 | 53 | $path = $repository->metadata()->entityClass.'::'.$attribute; |
|||||
185 | 53 | $isUninitialized = false; |
|||||
186 | |||||||
187 | try { |
||||||
188 | 53 | $expectedValue = is_object($expected) ? $repository->extractOne($expected, $attribute) : ($expected[$attribute] ?? null); |
|||||
0 ignored issues
–
show
The method
extractOne() does not exist on Bdf\Prime\Repository\RepositoryInterface . Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Repository\RepositoryInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
189 | 4 | } catch (UninitializedPropertyException $e) { |
|||||
190 | 4 | $isUninitialized = true; |
|||||
191 | } |
||||||
192 | |||||||
193 | try { |
||||||
194 | 53 | $value = $repository->extractOne($entity, $attribute); |
|||||
195 | |||||||
196 | 53 | if ($isUninitialized) { |
|||||
197 | 53 | $this->fail($message . ': Expected attribute "'.$path.'" to be not initialised'); |
|||||
0 ignored issues
–
show
It seems like
fail() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
198 | } |
||||||
199 | 4 | } catch (UninitializedPropertyException $e) { |
|||||
200 | 4 | if (!$isUninitialized) { |
|||||
201 | $this->fail($message . ': The attribute "'.$path.'" is not initialised'); |
||||||
202 | } |
||||||
203 | } |
||||||
204 | |||||||
205 | 53 | if (!is_object($expectedValue)) { |
|||||
206 | 53 | $this->assertSame($expectedValue, $value, $message . ': Expected attribute "'.$path.'" is not the same'); |
|||||
0 ignored issues
–
show
The method
assertSame() does not exist on Bdf\Prime\Test\RepositoryAssertion . Did you maybe mean assertSameEntities() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
207 | 53 | continue; |
|||||
208 | } |
||||||
209 | |||||||
210 | 17 | if ($expectedValue instanceof Constraint) { |
|||||
211 | 2 | $this->assertThat($value, $expectedValue, $message . ': Expected attribute "'.$path.'" is not the same'); |
|||||
0 ignored issues
–
show
It seems like
assertThat() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
212 | 17 | } elseif ($expectedValue instanceof \DateTimeInterface) { |
|||||
213 | 17 | $this->assertEqualsWithDelta($expectedValue, $value, $dateTimeDelta, $message . ': Expected attribute "'.$path.'" is not the same'); |
|||||
0 ignored issues
–
show
It seems like
assertEqualsWithDelta() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
214 | } else { |
||||||
215 | $this->assertEquals($expectedValue, $value, $message . ': Expected attribute "'.$path.'" is not the same'); |
||||||
216 | } |
||||||
217 | } |
||||||
218 | } |
||||||
219 | } |
||||||
220 |