Skip to content

Commit 29c0db2

Browse files
committed
updated all guides
1 parent 04415a9 commit 29c0db2

File tree

5 files changed

+242
-397
lines changed

5 files changed

+242
-397
lines changed

guides/09-Data.md

Lines changed: 56 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Working with Data
1+
# Data Management
22

33
Tests should not affect each other. That's a rule of thumb. When tests interact with a database,
44
they may change the data inside it, which would eventually lead to data inconsistency.
@@ -17,16 +17,14 @@ you should use a special test database for testing. **Do not ever run tests on d
1717

1818
Codeception has a `Db` module, which takes on most of the tasks of database interaction.
1919

20-
{% highlight yaml %}
21-
20+
```yaml
2221
modules:
2322
config:
2423
Db:
2524
dsn: 'PDO DSN HERE'
2625
user: 'root'
2726
password:
28-
29-
{% endhighlight %}
27+
```
3028
3129
<div class="alert alert-notice">
3230
Use <a href="https://codeception.com/docs/06-ModulesAndHelpers#Dynamic-Configuration-With-Params">module parameters</a>
@@ -36,8 +34,7 @@ to set the database credentials from environment variables or from application c
3634
Db module can cleanup database between tests by loading a database dump. This can be done by parsing SQL file and
3735
executing its commands using current connection
3836
39-
{% highlight yaml %}
40-
37+
```yaml
4138
modules:
4239
config:
4340
Db:
@@ -48,14 +45,12 @@ modules:
4845
cleanup: true # reload dump between tests
4946
populate: true # load dump before all tests
5047

51-
52-
{% endhighlight %}
48+
```
5349

5450
Alternatively an external tool (like mysql client, or pg_restore) can be used. This approach is faster and won't produce parsing errors while loading a dump.
5551
Use `populator` config option to specify the command. For MySQL it can look like this:
5652

