You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. Codeception Extension for Yii 2
  2. ===============================
  3. This extension provides [Codeception](http://codeception.com/) integration for the Yii Framework 2.0.
  4. It provides classes that help with testing with codeception:
  5. - a base class for unit-tests: `yii\codeception\TestCase`;
  6. - a base class for codeception page-objects: `yii\codeception\BasePage`.
  7. This repository is a git submodule of <https://github.com/yiisoft/yii2>.
  8. Please submit issue reports and pull requests to the main repository.
  9. For license information check the [LICENSE](LICENSE.md)-file.
  10. Installation
  11. ------------
  12. The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
  13. Either run
  14. ```
  15. php composer.phar require --prefer-dist yiisoft/yii2-codeception
  16. ```
  17. or add
  18. ```json
  19. "yiisoft/yii2-codeception": "~2.0.0"
  20. ```
  21. to the require section of your composer.json.
  22. Usage
  23. -----
  24. When using codeception page-objects they have some similar code, this code was extracted and put into the `BasePage`
  25. class to reduce code duplication. Simply extend your page object from this class, like it is done in `yii2-app-basic` and
  26. `yii2-app-advanced` boilerplates.
  27. For unit testing there is a `TestCase` class which holds some common features like application creation before each test
  28. and application destroy after each test. You can configure a mock application using this class.
  29. `TestCase` is extended from `Codeception\TestCase\Case` so all methods and assertions are available.
  30. You may use codeception modules and fire events in your test, just use methods:
  31. Getting Codeception modules
  32. ---------------------------
  33. If you want to use codeception modules and helpers in your unit tests, you can do it like this:
  34. ```php
  35. <?php
  36. #in your unit-test
  37. $this->getModule('CodeHelper'); #or some other module
  38. ```
  39. You also can use all actor methods by accessing actor instance like:
  40. ```php
  41. <?php
  42. $this->unitTester->someMethodFromModule();
  43. ```
  44. Codeception events
  45. ------------------
  46. To fire event do this:
  47. ```php
  48. <?php
  49. use Codeception\Event\TestEvent;
  50. public function testSomething()
  51. {
  52. $this->fire('myevent', new TestEvent($this));
  53. }
  54. ```
  55. 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,
  56. for example ```myevent.somegroup```.
  57. Special test method chaining
  58. ----------------------------
  59. Execution of special tests methods is (for example on ```UserTest``` class):
  60. ```
  61. tests\unit\models\UserTest::setUpBeforeClass();
  62. tests\unit\models\UserTest::_before();
  63. tests\unit\models\UserTest::setUp();
  64. tests\unit\models\UserTest::testSomething();
  65. tests\unit\models\UserTest::tearDown();
  66. tests\unit\models\UserTest::_after();
  67. tests\unit\models\UserTest::tearDownAfterClass();
  68. ```
  69. If you use special methods dont forget to call its parent.
  70. Customizing application config
  71. ------------------------------
  72. You may need to specify different configuration files per test cases, to do this you can make it like the following:
  73. ```php
  74. <?php
  75. SomeConsoleTest extends \yii\codeception\TestCase
  76. {
  77. // this is the config file to load as application config
  78. public $appConfig = '@app/path/to/my/custom/config/for/test.php';
  79. }
  80. ```
  81. The `$appConfig` property could be an array or a valid alias, pointing to the file that returns a config array. You can specify
  82. application class in the config, for example for testing console commands or features you can create `_console.php` config under
  83. `tests/unit` directory like this:
  84. ```php
  85. return yii\helpers\ArrayHelper::merge(
  86. require(__DIR__ . '/../../config/console.php'),
  87. require(__DIR__ . '/../_config.php'),
  88. [
  89. 'class' => 'yii\console\Application',
  90. 'components' => [
  91. //override console components if needed
  92. ],
  93. ]
  94. );
  95. ```
  96. and then just use your `ConsoleTestCase` like the following:
  97. ```php
  98. use \yii\codeception\TestCase;
  99. class ConsoleTestCase extends TestCase
  100. {
  101. public $appConfig = '@tests/unit/_console.php';
  102. }
  103. ```
  104. You can extend other console test cases from this basic `ConsoleTestCase`.
  105. Reconfiguring components for testing
  106. ------------------------------------
  107. You can reconfigure a component for testing, for this purpose in your `setUp` method of your test case
  108. you can do this for example:
  109. ```php
  110. <?php
  111. use \yii\codeception\TestCase;
  112. use Yii;
  113. class MailTest extends TestCase
  114. {
  115. protected function setUp()
  116. {
  117. // don't forget to call parent method that will setup Yii application
  118. parent::setUp();
  119. Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) {
  120. return 'testing_message.eml';
  121. };
  122. }
  123. }
  124. ```
  125. > **Tip**: You also can reconfigure Yii components and properties with method `Yii::configure()`.
  126. 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.
  127. 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.
  128. 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:
  129. ```php
  130. use \yii\codeception\TestCase;
  131. class SomeMyTest extends TestCase
  132. {
  133. public function testOne()
  134. {
  135. ...
  136. }
  137. public function testTwo()
  138. {
  139. $this->mockApplication([
  140. 'language' => 'ru-RU',
  141. 'components' => [
  142. 'db' => [
  143. //your custom configuration here
  144. ],
  145. ],
  146. ]);
  147. //your expectations and assertions goes here
  148. }
  149. public function testThree()
  150. {
  151. ...
  152. }
  153. }
  154. ```
  155. Additional debug output
  156. -----------------------
  157. Because of Codeception buffers all output you can't make simple `var_dump()` in the TestCase, instead you need to use
  158. `Codeception\Util\Debug::debug()` function and then run test with `--debug` key, for example:
  159. ```php
  160. <?php
  161. use Codeception\Util\Debug;
  162. SomeDebugTest extends \yii\codeception\TestCase
  163. {
  164. public function testSmth()
  165. {
  166. Debug::debug('some string');
  167. Debug::debug($someArray);
  168. Debug::debug($someObject);
  169. }
  170. }
  171. ```
  172. Then run command `php codecept.phar run --debug unit/SomeDebugTest` and you will see in output:
  173. ```html
  174. some string
  175. Array
  176. (
  177. [0] => 1
  178. [1] => 2
  179. [2] => 3
  180. [3] => 4
  181. [4] => 5
  182. )
  183. yii\web\User Object
  184. (
  185. [identityClass] => app\models\User
  186. [enableAutoLogin] =>
  187. [loginUrl] => Array
  188. (
  189. [0] => site/login
  190. )
  191. [identityCookie] => Array
  192. (
  193. [name] => _identity
  194. [httpOnly] => 1
  195. )
  196. [authTimeout] =>
  197. [autoRenewCookie] => 1
  198. [idParam] => __id
  199. [authTimeoutParam] => __expire
  200. [returnUrlParam] => __returnUrl
  201. [_access:yii\web\User:private] => Array
  202. (
  203. )
  204. [_identity:yii\web\User:private] =>
  205. [_events:yii\base\Component:private] =>
  206. [_behaviors:yii\base\Component:private] =>
  207. )
  208. ```
  209. For further instructions refer to the testing section in the [Yii Definitive Guide](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-overview.md).