Skip to content

Commit e63cf7d

Browse files
Update Doctrine2.php (#12)
Switched to more modern syntax: `User::class` and `[]`
1 parent 0ff3e7e commit e63cf7d

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

src/Codeception/Module/Doctrine2.php

+26-33
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@
5353
* modules:
5454
* enabled:
5555
* - Doctrine2:
56-
* connection_callback: ['MyDb', 'createEntityManager']
57-
* cleanup: true # All doctrine queries will be wrapped in a transaction, which will be rolled back at the end of each test
56+
* connection_callback: ['MyDb', 'createEntityManager'] # Call the static method `MyDb::createEntityManager()` to get the Entity Manager
5857
* ```
5958
*
60-
* This will use static method of `MyDb::createEntityManager()` to establish the Entity Manager.
61-
*
6259
* By default, the module will wrap everything into a transaction for each test and roll it back afterwards
6360
* (this is controlled by the `cleanup` setting).
6461
* By doing this, tests will run much faster and will be isolated from each other.
@@ -97,7 +94,7 @@
9794
* for more flexibility, e.g.:
9895
*
9996
* ```php
100-
* $I->seeInRepository('User', [
97+
* $I->seeInRepository(User::class, [
10198
* 'name' => 'John',
10299
* Criteria::create()->where(
103100
* Criteria::expr()->endsWith('email', '@domain.com')
@@ -108,7 +105,7 @@
108105
* If criteria is just a `->where(...)` construct, you can pass just expression without criteria wrapper:
109106
*
110107
* ```php
111-
* $I->seeInRepository('User', [
108+
* $I->seeInRepository(User::class, [
112109
* 'name' => 'John',
113110
* Criteria::expr()->endsWith('email', '@domain.com'),
114111
* ]);
@@ -340,7 +337,7 @@ public function clearEntityManager()
340337
}
341338

342339
/**
343-
* This method is deprecated in favor of `haveInRepository()`. It's functionality is exactly the same.
340+
* This method is deprecated in favor of `haveInRepository()`. Its functionality is exactly the same.
344341
*/
345342
public function persistEntity($obj, $values = [])
346343
{
@@ -359,7 +356,7 @@ public function persistEntity($obj, $values = [])
359356
* ``` php
360357
* <?php
361358
*
362-
* $I->haveFakeRepository('Entity\User', array('findByUsername' => function($username) { return null; }));
359+
* $I->haveFakeRepository(User::class, ['findByUsername' => function($username) { return null; }]);
363360
*
364361
* ```
365362
*
@@ -445,50 +442,46 @@ public function haveFakeRepository($classname, $methods = [])
445442
* If the primary key is composite, an array of values is returned.
446443
*
447444
* ```php
448-
* $I->haveInRepository('Entity\User', array('name' => 'davert'));
445+
* $I->haveInRepository(User::class, ['name' => 'davert']);
449446
* ```
450447
*
451448
* This method also accepts instances as first argument, which is useful when the entity constructor
452449
* has some arguments:
453450
*
454451
* ```php
455-
* $I->haveInRepository(new User($arg), array('name' => 'davert'));
452+
* $I->haveInRepository(new User($arg), ['name' => 'davert']);
456453
* ```
457454
*
458455
* Alternatively, constructor arguments can be passed by name. Given User constructor signature is `__constructor($arg)`, the example above could be rewritten like this:
459456
*
460457
* ```php
461-
* $I->haveInRepository('Entity\User', array('arg' => $arg, 'name' => 'davert'));
458+
* $I->haveInRepository(User::class, ['arg' => $arg, 'name' => 'davert']);
462459
* ```
463460
*
464461
* If the entity has relations, they can be populated too. In case of
465462
* [OneToMany](https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional)
466463
* the following format is expected:
467464
*
468465
* ```php
469-
* $I->haveInRepository('Entity\User', array(
466+
* $I->haveInRepository(User::class, [
470467
* 'name' => 'davert',
471-
* 'posts' => array(
472-
* array(
473-
* 'title' => 'Post 1',
474-
* ),
475-
* array(
476-
* 'title' => 'Post 2',
477-
* ),
478-
* ),
479-
* ));
468+
* 'posts' => [
469+
* ['title' => 'Post 1'],
470+
* ['title' => 'Post 2'],
471+
* ],
472+
* ]);
480473
* ```
481474
*
482475
* For [ManyToOne](https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional)
483476
* the format is slightly different:
484477
*
485478
* ```php
486-
* $I->haveInRepository('Entity\User', array(
479+
* $I->haveInRepository(User::class, [
487480
* 'name' => 'davert',
488-
* 'post' => array(
481+
* 'post' => [
489482
* 'title' => 'Post 1',
490-
* ),
491-
* ));
483+
* ],
484+
* ]);
492485
* ```
493486
*
494487
* This works recursively, so you can create deep structures in a single call.
@@ -838,9 +831,9 @@ private function populateEmbeddables($entityObject, array $data)
838831
*
839832
* ``` php
840833
* <?php
841-
* $I->seeInRepository('AppBundle:User', array('name' => 'davert'));
842-
* $I->seeInRepository('User', array('name' => 'davert', 'Company' => array('name' => 'Codegyre')));
843-
* $I->seeInRepository('Client', array('User' => array('Company' => array('name' => 'Codegyre')));
834+
* $I->seeInRepository(User::class, ['name' => 'davert']);
835+
* $I->seeInRepository(User::class, ['name' => 'davert', 'Company' => ['name' => 'Codegyre']]);
836+
* $I->seeInRepository(Client::class, ['User' => ['Company' => ['name' => 'Codegyre']]]);
844837
* ?>
845838
* ```
846839
*
@@ -889,7 +882,7 @@ protected function proceedSeeInRepository($entity, $params = [])
889882
*
890883
* ``` php
891884
* <?php
892-
* $email = $I->grabFromRepository('User', 'email', array('name' => 'davert'));
885+
* $email = $I->grabFromRepository(User::class, 'email', ['name' => 'davert']);
893886
* ?>
894887
* ```
895888
*
@@ -920,13 +913,13 @@ public function grabFromRepository($entity, $field, $params = [])
920913
*
921914
* ``` php
922915
* <?php
923-
* $users = $I->grabEntitiesFromRepository('AppBundle:User', array('name' => 'davert'));
916+
* $users = $I->grabEntitiesFromRepository(User::class, ['name' => 'davert']);
924917
* ?>
925918
* ```
926919
*
927920
* @version 1.1
928921
* @param $entity
929-
* @param array $params. For `IS NULL`, use `array('field'=>null)`
922+
* @param array $params. For `IS NULL`, use `['field' => null]`
930923
* @return array
931924
*/
932925
public function grabEntitiesFromRepository($entity, $params = [])
@@ -951,13 +944,13 @@ public function grabEntitiesFromRepository($entity, $params = [])
951944
*
952945
* ``` php
953946
* <?php
954-
* $user = $I->grabEntityFromRepository('User', array('id' => '1234'));
947+
* $user = $I->grabEntityFromRepository(User::class, ['id' => '1234']);
955948
* ?>
956949
* ```
957950
*
958951
* @version 1.1
959952
* @param $entity
960-
* @param array $params. For `IS NULL`, use `array('field'=>null)`
953+
* @param array $params. For `IS NULL`, use `['field' => null]`
961954
* @return object
962955
*/
963956
public function grabEntityFromRepository($entity, $params = [])

0 commit comments

Comments
 (0)