1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Gherkin\Node\TableNode; |
6
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
9
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardManager; |
10
|
|
|
use Doctrine\ORM\EntityManager; |
11
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
13
|
|
|
|
14
|
|
|
class EntityContext extends Context |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @Given /^the following ([\w ]+):?$/ |
18
|
|
|
*/ |
19
|
|
|
public function theFollowing($name, TableNode $table) |
20
|
|
|
{ |
21
|
|
|
$entityName = $this->resolveEntity($name)->getName(); |
22
|
|
|
|
23
|
|
|
$rows = $table->getRows(); |
|
|
|
|
24
|
|
|
$headers = array_shift($rows); |
25
|
|
|
|
26
|
|
|
foreach ($rows as $row) { |
27
|
|
|
$values = array_combine($headers, $row); |
28
|
|
|
$entity = new $entityName; |
29
|
|
|
$reflection = new \ReflectionClass($entity); |
30
|
|
|
|
31
|
|
View Code Duplication |
do { |
|
|
|
|
32
|
|
|
$this |
33
|
|
|
->getRecordBag() |
34
|
|
|
->getCollection($reflection->getName()) |
35
|
|
|
->attach($entity, $values) |
36
|
|
|
; |
37
|
|
|
$reflection = $reflection->getParentClass(); |
38
|
|
|
} while (false !== $reflection); |
39
|
|
|
|
40
|
|
|
$this |
41
|
|
|
->getEntityHydrator() |
42
|
|
|
->hydrate($this->getEntityManager(), $entity, $values) |
43
|
|
|
->completeRequired($this->getEntityManager(), $entity) |
44
|
|
|
; |
45
|
|
|
|
46
|
|
|
$this->getEntityManager()->persist($entity); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->getEntityManager()->flush(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @Given /^there (?:is|are) (\d+) ((?!\w* like)\w*)$/ |
54
|
|
|
*/ |
55
|
|
|
public function thereIs($nbr, $name) |
56
|
|
|
{ |
57
|
|
|
$entityName = $this->resolveEntity($name)->getName(); |
58
|
|
|
|
59
|
|
|
for ($i = 0; $i < $nbr; $i++) { |
60
|
|
|
$entity = new $entityName; |
61
|
|
|
$this |
62
|
|
|
->getRecordBag() |
63
|
|
|
->getCollection($entityName) |
64
|
|
|
->attach($entity) |
65
|
|
|
; |
66
|
|
|
$this |
67
|
|
|
->getEntityHydrator() |
68
|
|
|
->completeRequired($this->getEntityManager(), $entity) |
69
|
|
|
; |
70
|
|
|
|
71
|
|
|
$this->getEntityManager()->persist($entity); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->getEntityManager()->flush(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @Given /^there (?:is|are) (\d+) (.*) like:?$/ |
79
|
|
|
*/ |
80
|
|
|
public function thereIsLikeFollowing($nbr, $name, TableNode $table) |
81
|
|
|
{ |
82
|
|
|
$entityName = $this->resolveEntity($name)->getName(); |
83
|
|
|
|
84
|
|
|
$rows = $table->getRows(); |
|
|
|
|
85
|
|
|
$headers = array_shift($rows); |
86
|
|
|
|
87
|
|
|
for ($i = 0; $i < $nbr; $i++) { |
88
|
|
|
$row = $rows[$i % count($rows)]; |
|
|
|
|
89
|
|
|
$values = array_combine($headers, $row); |
90
|
|
|
$entity = new $entityName; |
91
|
|
|
$this |
92
|
|
|
->getRecordBag() |
93
|
|
|
->getCollection($entityName) |
94
|
|
|
->attach($entity, $values) |
95
|
|
|
; |
96
|
|
|
$this |
97
|
|
|
->getEntityHydrator() |
98
|
|
|
->hydrate($this->getEntityManager(), $entity, $values) |
99
|
|
|
->completeRequired($this->getEntityManager(), $entity) |
100
|
|
|
; |
101
|
|
|
|
102
|
|
|
$this->getEntityManager()->persist($entity); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->getEntityManager()->flush(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @Given /^(\w+) (.+) should have been (created|deleted)$/ |
110
|
|
|
*/ |
111
|
|
|
public function entitiesShouldHaveBeen($expected, $entity, $state) |
112
|
|
|
{ |
113
|
|
|
$expected = (int) $expected; |
114
|
|
|
|
115
|
|
|
$entityName = $this->resolveEntity($entity)->getName(); |
116
|
|
|
$collection = $this |
117
|
|
|
->getRecordBag() |
118
|
|
|
->getCollection($entityName) |
119
|
|
|
; |
120
|
|
|
|
121
|
|
|
$records = array_map(function ($e) { return $e->getEntity(); }, $collection->all()); |
122
|
|
|
$entities = $this |
123
|
|
|
->getEntityManager() |
124
|
|
|
->getRepository($entityName) |
125
|
|
|
->createQueryBuilder('o') |
126
|
|
|
->resetDQLParts() |
127
|
|
|
->select('o') |
128
|
|
|
->from($entityName, ' o') |
129
|
|
|
->getQuery() |
130
|
|
|
->getResult() |
131
|
|
|
; |
132
|
|
|
|
133
|
|
|
if ($state === 'created') { |
134
|
|
|
$diff = $this->compareArray($entities, $records); |
135
|
|
|
foreach ($diff as $e) { |
136
|
|
|
$collection->attach($e); |
137
|
|
|
} |
138
|
|
|
} else { |
139
|
|
|
$diff = $this->compareArray($records, $entities); |
140
|
|
|
} |
141
|
|
|
$real = count($diff); |
142
|
|
|
|
143
|
|
|
$this |
144
|
|
|
->getAsserter() |
145
|
|
|
->assertEquals( |
146
|
|
|
$real, |
147
|
|
|
$expected, |
148
|
|
|
sprintf('%s %s should have been %s, %s actually', $expected, $entity, $state, $real) |
149
|
|
|
) |
150
|
|
|
; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @Then /^should be (\d+) (.*) like:?$/ |
155
|
|
|
*/ |
156
|
|
|
public function existLikeFollowing($nbr, $name, TableNode $table) |
157
|
|
|
{ |
158
|
|
|
$entityName = $this->resolveEntity($name)->getName(); |
159
|
|
|
|
160
|
|
|
$rows = $table->getRows(); |
|
|
|
|
161
|
|
|
$headers = array_shift($rows); |
162
|
|
|
|
163
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
164
|
|
|
|
165
|
|
|
for ($i = 0; $i < $nbr; $i++) { |
166
|
|
|
$row = $rows[$i % count($rows)]; |
167
|
|
|
|
168
|
|
|
$values = array_combine($headers, $row); |
169
|
|
|
$object = $this->getEntityManager() |
170
|
|
|
->getRepository($entityName) |
171
|
|
|
->findOneBy( |
172
|
|
|
$this->getEntityIdentifiers($entityName, $headers, $row) |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
if (is_null($object)) { |
176
|
|
|
throw new \Exception(sprintf("There is not any object for the following identifiers: %s", json_encode($this->getEntityIdentifiers($entityName, $headers, $row)))); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
$this->getEntityManager()->refresh($object); |
179
|
|
|
|
180
|
|
|
foreach ($values as $key => $value) { |
181
|
|
|
if ($value != $accessor->getValue($object, $key) ) { |
182
|
|
|
throw new \Exception("The expected object does not have property $key with value $value"); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @BeforeScenario |
190
|
|
|
*/ |
191
|
|
|
public function beforeScenario($event) |
192
|
|
|
{ |
193
|
|
|
$this->storeTags($event); |
194
|
|
|
|
195
|
|
|
if ($this->hasTags([ 'reset-schema', '~not-reset-schema' ])) { |
196
|
|
|
foreach ($this->getEntityManagers() as $name => $entityManager) { |
197
|
|
|
$connection = $entityManager->getConnection(); |
198
|
|
|
if ($connection instanceof PoolingShardConnection) { |
199
|
|
|
$poolingShardManager = $this->getPoolingShardManager($connection); |
200
|
|
|
foreach ($poolingShardManager->getShards() as $shardId) { |
201
|
|
|
// Switch to shard database |
202
|
|
|
$connection->connect($shardId['id']); |
203
|
|
|
$this->resetSchema($entityManager); |
204
|
|
|
} |
205
|
|
|
// Back to global |
206
|
|
|
$connection->connect(0); |
207
|
|
|
} else { |
208
|
|
|
$this->resetSchema($entityManager); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @AfterScenario |
216
|
|
|
*/ |
217
|
|
|
public function afterScenario($event) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$this->getRecordBag()->clear(); |
220
|
|
|
$this->getUniqueCache()->clear(); |
221
|
|
|
$this->getEntityManager()->clear(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param EntityManager $entityManager |
226
|
|
|
*/ |
227
|
|
|
protected function resetSchema(EntityManager $entityManager) |
228
|
|
|
{ |
229
|
|
|
$metadata = $this->getMetadata($entityManager); |
230
|
|
|
if (!empty($metadata)) { |
231
|
|
|
$tool = new SchemaTool($entityManager); |
232
|
|
|
$tool->dropSchema($metadata); |
233
|
|
|
$tool->createSchema($metadata); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
protected function compareArray(array $a1, array $a2) |
238
|
|
|
{ |
239
|
|
|
$diff = []; |
240
|
|
|
foreach ($a1 as $e) { |
241
|
|
|
if (!in_array($e, $a2)) { |
242
|
|
|
$diff[] = $e; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $diff; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Find PoolingShardManager related to connection. |
251
|
|
|
* |
252
|
|
|
* Cannot directly find PoolingShardManager by EntityManager's name cause |
253
|
|
|
* PoolingShardManager is created from Connection's name. |
254
|
|
|
* |
255
|
|
|
* @param PoolingShardConnection $poolingShardConnection |
256
|
|
|
* |
257
|
|
|
* @return PoolingShardManager |
258
|
|
|
* |
259
|
|
|
* @throws \LogicException Unable to find PoolingShardManager related to this PoolingShardConnection. |
260
|
|
|
*/ |
261
|
|
|
protected function getPoolingShardManager(PoolingShardConnection $poolingShardConnection) |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
foreach ($this->getConnections() as $name => $connection) { |
264
|
|
|
if ($connection === $poolingShardConnection |
265
|
|
|
&& $this->getKernel()->getContainer()->has(sprintf('doctrine.dbal.%s_shard_manager', $name)) |
266
|
|
|
) { |
267
|
|
|
return $this->get(sprintf('doctrine.dbal.%s_shard_manager', $name)); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
throw new \LogicException('Unable to find PoolingShardManager related to this PoolingShardConnection.'); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param EntityManager $entityManager |
275
|
|
|
* |
276
|
|
|
* @return ClassMetadata[] |
277
|
|
|
*/ |
278
|
|
|
protected function getMetadata(EntityManager $entityManager) |
279
|
|
|
{ |
280
|
|
|
return $entityManager->getMetadataFactory()->getAllMetadata(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return EntityManager[] |
285
|
|
|
*/ |
286
|
|
|
protected function getEntityManagers() |
287
|
|
|
{ |
288
|
|
|
return $this->get('doctrine')->getManagers(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @return Connection[] |
293
|
|
|
*/ |
294
|
|
|
protected function getConnections() |
295
|
|
|
{ |
296
|
|
|
return $this->get('doctrine')->getConnections(); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
protected function getDefaultOptions() |
300
|
|
|
{ |
301
|
|
|
return [ |
302
|
|
|
'Entities' => [''], |
303
|
|
|
]; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param string $entityName |
308
|
|
|
* @param array $headers Headers of the Behat TableNode |
309
|
|
|
* @param array $row current row if tge Behat TableNode |
310
|
|
|
* @return array ['id_column_A' => 'value', 'id_column_B' => 'value'] |
311
|
|
|
* @throws \Exception |
312
|
|
|
*/ |
313
|
|
|
protected function getEntityIdentifiers($entityName, $headers, $row) |
314
|
|
|
{ |
315
|
|
|
$metadata = $this->getEntityManager()->getClassMetadata($entityName); |
|
|
|
|
316
|
|
|
$identifiers = $metadata->getIdentifierFieldNames(); |
317
|
|
|
|
318
|
|
|
$identifiersWithValues = []; |
|
|
|
|
319
|
|
|
|
320
|
|
|
foreach ($identifiers as $identifier) { |
321
|
|
|
$headersPosition = array_search($identifier, $headers); |
|
|
|
|
322
|
|
|
$identifiersWithValues[$identifier] = $row[$headersPosition]; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return $identifiersWithValues; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.