Skip to content

Commit

Permalink
Update Doctrine2.php (#12)
Browse files Browse the repository at this point in the history
Switched to more modern syntax: `User::class` and `[]`
  • Loading branch information
ThomasLandauer committed Oct 11, 2020
1 parent 0ff3e7e commit e63cf7d
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions src/Codeception/Module/Doctrine2.php
Expand Up @@ -53,12 +53,9 @@
* modules:
* enabled:
* - Doctrine2:
* connection_callback: ['MyDb', 'createEntityManager']
* cleanup: true # All doctrine queries will be wrapped in a transaction, which will be rolled back at the end of each test
* connection_callback: ['MyDb', 'createEntityManager'] # Call the static method `MyDb::createEntityManager()` to get the Entity Manager
* ```
*
* This will use static method of `MyDb::createEntityManager()` to establish the Entity Manager.
*
* By default, the module will wrap everything into a transaction for each test and roll it back afterwards
* (this is controlled by the `cleanup` setting).
* By doing this, tests will run much faster and will be isolated from each other.
Expand Down Expand Up @@ -97,7 +94,7 @@
* for more flexibility, e.g.:
*
* ```php
* $I->seeInRepository('User', [
* $I->seeInRepository(User::class, [
* 'name' => 'John',
* Criteria::create()->where(
* Criteria::expr()->endsWith('email', '@domain.com')
Expand All @@ -108,7 +105,7 @@
* If criteria is just a `->where(...)` construct, you can pass just expression without criteria wrapper:
*
* ```php
* $I->seeInRepository('User', [
* $I->seeInRepository(User::class, [
* 'name' => 'John',
* Criteria::expr()->endsWith('email', '@domain.com'),
* ]);
Expand Down Expand Up @@ -340,7 +337,7 @@ public function clearEntityManager()
}

/**
* This method is deprecated in favor of `haveInRepository()`. It's functionality is exactly the same.
* This method is deprecated in favor of `haveInRepository()`. Its functionality is exactly the same.
*/
public function persistEntity($obj, $values = [])
{
Expand All @@ -359,7 +356,7 @@ public function persistEntity($obj, $values = [])
* ``` php
* <?php
*
* $I->haveFakeRepository('Entity\User', array('findByUsername' => function($username) { return null; }));
* $I->haveFakeRepository(User::class, ['findByUsername' => function($username) { return null; }]);
*
* ```
*
Expand Down Expand Up @@ -445,50 +442,46 @@ public function haveFakeRepository($classname, $methods = [])
* If the primary key is composite, an array of values is returned.
*
* ```php
* $I->haveInRepository('Entity\User', array('name' => 'davert'));
* $I->haveInRepository(User::class, ['name' => 'davert']);
* ```
*
* This method also accepts instances as first argument, which is useful when the entity constructor
* has some arguments:
*
* ```php
* $I->haveInRepository(new User($arg), array('name' => 'davert'));
* $I->haveInRepository(new User($arg), ['name' => 'davert']);
* ```
*
* Alternatively, constructor arguments can be passed by name. Given User constructor signature is `__constructor($arg)`, the example above could be rewritten like this:
*
* ```php
* $I->haveInRepository('Entity\User', array('arg' => $arg, 'name' => 'davert'));
* $I->haveInRepository(User::class, ['arg' => $arg, 'name' => 'davert']);
* ```
*
* If the entity has relations, they can be populated too. In case of
* [OneToMany](https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional)
* the following format is expected:
*
* ```php
* $I->haveInRepository('Entity\User', array(
* $I->haveInRepository(User::class, [
* 'name' => 'davert',
* 'posts' => array(
* array(
* 'title' => 'Post 1',
* ),
* array(
* 'title' => 'Post 2',
* ),
* ),
* ));
* 'posts' => [
* ['title' => 'Post 1'],
* ['title' => 'Post 2'],
* ],
* ]);
* ```
*
* For [ManyToOne](https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional)
* the format is slightly different:
*
* ```php
* $I->haveInRepository('Entity\User', array(
* $I->haveInRepository(User::class, [
* 'name' => 'davert',
* 'post' => array(
* 'post' => [
* 'title' => 'Post 1',
* ),
* ));
* ],
* ]);
* ```
*
* This works recursively, so you can create deep structures in a single call.
Expand Down Expand Up @@ -838,9 +831,9 @@ private function populateEmbeddables($entityObject, array $data)
*
* ``` php
* <?php
* $I->seeInRepository('AppBundle:User', array('name' => 'davert'));
* $I->seeInRepository('User', array('name' => 'davert', 'Company' => array('name' => 'Codegyre')));
* $I->seeInRepository('Client', array('User' => array('Company' => array('name' => 'Codegyre')));
* $I->seeInRepository(User::class, ['name' => 'davert']);
* $I->seeInRepository(User::class, ['name' => 'davert', 'Company' => ['name' => 'Codegyre']]);
* $I->seeInRepository(Client::class, ['User' => ['Company' => ['name' => 'Codegyre']]]);
* ?>
* ```
*
Expand Down Expand Up @@ -889,7 +882,7 @@ protected function proceedSeeInRepository($entity, $params = [])
*
* ``` php
* <?php
* $email = $I->grabFromRepository('User', 'email', array('name' => 'davert'));
* $email = $I->grabFromRepository(User::class, 'email', ['name' => 'davert']);
* ?>
* ```
*
Expand Down Expand Up @@ -920,13 +913,13 @@ public function grabFromRepository($entity, $field, $params = [])
*
* ``` php
* <?php
* $users = $I->grabEntitiesFromRepository('AppBundle:User', array('name' => 'davert'));
* $users = $I->grabEntitiesFromRepository(User::class, ['name' => 'davert']);
* ?>
* ```
*
* @version 1.1
* @param $entity
* @param array $params. For `IS NULL`, use `array('field'=>null)`
* @param array $params. For `IS NULL`, use `['field' => null]`
* @return array
*/
public function grabEntitiesFromRepository($entity, $params = [])
Expand All @@ -951,13 +944,13 @@ public function grabEntitiesFromRepository($entity, $params = [])
*
* ``` php
* <?php
* $user = $I->grabEntityFromRepository('User', array('id' => '1234'));
* $user = $I->grabEntityFromRepository(User::class, ['id' => '1234']);
* ?>
* ```
*
* @version 1.1
* @param $entity
* @param array $params. For `IS NULL`, use `array('field'=>null)`
* @param array $params. For `IS NULL`, use `['field' => null]`
* @return object
*/
public function grabEntityFromRepository($entity, $params = [])
Expand Down

0 comments on commit e63cf7d

Please sign in to comment.