Skip to content

Commit ca4378a

Browse files
committed
Issue #5631 Improve docs for Yii2 module
1 parent 4c4135d commit ca4378a

File tree

1 file changed

+111
-54
lines changed

1 file changed

+111
-54
lines changed

src/Codeception/Module/Yii2.php

Lines changed: 111 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,62 @@
2020

2121
/**
2222
* This module provides integration with [Yii framework](http://www.yiiframework.com/) (2.0).
23-
* It initializes Yii framework in test environment and provides actions for functional testing.
23+
*
24+
* It initializes the Yii framework in a test environment and provides actions
25+
* for functional testing.
26+
*
2427
* ## Application state during testing
28+
*
2529
* This section details what you can expect when using this module.
26-
* * You will get a fresh application in `\Yii::$app` at the start of each test (available in the test and in `_before()`).
27-
* * Inside your test you may change application state; however these changes will be lost when doing a request if you have enabled `recreateApplication`.
28-
* * When executing a request via one of the request functions the `request` and `response` component are both recreated.
29-
* * After a request the whole application is available for inspection / interaction.
30-
* * You may use multiple database connections, each will use a separate transaction; to prevent accidental mistakes we
31-
* will warn you if you try to connect to the same database twice but we cannot reuse the same connection.
30+
*
31+
* * You will get a fresh application in `\Yii::$app` at the start of each test
32+
* (available in the test and in `_before()`).
33+
* * Inside your test you may change application state; however these changes
34+
* will be lost when doing a request if you have enabled `recreateApplication`.
35+
* * When executing a request via one of the request functions the `request`
36+
* and `response` component are both recreated.
37+
* * After a request the whole application is available for inspection /
38+
* interaction.
39+
* * You may use multiple database connections, each will use a separate
40+
* transaction; to prevent accidental mistakes we will warn you if you try to
41+
* connect to the same database twice but we cannot reuse the same connection.
3242
*
3343
* ## Config
3444
*
35-
* * `configFile` *required* - the path to the application config file. File should be configured for test environment and return configuration array.
45+
* * `configFile` *required* - path to the application config file. The file
46+
* should be configured for the test environment and return a configuration
47+
* array.
3648
* * `entryUrl` - initial application url (default: http://localhost/index-test.php).
37-
* * `entryScript` - front script title (like: index-test.php). If not set - taken from entryUrl.
38-
* * `transaction` - (default: true) wrap all database connection inside a transaction and roll it back after the test. Should be disabled for acceptance testing..
39-
* * `cleanup` - (default: true) cleanup fixtures after the test
40-
* * `ignoreCollidingDSN` - (default: false) When 2 database connections use the same DSN but different settings an exception will be thrown, set this to true to disable this behavior.
41-
* * `fixturesMethod` - (default: _fixtures) Name of the method used for creating fixtures.
42-
* * `responseCleanMethod` - (default: clear) Method for cleaning the response object. Note that this is only for multiple requests inside a single test case.
43-
* Between test casesthe whole application is always recreated
44-
* * `requestCleanMethod` - (default: recreate) Method for cleaning the request object. Note that this is only for multiple requests inside a single test case.
45-
* Between test cases the whole application is always recreated
46-
* * `recreateComponents` - (default: []) Some components change their state making them unsuitable for processing multiple requests. In production this is usually
47-
* not a problem since web apps tend to die and start over after each request. This allows you to list application components that need to be recreated before each request.
48-
* As a consequence, any components specified here should not be changed inside a test since those changes will get regarded.
49-
* * `recreateApplication` - (default: false) whether to recreate the whole application before each request
50-
* * `closeSessionOnRecreateApplication` - (default: true) whether to close the session in between requests inside a single test,
51-
* * if recreateApplication is set to true
52-
* You can use this module by setting params in your functional.suite.yml:
49+
* * `entryScript` - front script title (like: index-test.php). If not set it's
50+
* taken from `entryUrl`.
51+
* * `transaction` - (default: `true`) wrap all database connection inside a
52+
* transaction and roll it back after the test. Should be disabled for
53+
* acceptance testing.
54+
* * `cleanup` - (default: `true`) cleanup fixtures after the test
55+
* * `ignoreCollidingDSN` - (default: `false`) When 2 database connections use
56+
* the same DSN but different settings an exception will be thrown. Set this to
57+
* true to disable this behavior.
58+
* * `fixturesMethod` - (default: `_fixtures`) Name of the method used for
59+
* creating fixtures.
60+
* * `responseCleanMethod` - (default: `clear`) Method for cleaning the
61+
* response object. Note that this is only for multiple requests inside a
62+
* single test case. Between test cases the whole application is always
63+
* recreated.
64+
* * `requestCleanMethod` - (default: `recreate`) Method for cleaning the
65+
* request object. Note that this is only for multiple requests inside a single
66+
* test case. Between test cases the whole application is always recreated.
67+
* * `recreateComponents` - (default: `[]`) Some components change their state
68+
* making them unsuitable for processing multiple requests. In production
69+
* this is usually not a problem since web apps tend to die and start over
70+
* after each request. This allows you to list application components that
71+
* need to be recreated before each request. As a consequence, any
72+
* components specified here should not be changed inside a test since those
73+
* changes will get discarded.
74+
* * `recreateApplication` - (default: `false`) whether to recreate the whole
75+
* application before each request
76+
*
77+
* You can use this module by setting params in your `functional.suite.yml`:
78+
*
5379
* ```yaml
5480
* actor: FunctionalTester
5581
* modules:
@@ -60,9 +86,11 @@
6086
*
6187
* ### Parts
6288
*
63-
* By default all available methods are loaded, but you can specify parts to select only needed actions and avoid conflicts.
89+
* By default all available methods are loaded, but you can also use the `part`
90+
* option to select only the needed actions and to avoid conflicts. The
91+
* avilable parts are:
6492
*
65-
* * `init` - use module only for initialization (for acceptance tests).
93+
* * `init` - use the module only for initialization (for acceptance tests).
6694
* * `orm` - include only `haveRecord/grabRecord/seeRecord/dontSeeRecord` actions.
6795
* * `fixtures` - use fixtures inside tests with `haveFixtures/grabFixture/grabFixtures` actions.
6896
* * `email` - include email actions `seeEmailsIsSent/grabLastSentEmail/...`
@@ -100,24 +128,26 @@
100128
* browser: firefox
101129
* - Yii2:
102130
* configFile: 'config/test.php'
103-
* part: ORM # allow to use AR methods
131+
* part: orm # allow to use AR methods
104132
* transaction: false # don't wrap test in transaction
105133
* cleanup: false # don't cleanup the fixtures
106134
* entryScript: index-test.php
107135
* ```
108136
*
109137
* ## Fixtures
110138
*
111-
* This module allows to use [fixtures](http://www.yiiframework.com/doc-2.0/guide-test-fixtures.html) inside a test. There are two options for that.
112-
* Fixtures can be loaded using [haveFixtures](#haveFixtures) method inside a test:
139+
* This module allows to use
140+
* [fixtures](http://www.yiiframework.com/doc-2.0/guide-test-fixtures.html)
141+
* inside a test. There are two ways to do that. Fixtures can either be loaded
142+
* with the [haveFixtures](#haveFixtures) method inside a test:
113143
*
114144
* ```php
115145
* <?php
116146
* $I->haveFixtures(['posts' => PostsFixture::className()]);
117147
* ```
118148
*
119149
* or, if you need to load fixtures before the test, you
120-
* can specify fixtures with `_fixtures` method of a testcase:
150+
* can specify fixtures in the `_fixtures` method of a test case:
121151
*
122152
* ```php
123153
* <?php
@@ -129,15 +159,16 @@
129159
* ```
130160
*
131161
* ## URL
132-
* This module provide to use native URL formats of Yii2 for all codeception commands that use url for work.
133-
* This commands allows input like:
162+
*
163+
* With this module you can also use Yii2's URL format for all codeception
164+
* commands that expect a URL:
134165
*
135166
* ```php
136167
* <?php
137168
* $I->amOnPage(['site/view','page'=>'about']);
138169
* $I->amOnPage('index-test.php?site/index');
139170
* $I->amOnPage('http://localhost/index-test.php?site/index');
140-
* $I->sendAjaxPostRequest(['/user/update', 'id' => 1], ['UserForm[name]' => 'G.Hopper');
171+
* $I->sendAjaxPostRequest(['/user/update', 'id' => 1], ['UserForm[name]' => 'G.Hopper']);
141172
* ```
142173
*
143174
* ## Status
@@ -389,7 +420,7 @@ public function _parts()
389420
}
390421

391422
/**
392-
* Authorizes user on a site without submitting login form.
423+
* Authenticates a user on a site without submitting a login form.
393424
* Use it for fast pragmatic authorization in functional tests.
394425
*
395426
* ```php
@@ -401,7 +432,7 @@ public function _parts()
401432
* $admin = \app\models\User::findByUsername('admin');
402433
* $I->amLoggedInAs($admin);
403434
* ```
404-
* Requires `user` component to be enabled and configured.
435+
* Requires the `user` component to be enabled and configured.
405436
*
406437
* @param $user
407438
* @throws ModuleException
@@ -419,7 +450,7 @@ public function amLoggedInAs($user)
419450

420451
/**
421452
* Creates and loads fixtures from a config.
422-
* Signature is the same as for `fixtures()` method of `yii\test\FixtureTrait`
453+
* The signature is the same as for the `fixtures()` method of `yii\test\FixtureTrait`
423454
*
424455
* ```php
425456
* <?php
@@ -432,8 +463,10 @@ public function amLoggedInAs($user)
432463
* ]);
433464
* ```
434465
*
435-
* Note: if you need to load fixtures before the test (probably before the cleanup transaction is started;
436-
* `cleanup` options is `true` by default), you can specify fixtures with _fixtures method of a testcase
466+
* Note: if you need to load fixtures before a test (probably before the
467+
* cleanup transaction is started; `cleanup` option is `true` by default),
468+
* you can specify the fixtures in the `_fixtures()` method of a test case
469+
*
437470
* ```php
438471
* <?php
439472
* // inside Cest file or Codeception\TestCase\Unit
@@ -446,7 +479,7 @@ public function amLoggedInAs($user)
446479
* ];
447480
* }
448481
* ```
449-
* instead of defining `haveFixtures` in Cest `_before`
482+
* instead of calling `haveFixtures` in Cest `_before`
450483
*
451484
* @param $fixtures
452485
* @part fixtures
@@ -484,22 +517,23 @@ function ($fixturesStore) {
484517

485518
/**
486519
* Gets a fixture by name.
487-
* Returns a Fixture instance. If a fixture is an instance of `\yii\test\BaseActiveFixture` a second parameter
488-
* can be used to return a specific model:
520+
* Returns a Fixture instance. If a fixture is an instance of
521+
* `\yii\test\BaseActiveFixture` a second parameter can be used to return a
522+
* specific model:
489523
*
490524
* ```php
491525
* <?php
492526
* $I->haveFixtures(['users' => UserFixture::className()]);
493527
*
494528
* $users = $I->grabFixture('users');
495529
*
496-
* // get first user by key, if a fixture is instance of ActiveFixture
530+
* // get first user by key, if a fixture is an instance of ActiveFixture
497531
* $user = $I->grabFixture('users', 'user1');
498532
* ```
499533
*
500534
* @param $name
501535
* @return mixed
502-
* @throws ModuleException if a fixture is not found
536+
* @throws ModuleException if the fixture is not found
503537
* @part fixtures
504538
*/
505539
public function grabFixture($name, $index = null)
@@ -519,7 +553,7 @@ public function grabFixture($name, $index = null)
519553
}
520554

