commit | author | age
|
2207d6
|
1 |
# Doctrine2 |
W |
2 |
|
|
3 |
|
|
4 |
Access the database using [Doctrine2 ORM](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/). |
|
5 |
|
|
6 |
When used with Zend Framework 2 or Symfony2, Doctrine's Entity Manager is automatically retrieved from Service Locator. |
|
7 |
Set up your `functional.suite.yml` like this: |
|
8 |
|
|
9 |
``` |
|
10 |
modules: |
|
11 |
enabled: |
|
12 |
- Symfony # 'ZF2' or 'Symfony' |
|
13 |
- Doctrine2: |
|
14 |
depends: Symfony |
|
15 |
cleanup: true # All doctrine queries will be wrapped in a transaction, which will be rolled back at the end of each test |
|
16 |
``` |
|
17 |
|
|
18 |
If you don't use Symfony or Zend Framework, you need to specify a callback function to retrieve the Entity Manager: |
|
19 |
|
|
20 |
``` |
|
21 |
modules: |
|
22 |
enabled: |
|
23 |
- Doctrine2: |
|
24 |
connection_callback: ['MyDb', 'createEntityManager'] |
|
25 |
cleanup: true # All doctrine queries will be wrapped in a transaction, which will be rolled back at the end of each test |
|
26 |
|
|
27 |
``` |
|
28 |
|
|
29 |
This will use static method of `MyDb::createEntityManager()` to establish the Entity Manager. |
|
30 |
|
|
31 |
By default, the module will wrap everything into a transaction for each test and roll it back afterwards. By doing this |
|
32 |
tests will run much faster and will be isolated from each other. |
|
33 |
|
|
34 |
## Status |
|
35 |
|
|
36 |
* Maintainer: **davert** |
|
37 |
* Stability: **stable** |
|
38 |
* Contact: codecept@davert.mail.ua |
|
39 |
|
|
40 |
## Config |
|
41 |
|
|
42 |
## Public Properties |
|
43 |
|
|
44 |
* `em` - Entity Manager |
|
45 |
|
|
46 |
## Actions |
|
47 |
|
|
48 |
### dontSeeInRepository |
|
49 |
|
|
50 |
Flushes changes to database and performs `findOneBy()` call for current repository. |
|
51 |
|
|
52 |
* `param` $entity |
|
53 |
* `param array` $params |
|
54 |
|
|
55 |
|
|
56 |
### flushToDatabase |
|
57 |
|
|
58 |
Performs $em->flush(); |
|
59 |
|
|
60 |
|
|
61 |
### grabEntitiesFromRepository |
|
62 |
|
|
63 |
Selects entities from repository. |
|
64 |
It builds query based on array of parameters. |
|
65 |
You can use entity associations to build complex queries. |
|
66 |
|
|
67 |
Example: |
|
68 |
|
|
69 |
``` php |
|
70 |
<?php |
|
71 |
$users = $I->grabEntitiesFromRepository('AppBundle:User', array('name' => 'davert')); |
|
72 |
?> |
|
73 |
``` |
|
74 |
|
|
75 |
* `Available since` 1.1 |
|
76 |
* `param` $entity |
|
77 |
* `param array` $params |
|
78 |
* `return` array |
|
79 |
|
|
80 |
|
|
81 |
### grabEntityFromRepository |
|
82 |
|
|
83 |
Selects a single entity from repository. |
|
84 |
It builds query based on array of parameters. |
|
85 |
You can use entity associations to build complex queries. |
|
86 |
|
|
87 |
Example: |
|
88 |
|
|
89 |
``` php |
|
90 |
<?php |
|
91 |
$user = $I->grabEntityFromRepository('User', array('id' => '1234')); |
|
92 |
?> |
|
93 |
``` |
|
94 |
|
|
95 |
* `Available since` 1.1 |
|
96 |
* `param` $entity |
|
97 |
* `param array` $params |
|
98 |
* `return` object |
|
99 |
|
|
100 |
|
|
101 |
### grabFromRepository |
|
102 |
|
|
103 |
Selects field value from repository. |
|
104 |
It builds query based on array of parameters. |
|
105 |
You can use entity associations to build complex queries. |
|
106 |
|
|
107 |
Example: |
|
108 |
|
|
109 |
``` php |
|
110 |
<?php |
|
111 |
$email = $I->grabFromRepository('User', 'email', array('name' => 'davert')); |
|
112 |
?> |
|
113 |
``` |
|
114 |
|
|
115 |
* `Available since` 1.1 |
|
116 |
* `param` $entity |
|
117 |
* `param` $field |
|
118 |
* `param array` $params |
|
119 |
* `return` array |
|
120 |
|
|
121 |
|
|
122 |
### haveFakeRepository |
|
123 |
|
|
124 |
Mocks the repository. |
|
125 |
|
|
126 |
With this action you can redefine any method of any repository. |
|
127 |
Please, note: this fake repositories will be accessible through entity manager till the end of test. |
|
128 |
|
|
129 |
Example: |
|
130 |
|
|
131 |
``` php |
|
132 |
<?php |
|
133 |
|
|
134 |
$I->haveFakeRepository('Entity\User', array('findByUsername' => function($username) { return null; })); |
|
135 |
|
|
136 |
``` |
|
137 |
|
|
138 |
This creates a stub class for Entity\User repository with redefined method findByUsername, |
|
139 |
which will always return the NULL value. |
|
140 |
|
|
141 |
* `param` $classname |
|
142 |
* `param array` $methods |
|
143 |
|
|
144 |
|
|
145 |
### haveInRepository |
|
146 |
|
|
147 |
Persists record into repository. |
|
148 |
This method creates an entity, and sets its properties directly (via reflection). |
|
149 |
Setters of entity won't be executed, but you can create almost any entity and save it to database. |
|
150 |
Returns id using `getId` of newly created entity. |
|
151 |
|
|
152 |
```php |
|
153 |
$I->haveInRepository('Entity\User', array('name' => 'davert')); |
|
154 |
``` |
|
155 |
|
|
156 |
|
|
157 |
### onReconfigure |
|
158 |
|
|
159 |
@throws ModuleConfigException |
|
160 |
|
|
161 |
|
|
162 |
### persistEntity |
|
163 |
|
|
164 |
Adds entity to repository and flushes. You can redefine it's properties with the second parameter. |
|
165 |
|
|
166 |
Example: |
|
167 |
|
|
168 |
``` php |
|
169 |
<?php |
|
170 |
$I->persistEntity(new \Entity\User, array('name' => 'Miles')); |
|
171 |
$I->persistEntity($user, array('name' => 'Miles')); |
|
172 |
``` |
|
173 |
|
|
174 |
* `param` $obj |
|
175 |
* `param array` $values |
|
176 |
|
|
177 |
|
|
178 |
### seeInRepository |
|
179 |
|
|
180 |
Flushes changes to database, and executes a query with parameters defined in an array. |
|
181 |
You can use entity associations to build complex queries. |
|
182 |
|
|
183 |
Example: |
|
184 |
|
|
185 |
``` php |
|
186 |
<?php |
|
187 |
$I->seeInRepository('AppBundle:User', array('name' => 'davert')); |
|
188 |
$I->seeInRepository('User', array('name' => 'davert', 'Company' => array('name' => 'Codegyre'))); |
|
189 |
$I->seeInRepository('Client', array('User' => array('Company' => array('name' => 'Codegyre'))); |
|
190 |
?> |
|
191 |
``` |
|
192 |
|
|
193 |
Fails if record for given criteria can\'t be found, |
|
194 |
|
|
195 |
* `param` $entity |
|
196 |
* `param array` $params |
|
197 |
|
|
198 |
<p> </p><div class="alert alert-warning">Module reference is taken from the source code. <a href="https://github.com/Codeception/Codeception/tree/2.5/src/Codeception/Module/Doctrine2.php">Help us to improve documentation. Edit module reference</a></div> |