57-
{% highlight yaml %}
58-
53+
```yaml
5954
modules:
6055
enabled:
6156
- Db:
@@ -65,8 +60,7 @@ modules:
6560
cleanup: true # run populator before each test
6661
populate: true # run populator before all test
6762
populator: 'mysql -u $user $dbname < tests/_data/dump.sql'
68-
69-
{% endhighlight %}
63+
```
7064
7165
See the [Db module reference](https://codeception.com/docs/modules/Db#SQL-data-dump) for more examples.
7266
@@ -87,34 +81,27 @@ The Db module provides actions to create and verify data inside a database.
8781
If you want to create a special database record for one test,
8882
you can use [`haveInDatabase`](https://codeception.com/docs/modules/Db#haveInDatabase) method of `Db` module:
8983

90-
{% highlight php %}
91-
92-
<?php
84+
```php
9385
$I->haveInDatabase('posts', [
9486
'title' => 'Top 10 Testing Frameworks',
9587
'body' => '1. Codeception'
9688
]);
9789
$I->amOnPage('/posts');
9890
$I->see('Top 10 Testing Frameworks');
99-
100-
101-
{% endhighlight %}
91+
```
10292

10393
`haveInDatabase` inserts a row with the provided values into the database.
10494
All added records will be deleted at the end of the test.
10595

10696
If you want to check that a table record was created
10797
use [`seeInDatabase`](https://codeception.com/docs/modules/Db#haveInDatabase) method:
10898

109-
{% highlight php %}
110-
111-
<?php
99+
```php
112100
$I->amOnPage('/posts/1');
113101
$I->fillField('comment', 'This is nice!');
114102
$I->click('Submit');
115103
$I->seeInDatabase('comments', ['body' => 'This is nice!']);
116-
117-
{% endhighlight %}
104+
```
118105

119106
See the module [reference](https://codeception.com/docs/modules/Db) for other methods you can use for database testing.
120107

@@ -155,9 +142,7 @@ Corresponding framework modules provide similar methods for ORM access:
155142

156143
They allow you to create and check data by model name and field names in the model. Here is the example in Laravel:
157144

158-
{% highlight php %}
159-
160-
<?php
145+
```php
161146
// create record and get its id
162147
$id = $I->haveRecord('posts', ['body' => 'My first blogpost', 'user_id' => 1]);
163148
$I->amOnPage('/posts/'.$id);
@@ -167,30 +152,27 @@ $I->seeRecord('posts', ['id' => $id]);
167152
$I->click('Delete');
168153
// record was deleted
169154
$I->dontSeeRecord('posts', ['id' => $id]);
155+
```
170156

171-
{% endhighlight %}
172-
173-
Laravel5 module provides the method `have` which uses the [factory](https://laravel.com/docs/5.8/database-testing#generating-factories) method to generate models with fake data.
157+
Laravel5 module provides the method `have` which uses the [factory](https://laravel.com/docs/9.x/database-testing#creating-models-using-factories) method to generate models with fake data.
174158

175159
If you want to use ORM for integration testing only, you should enable the framework module with only the `ORM` part enabled:
176160

177-
{% highlight yaml %}
178-
161+
```yaml
179162
modules:
180163
enabled:
181-
- Laravel5:
164+
- Laravel:
182165
- part: ORM
166+
```
183167

184-
{% endhighlight %}
185-
186-
{% highlight yaml %}
168+
Similarly for Yii2 framework:
187169

170+
```yaml
188171
modules:
189172
enabled:
190173
- Yii2:
191174
- part: ORM
192-
193-
{% endhighlight %}
175+
```
194176

195177
This way no web actions will be added to `$I` object.
196178

@@ -199,44 +181,30 @@ Please note that inside acceptance tests, web applications work inside a webserv
199181
by rolling back transactions. You will need to disable cleaning up,
200182
and use the `Db` module to clean the database up between tests. Here is a sample config:
201183

202-
{% highlight yaml %}
203-
184+
```yaml
204185
modules:
205186
enabled:
206187
- WebDriver:
207188
url: http://localhost
208189
browser: firefox
209-
- Laravel5:
190+
- Laravel:
210191
cleanup: false
211192
- Db
212-
213-
{% endhighlight %}
193+
```
214194

215195
### Doctrine
216196

217197
Doctrine is also a popular ORM, unlike some others it implements the DataMapper pattern and is not bound to any framework.
218198
The [Doctrine2](https://codeception.com/docs/modules/Doctrine2) module requires an `EntityManager` instance to work with.
219199
It can be obtained from a Symfony framework or Zend Framework (configured with Doctrine):
220200

221-
{% highlight yaml %}
222-
201+
```yaml
223202
modules:
224203
enabled:
225204
- Symfony
226205
- Doctrine2:
227206
depends: Symfony
228-
229-
{% endhighlight %}
230-
231-
{% highlight yaml %}
232-
233-
modules:
234-
enabled:
235-
- ZF2
236-
- Doctrine2:
237-
depends: ZF2
238-
239-
{% endhighlight %}
207+
```
240208

241209
If no framework is used with Doctrine you should provide the `connection_callback` option
242210
with a valid callback to a function which returns an `EntityManager` instance.
@@ -259,41 +227,34 @@ and are provided by the [DataFactory](https://codeception.com/docs/modules/DataF
259227

260228
Once configured, it can create records with ease:
261229

262-
{% highlight php %}
263-
264-
<?php
230+
```php
265231
// creates a new user
266-
$user_id = $I->have('App\Model\User');
232+
$user_id = $I->have(\App\Model\User::class);
267233
// creates 3 posts
268-
$I->haveMultiple('App\Model\Post', 3);
269-
270-
{% endhighlight %}
234+
$I->haveMultiple(\App\Model\Post::class, 3);
235+
```
271236

272237
Created records will be deleted at the end of a test.
273238
The DataFactory module only works with ORM, so it requires one of the ORM modules to be enabled:
274239

275-
{% highlight yaml %}
276-
240+
```yaml
277241
modules:
278242
enabled:
279243
- Yii2:
280244
configFile: path/to/config.php
281245
- DataFactory:
282246
depends: Yii2
247+
```
283248

284-
{% endhighlight %}
285-
286-
{% highlight yaml %}
287-
249+
```yaml
288250
modules:
289251
enabled:
290252
- Symfony
291253
- Doctrine2:
292254
depends: Symfony
293255
- DataFactory:
294256
depends: Doctrine2
295-
296-
{% endhighlight %}
257+
```
297258

298259
DataFactory provides a powerful solution for managing data in integration/functional/acceptance tests.
299260
Read the [full reference](https://codeception.com/docs/modules/DataFactory) to learn how to set this module up.
@@ -309,24 +270,24 @@ This principle is so general that it can work for testing APIs, items on a web p
309270
Let's check that list of categories on a page is the same it was before.
310271
Create a snapshot class:
311272

312-
{% highlight php %}
313-
vendor/bin/codecept g:snapshot Categories
314-
315-
{% endhighlight %}
273+
```php
274+
php vendor/bin/codecept g:snapshot Categories
275+
```
316276

317277
Inject an actor class via constructor and implement `fetchData` method which should return a data set from a test.
318278

319-
{% highlight php %}
320-
279+
```php
321280
<?php
322-
namespace Snapshot;
281+
282+
namespace Tests\Support\Snapshot;
283+
284+
use Tests\Support\AcceptanceTester;
323285
324286
class Categories extends \Codeception\Snapshot
325287
{
326-
/** @var \AcceptanceTester */
327-
protected $i;
288+
protected AcceptanceTester $i;
328289
329-
public function __construct(\AcceptanceTester $I)
290+
public function __construct(AcceptanceTester $I)
330291
{
331292
$this->i = $I;
332293
}
@@ -337,60 +298,51 @@ class Categories extends \Codeception\Snapshot
337298
return $this->i->grabMultiple('a.category');
338299
}
339300
}
340-
341-
{% endhighlight %}
301+
```
342302

343303
Inside a test you can inject the snapshot class and call `assert` method on it:
344304

345-
{% highlight php %}
346-
347-
<?php
348-
public function testCategoriesAreTheSame(\AcceptanceTester $I, \Snapshot\Categories $snapshot)
305+
```php
306+
public function testCategoriesAreTheSame(AcceptanceTester $I, \Tests\Snapshot\Categories $snapshot)
349307
{
350308
$I->amOnPage('/categories');
351309
// if previously saved array of users does not match current set, test will fail
352310
// to update data in snapshot run test with --debug flag
353311
$snapshot->assert();
354312
}
355-
356-
{% endhighlight %}
313+
```
357314

358315
On the first run, data will be obtained via `fetchData` method and saved to `tests/_data` directory in json format.
359316
On next execution the obtained data will be compared with previously saved snapshot.
360317

361-
> To update a snapshot with a new data run tests in `--debug` mode.
318+
> To update a snapshot with new data, run tests in `--debug` mode.
362319

363320
By default Snapshot uses `assertEquals` assertion, however this can be customized by overriding `assertData` method.
364321

365-
### Failed assertion output
322+
### Failed Assertion Output
366323

367324
The assertion performed by `assertData` will not display the typical diff output from `assertEquals` or any customized failed assertion.
368325
To have the diff displayed when running tests, you can call the snapshot method `shouldShowDiffOnFail`:
369326

370-
{% highlight php %}
371-
372-
<?php
373-
public function testCategoriesAreTheSame(\AcceptanceTester $I, \Snapshot\Categories $snapshot)
327+
```php
328+
public function testCategoriesAreTheSame(AcceptanceTester $I, \Tests\Snapshot\Categories $snapshot)
374329
{
375330
$I->amOnPage('/categories');
376331
// I want to see the diff in case the snapshot data changes
377332
$snapshot->shouldShowDiffOnFail();
378333
$snapshot->assert();
379334
}
380-
381-
{% endhighlight %}
335+
```
382336

383337
If ever needed, the diff output can also be omitted by calling `shouldShowDiffOnFail(false)`.
384338

385-
### Working with different data formats
339+
### Change Storage Format
386340

387341
By default, all snapshot files are stored in json format, so if you have to work with different formats, neither the diff output or the snapshot file data will be helpful.
388342
To fix this, you can call the snapshot method `shouldSaveAsJson(false)` and set the file extension by calling `setSnapshotFileExtension()`:
389343

390-
{% highlight php %}
391-
392-
<?php
393-
public function testCategoriesAreTheSame(\AcceptanceTester $I, \Snapshot\Categories $snapshot)
344+
```php
345+
public function testCategoriesAreTheSame(AcceptanceTester $I, \Tests\Snapshot\Categories $snapshot)
394346
{
395347
// I fetch an HTML page
396348
$I->amOnPage('/categories.html');
@@ -399,16 +351,8 @@ public function testCategoriesAreTheSame(\AcceptanceTester $I, \Snapshot\Categor
399351
$snapshot->setSnapshotFileExtension('html');
400352
$snapshot->assert();
401353
}
402-
403-
{% endhighlight %}
354+
```
404355

405356
The snapshot file will be stored without encoding it to json format, and with the `.html` extension.
406357

407358
> Beware that this option will not perform any changes in the data returned by `fetchData`, and store it as it is.
408-
409-
## Conclusion
410-
411-
Codeception also assists the developer when dealing with data. Tools for database population
412-
and cleaning up are bundled within the `Db` module. If you use ORM, you can use one of the provided framework modules
413-
to operate with database through a data abstraction layer, and use the DataFactory module to generate new records with ease.
414-

0 commit comments

Comments
 (0)