Repository navigation
Expand file tree
/
Copy pathDoctrine2.php
More file actions
432 lines (392 loc) · 13.3 KB
/
Copy pathDoctrine2.php
File metadata and controls
432 lines (392 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
namespace Codeception\Module;
use Codeception\Module as CodeceptionModule;
use Codeception\Exception\ModuleConfigException;
use Codeception\Lib\Interfaces\DependsOnModule;
use Codeception\Lib\Interfaces\DoctrineProvider;
use Codeception\TestCase;
use Doctrine\ORM\EntityManager;
use Codeception\Util\Stub;
/**
* Allows integration and testing for projects with Doctrine2 ORM.
* Doctrine2 uses EntityManager to perform all database operations.
*
* When using with Zend Framework 2 or Symfony2 Doctrine connection is automatically retrieved from Service Locator.
* In this case you should include either **Symfony2** or **ZF2** module and specify it as dependent for Doctrine:
*
* ```
* modules:
* enabled:
* - Symfony2
* - Doctrine2:
* depends: Symfony2
* ```
*
* If you don't use any of frameworks above, you should specify a callback function to receive entity manager:
*
* ```
* modules:
* enabled:
* - Doctrine2:
* connection_callback: ['MyDb', 'createEntityManager']
*
* ```
*
* This will use static method of `MyDb::createEntityManager()` to establish EntityManager.
*
* By default module will wrap everything into transaction for each test and rollback it afterwards. By doing this
* tests won't write anything to database, and so will run much faster and will be isolate dfrom each other.
* This behavior can be changed by specifying `cleanup: false` in config.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: codecept@davert.mail.ua
*
* ## Config
*
* * cleanup: true - all doctrine queries will be run in transaction, which will be rolled back at the end of test.
* * connection_callback: - callable that will return an instance of EntityManager. This is a must if you run Doctrine without Zend2 or Symfony2 frameworks
*
* ### Example (`functional.suite.yml`)
*
* modules:
* enabled: [Doctrine2]
* config:
* Doctrine2:
* cleanup: false
*
* ## Public Properties
*
* * `em` - Entity Manager
*/
class Doctrine2 extends CodeceptionModule implements DependsOnModule
{
protected $config = [
'cleanup' => true,
'connection_callback' => false,
'depends' => null
];
protected $dependencyMessage = <<<EOF
Provide connection_callback function to establish database connection and get Entity Manager:
modules:
enabled:
- Doctrine2:
connection_callback: [My\ConnectionClass, getEntityManager]
Or set a dependent module, which can be either Symfony2 or ZF2 to get EM from service locator:
modules:
enabled:
- Doctrine2:
depends: Symfony2
EOF;
/**
* @var \Doctrine\ORM\EntityManager
*/
public $em = null;
/**
* @var \Codeception\Lib\Interfaces\DoctrineProvider
*/
private $dependentModule;
public function _depends()
{
if ($this->config['connection_callback']) {
return [];
}
return ['Codeception\Lib\Interfaces\DoctrineProvider' => $this->dependencyMessage];
}
public function _inject(DoctrineProvider $dependentModule = null)
{
$this->dependentModule = $dependentModule;
}
public function _beforeSuite($settings = [])
{
$this->retrieveEntityManager();
}
public function _before(TestCase $test)
{
$this->retrieveEntityManager();
if ($this->config['cleanup']) {
$this->em->getConnection()->beginTransaction();
}
}
protected function retrieveEntityManager()
{
if ($this->dependentModule) {
$this->em = $this->dependentModule->_getEntityManager();
} else {
if (is_callable($this->config['connection_callback'])) {
$this->em = call_user_func($this->config['connection_callback']);
}
}
if (!$this->em) {
throw new ModuleConfigException(
__CLASS__,
"EntityManager can't be obtained.\n \n"
. "Please specify either `connection_callback` config option\n"
. "with callable which will return instance of EntityManager or\n"
. "pass a dependent module which are Symfony2 or ZF2\n"
. "to connect to Doctrine using Dependency Injection Container"
);
}
if (!($this->em instanceof \Doctrine\ORM\EntityManager)) {
throw new ModuleConfigException(
__CLASS__,
"Connection object is not an instance of \\Doctrine\\ORM\\EntityManager.\n"
. "Use `connection_callback` or dependent framework modules to specify one"
);
}
$this->em->getConnection()->connect();
}
public function _after(TestCase $test)
{
if (!$this->em instanceof \Doctrine\ORM\EntityManager) {
return;
}
if ($this->config['cleanup'] && $this->em->getConnection()->isTransactionActive()) {
try {
$this->em->getConnection()->rollback();
} catch (\PDOException $e) {
}
}
$this->clean();
}
protected function clean()
{
$em = $this->em;
$reflectedEm = new \ReflectionClass($em);
if ($reflectedEm->hasProperty('repositories')) {
$property = $reflectedEm->getProperty('repositories');
$property->setAccessible(true);
$property->setValue($em, []);
}
$this->em->clear();
}
/**
* Performs $em->flush();
*/
public function flushToDatabase()
{
$this->em->flush();
}
/**
* Adds entity to repository and flushes. You can redefine it's properties with the second parameter.
*
* Example:
*
* ``` php
* <?php
* $I->persistEntity(new \Entity\User, array('name' => 'Miles'));
* $I->persistEntity($user, array('name' => 'Miles'));
* ```
*
* @param $obj
* @param array $values
*/
public function persistEntity($obj, $values = [])
{
if ($values) {
$reflectedObj = new \ReflectionClass($obj);
foreach ($values as $key => $val) {
$property = $reflectedObj->getProperty($key);
$property->setAccessible(true);
$property->setValue($obj, $val);
}
}
$this->em->persist($obj);
$this->em->flush();
}
/**
* Mocks the repository.
*
* With this action you can redefine any method of any repository.
* Please, note: this fake repositories will be accessible through entity manager till the end of test.
*
* Example:
*
* ``` php
* <?php
*
* $I->haveFakeRepository('Entity\User', array('findByUsername' => function($username) { return null; }));
*
* ```
*
* This creates a stub class for Entity\User repository with redefined method findByUsername, which will always return the NULL value.
*
* @param $classname
* @param array $methods
*/
public function haveFakeRepository($classname, $methods = [])
{
$em = $this->em;
$metadata = $em->getMetadataFactory()->getMetadataFor($classname);
$customRepositoryClassName = $metadata->customRepositoryClassName;
if (!$customRepositoryClassName) {
$customRepositoryClassName = '\Doctrine\ORM\EntityRepository';
}
$mock = Stub::make(
$customRepositoryClassName, array_merge(
[
'_entityName' => $metadata->name,
'_em' => $em,
'_class' => $metadata
],
$methods
)
);
$em->clear();
$reflectedEm = new \ReflectionClass($em);
if ($reflectedEm->hasProperty('repositories')) {
$property = $reflectedEm->getProperty('repositories');
$property->setAccessible(true);
$property->setValue($em, array_merge($property->getValue($em), [$classname => $mock]));
} else {
$this->debugSection('Warning', 'Repository can\'t be mocked, the EventManager class doesn\'t have "repositories" property');
}
}
/**
* Persists record into repository.
* This method crates an entity, and sets its properties directly (via reflection).
* Setters of entity won't be executed, but you can create almost any entity and save it to database.
* Returns id using `getId` of newly created entity.
*
* ```php
* $I->haveInRepository('Entity\User', array('name' => 'davert'));
* ```
*/
public function haveInRepository($entity, array $data)
{
$reflectedEntity = new \ReflectionClass($entity);
$entityObject = $reflectedEntity->newInstance();
foreach ($reflectedEntity->getProperties() as $property) {
/** @var $property \ReflectionProperty */
if (!isset($data[$property->name])) {
continue;
}
$property->setAccessible(true);
$property->setValue($entityObject, $data[$property->name]);
}
$this->em->persist($entityObject);
$this->em->flush();
if (method_exists($entityObject, 'getId')) {
$id = $entityObject->getId();
$this->debug("$entity entity created with id:$id");
return $id;
}
}
/**
* Flushes changes to database executes a query defined by array.
* It builds query based on array of parameters.
* You can use entity associations to build complex queries.
*
* Example:
*
* ``` php
* <?php
* $I->seeInRepository('User', array('name' => 'davert'));
* $I->seeInRepository('User', array('name' => 'davert', 'Company' => array('name' => 'Codegyre')));
* $I->seeInRepository('Client', array('User' => array('Company' => array('name' => 'Codegyre')));
* ?>
* ```
*
* Fails if record for given criteria can\'t be found,
*
* @param $entity
* @param array $params
*/
public function seeInRepository($entity, $params = [])
{
$res = $this->proceedSeeInRepository($entity, $params);
$this->assert($res);
}
/**
* Flushes changes to database and performs ->findOneBy() call for current repository.
*
* @param $entity
* @param array $params
*/
public function dontSeeInRepository($entity, $params = [])
{
$res = $this->proceedSeeInRepository($entity, $params);
$this->assertNot($res);
}
protected function proceedSeeInRepository($entity, $params = [])
{
// we need to store to database...
$this->em->flush();
$data = $this->em->getClassMetadata($entity);
$qb = $this->em->getRepository($entity)->createQueryBuilder('s');
$this->buildAssociationQuery($qb, $entity, 's', $params);
$this->debug($qb->getDQL());
$res = $qb->getQuery()->getArrayResult();
return ['True', (count($res) > 0), "$entity with " . json_encode($params)];
}
/**
* Selects field value from repository.
* It builds query based on array of parameters.
* You can use entity associations to build complex queries.
*
* Example:
*
* ``` php
* <?php
* $email = $I->grabFromRepository('User', 'email', array('name' => 'davert'));
* ?>
* ```
*
* @version 1.1
* @param $entity
* @param $field
* @param array $params
* @return array
*/
public function grabFromRepository($entity, $field, $params = [])
{
// we need to store to database...
$this->em->flush();
$data = $this->em->getClassMetadata($entity);
$qb = $this->em->getRepository($entity)->createQueryBuilder('s');
$qb->select('s.' . $field);
$this->buildAssociationQuery($qb, $entity, 's', $params);
$this->debug($qb->getDQL());
return $qb->getQuery()->getSingleScalarResult();
}
/**
* It's Fuckin Recursive!
*
* @param $qb
* @param $assoc
* @param $alias
* @param $params
*/
protected function buildAssociationQuery($qb, $assoc, $alias, $params)
{
$data = $this->em->getClassMetadata($assoc);
foreach ($params as $key => $val) {
if (isset($data->associationMappings)) {
if ($map = array_key_exists($key, $data->associationMappings)) {
if (is_array($val)) {
$qb->innerJoin("$alias.$key", $key);
foreach ($val as $column => $v) {
if (is_array($v)) {
$this->buildAssociationQuery($qb, $map['targetEntity'], $column, $v);
continue;
}
$paramname = $key . '__' . $column;
$qb->andWhere("$key.$column = :$paramname");
$qb->setParameter($paramname, $v);
}
continue;
}
}
}
if ($val === null) {
$qb->andWhere("s.$key IS NULL");
} else {
$paramname = "s__$key";
$qb->andWhere("s.$key = :$paramname");
$qb->setParameter($paramname, $val);
}
}
}
}