diff --git a/extensions/codeception/README.md b/extensions/codeception/README.md index d065961409..777df05494 100644 --- a/extensions/codeception/README.md +++ b/extensions/codeception/README.md @@ -41,6 +41,11 @@ 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 +--------------------------- + +If you want to use codeception modules and helpers in your unit tests, you can do it like this: + ```php codeGuy->someMethodFromModule(); ``` +Codeception events +------------------ -to fire event do this: +To fire event do this: ```php 'yii\console\Application', + 'components' => [ + //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 the 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 test cases from this basic `ConsoleTestCase`. + +Reconfiguring components for testing +------------------------------------ + +You can reconfigure a component for testing, for this purpose in your `setUp` method of your test case +you can do this for example: ```php [ - 'mail' => [ - 'useFileTransport' => true, - ], - ] - ]; + + protected function setUp() + { + // don't forget to call parent method that will setup Yii application + parent::setUp(); + + Yii::$app->mail->fileTransportCallback = function ($mailer, $message) { + return 'testing_message.eml'; + }; + } + } ``` +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 + +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() + { + ... + } + +} +``` + +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: