From 67aff101027e1354efc648cabb53d1e5687f3bc9 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jan 2014 13:47:45 +0400 Subject: [PATCH 1/7] fixed test docs for codeception extension --- extensions/codeception/README.md | 104 +++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 20 deletions(-) diff --git a/extensions/codeception/README.md b/extensions/codeception/README.md index d065961409..72a036ae96 100644 --- a/extensions/codeception/README.md +++ b/extensions/codeception/README.md @@ -93,44 +93,108 @@ If you use special methods dont forget to call its parent. SomeConsoleTest extends \yii\codeception\TestCase { - // this is the config file to load as application config - public static $applicationConfig = '@app/config/web.php'; - // this defines the application class to use for mock applications - protected $applicationClass = 'yii\web\Application'; + // this is the config file to load as application config + public $appConfig = '@app/path/to/my/custom/config/for/test.php'; + } ``` -The `$applicationConfig` property may be set for all tests in a `_bootstrap.php` file like this: +The `$appConfig` property could be an array or valid alias, pointing to file that returns config array. You can sepcify +application class in the config, for example for testing console commands/features you can create `_console.php` config under +`tests/unit` folder like this: ```php - [ + //override console components if needed + ], + ] ); ``` -Don't forget that you have to include autoload and Yii class in the `_bootstrap.php` file. +and then just use your `ConsoleTestCase` like following: -You also can reconfigure some components for tests, for this purpose there is a `$config` property in the `TestCase` class. +```php + +use \yii\codeception\TestCase; + +class ConsoleTestCase extends TestCase +{ + + public $appConfig = '@tests/unit/_console.php'; + +} +``` + +You can extend other console tests cases from this basic `ConsoleTestCase`. + +You also can reconfigure some components for tests, for this purpose in your `setUp` method of your test case +you can do this for example: ```php [ - 'mail' => [ - 'useFileTransport' => true, - ], - ] - ]; + + protected function setUp() + { + //dont forget to call parent method that will setup Yii application + parent::setUp(); + + Yii::$app->mail->fileTransportCallback = function ($mailer, $message) { + return 'testing_message.eml'; + }; + } + } ``` +You dont need to worry about application instances and isolation because application will be created [each time](https://github.com/yiisoft/yii2/blob/master/extensions/codeception/TestCase.php#L31) before test. +You also can mock application in some other custom way, for this purposes you have method [`mockApplication`](https://github.com/yiisoft/yii2/blob/master/extensions/codeception/TestCase.php#L55) available in your test case, +this method will create new application instance and replace old one. Use this method when you need to create application with config that is not suitable for all other test methods in current tests case, for example: + +```php + +use \yii\codeception\TestCase; + +class SomeMyTest extends TestCase +{ + + public function testOne() + { + ... + } + + public function testTwo() + { + $this->mockApplication([ + 'language' => 'ru-RU', + 'components' => [ + 'db' => [ + //your custom configuration here + ], + ], + ]); + + //your expectations and assertions goes here + } + + public function testThree() + { + ... + } + +} + + Because of Codeception buffers all output you can't make simple `var_dump()` in the TestCase, instead you need to use `Codeception\Util\Debug::debug()` function and then run test with `--debug` key, for example: From 107eb8114b15db204b71394b184b65603a4f3f9a Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jan 2014 13:56:00 +0400 Subject: [PATCH 2/7] added titles, fixed formatting --- extensions/codeception/README.md | 39 ++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/extensions/codeception/README.md b/extensions/codeception/README.md index 72a036ae96..6cb841e338 100644 --- a/extensions/codeception/README.md +++ b/extensions/codeception/README.md @@ -41,6 +41,9 @@ and application destroy after each test. You can configure a mock application us `TestCase` is extended from `Codeception\TestCase\Case` so all methods and assertions are available. You may use codeception modules and fire events in your test, just use methods: +Getting Codeception modules +--------------------------- + ```php codeGuy->someMethodFromModule(); ``` +Codeception events +------------------ to fire event do this: @@ -68,6 +73,10 @@ public function testSomething() this event can be catched in modules and helpers. If your test is in the group, then event name will be followed by the groupname, for example ```myevent.somegroup```. + +Special test methods chain call +------------------------------- + Execution of special tests methods is (for example on ```UserTest``` class): ``` @@ -88,31 +97,33 @@ tests\unit\models\UserTest::tearDownAfterClass(); If you use special methods dont forget to call its parent. +Customizing application config +------------------------------ + ```php [ - //override console components if needed - ], - ] + require(__DIR__ . '/../../config/console.php'), + require(__DIR__ . '/../_config.php'), + [ + 'class' => 'yii\console\Application', + 'components' => [ + //override console components if needed + ], + ] ); ``` @@ -124,14 +135,15 @@ use \yii\codeception\TestCase; class ConsoleTestCase extends TestCase { - public $appConfig = '@tests/unit/_console.php'; - } ``` You can extend other console tests cases from this basic `ConsoleTestCase`. +Reconfiguring components for test +--------------------------------- + You also can reconfigure some components for tests, for this purpose in your `setUp` method of your test case you can do this for example: @@ -193,7 +205,10 @@ class SomeMyTest extends TestCase } } +``` +Additional debug output +----------------------- Because of Codeception buffers all output you can't make simple `var_dump()` in the TestCase, instead you need to use `Codeception\Util\Debug::debug()` function and then run test with `--debug` key, for example: From faaabeec5cd13e1e8b86841c315de5923215bc39 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jan 2014 18:38:01 +0400 Subject: [PATCH 3/7] docs fixed --- extensions/codeception/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/extensions/codeception/README.md b/extensions/codeception/README.md index 6cb841e338..576d528c3b 100644 --- a/extensions/codeception/README.md +++ b/extensions/codeception/README.md @@ -59,7 +59,7 @@ $this->codeGuy->someMethodFromModule(); Codeception events ------------------ -to fire event do this: +To fire event do this: ```php Date: Sun, 26 Jan 2014 18:46:54 +0400 Subject: [PATCH 4/7] typo fix --- extensions/codeception/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/codeception/README.md b/extensions/codeception/README.md index 576d528c3b..9663657bc9 100644 --- a/extensions/codeception/README.md +++ b/extensions/codeception/README.md @@ -127,7 +127,7 @@ return yii\helpers\ArrayHelper::merge( ); ``` -and then just use your `ConsoleTestCase` like following: +and then just use your `ConsoleTestCase` like the following: ```php @@ -139,7 +139,7 @@ class ConsoleTestCase extends TestCase } ``` -You can extend other console tests cases from this basic `ConsoleTestCase`. +You can extend other console test cases from this basic `ConsoleTestCase`. Reconfiguring components for test --------------------------------- From d05e91393de6f3245f376cd5398c68c48c06806f Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jan 2014 18:47:46 +0400 Subject: [PATCH 5/7] docs fix --- extensions/codeception/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/codeception/README.md b/extensions/codeception/README.md index 9663657bc9..3fe18826d4 100644 --- a/extensions/codeception/README.md +++ b/extensions/codeception/README.md @@ -141,8 +141,8 @@ class ConsoleTestCase extends TestCase You can extend other console test cases from this basic `ConsoleTestCase`. -Reconfiguring components for test ---------------------------------- +Reconfiguring components for testing +------------------------------------ You also can reconfigure some components for tests, for this purpose in your `setUp` method of your test case you can do this for example: From 2186e3fd4fa7f3f7ea4123569d992f678bc76b7f Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jan 2014 18:59:04 +0400 Subject: [PATCH 6/7] fixed docs --- extensions/codeception/README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/extensions/codeception/README.md b/extensions/codeception/README.md index 3fe18826d4..bf55857ba7 100644 --- a/extensions/codeception/README.md +++ b/extensions/codeception/README.md @@ -44,6 +44,8 @@ You may use codeception modules and fire events in your test, just use methods: Getting Codeception modules --------------------------- +If you want to use codeception modules and helpers in your unit tests, you can do it like this: + ```php mail->fileTransportCallback = function ($mailer, $message) { @@ -169,9 +171,9 @@ class MailTest extends TestCase } ``` -You dont need to worry about application instances and isolation because application will be created [each time](https://github.com/yiisoft/yii2/blob/master/extensions/codeception/TestCase.php#L31) before test. -You also can mock application in some other custom way, for this purposes you have method [`mockApplication`](https://github.com/yiisoft/yii2/blob/master/extensions/codeception/TestCase.php#L55) available in your test case, -this method will create new application instance and replace old one. Use this method when you need to create application with config that is not suitable for all other test methods in current tests case, for example: +You don't need to worry about application instances and isolation because application will be created [each time](https://github.com/yiisoft/yii2/blob/master/extensions/codeception/TestCase.php#L31) before any of test method will be executed in test case. +You can mock application in a different way. For this purposes you have method [`mockApplication`](https://github.com/yiisoft/yii2/blob/master/extensions/codeception/TestCase.php#L55) available in your test case. +This method creates new application instance and replaces old one with it and is handy when you need to create application with a config that is different from other test methods in the current test suite. For example: ```php From 1949c532aa987b1527d1668b79c58169c43a328a Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jan 2014 19:05:03 +0400 Subject: [PATCH 7/7] docs added --- extensions/codeception/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extensions/codeception/README.md b/extensions/codeception/README.md index bf55857ba7..777df05494 100644 --- a/extensions/codeception/README.md +++ b/extensions/codeception/README.md @@ -102,6 +102,8 @@ If you use special methods dont forget to call its parent. Customizing application config ------------------------------ +You may need to specify different configuration files per test cases, to do this you can make it like the following: + ```php