521555
/**
522-
* Inserts record into the database.
556+
* Inserts a record into the database.
523557
*
524558
* ``` php
525559
* <?php
@@ -545,7 +579,7 @@ public function haveRecord($model, $attributes = [])
545579
}
546580

547581
/**
548-
* Checks that record exists in database.
582+
* Checks that a record exists in the database.
549583
*
550584
* ``` php
551585
* $I->seeRecord('app\models\User', array('name' => 'davert'));
@@ -565,7 +599,7 @@ public function seeRecord($model, $attributes = [])
565599
}
566600

567601
/**
568-
* Checks that record does not exist in database.
602+
* Checks that a record does not exist in the database.
569603
*
570604
* ``` php
571605
* $I->dontSeeRecord('app\models\User', array('name' => 'davert'));
@@ -585,7 +619,7 @@ public function dontSeeRecord($model, $attributes = [])
585619
}
586620

587621
/**
588-
* Retrieves record from database
622+
* Retrieves a record from the database
589623
*
590624
* ``` php
591625
* $category = $I->grabRecord('app\models\User', array('name' => 'davert'));
@@ -629,19 +663,41 @@ protected function findRecord($model, $attributes = [])
629663
}
630664

631665
/**
632-
* Similar to amOnPage but accepts route as first argument and params as second
666+
* Similar to `amOnPage` but accepts a route as first argument and params as second
633667
*
634668
* ```
635669
* $I->amOnRoute('site/view', ['page' => 'about']);
636670
* ```
637671
*
672+
* @param string $route A route
673+
* @param array $params Additional route parameters
638674
*/
639675
public function amOnRoute($route, array $params = [])
640676
{
641677
array_unshift($params, $route);
642678
$this->amOnPage($params);
643679
}
644680

681+
/**
682+
* Opens the page for the given relative URI or route.
683+
*
684+
* ``` php
685+
* <?php
686+
* // opens front page
687+
* $I->amOnPage('/');
688+
* // opens /register page
689+
* $I->amOnPage('/register');
690+
* // opens customer view page for id 25
691+
* $I->amOnPage(['customer/view', 'id' => 25]);
692+
* ```
693+
*
694+
* @param string|array $page the URI or route in array format
695+
*/
696+
public function amOnPage($page)
697+
{
698+
parent::amOnPage($page);
699+
}
700+
645701
/**
646702
* To support to use the behavior of urlManager component
647703
* for the methods like this: amOnPage(), sendAjaxRequest() and etc.
@@ -660,7 +716,8 @@ protected function clientRequest($method, $uri, array $parameters = [], array $f
660716
}
661717

662718
/**
663-
* Gets a component from Yii container. Throws exception if component is not available
719+
* Gets a component from the Yii container. Throws an exception if the
720+
* component is not available
664721
*
665722
* ```php
666723
* <?php
@@ -682,7 +739,7 @@ public function grabComponent($component)
682739
}
683740

684741
/**
685-
* Checks that email is sent.
742+
* Checks that an email is sent.
686743
*
687744
* ```php
688745
* <?php
@@ -718,8 +775,8 @@ public function dontSeeEmailIsSent()
718775

719776
/**
720777
* Returns array of all sent email messages.
721-
* Each message implements `yii\mail\MessageInterface` interface.
722-
* Useful to perform additional checks using `Asserts` module:
778+
* Each message implements the `yii\mail\MessageInterface` interface.
779+
* Useful to perform additional checks using the `Asserts` module:
723780
*
724781
* ```php
725782
* <?php
@@ -742,7 +799,7 @@ public function grabSentEmails()
742799
}
743800

744801
/**
745-
* Returns last sent email:
802+
* Returns the last sent email:
746803
*
747804
* ```php
748805
* <?php
@@ -790,7 +847,7 @@ public function setCookie($name, $val, array $params = [])
790847
}
791848

792849
/**
793-
* This function creates the CSRF Cookie.
850+
* Creates the CSRF Cookie.
794851
* @param string $val The value of the CSRF token
795852
* @return string[] Returns an array containing the name of the CSRF param and the masked CSRF token.
796853
*/

0 commit comments

Comments
 (0)