README.md 4.74 KB
Newer Older
1 2 3
Codeception Extension for Yii 2
===============================

4
This extension provides [Codeception](http://codeception.com/) integration for the Yii Framework 2.0.
5

6
It provides classes that help with testing with codeception:
7

8
- a base class for unit-tests: `yii\codeception\TestCase`;
9 10 11 12 13 14 15 16 17 18 19
- a base class for codeception page-objects: `yii\codeception\BasePage`.


Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
20
php composer.phar require --prefer-dist yiisoft/yii2-codeception "*"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
```

or add

```json
"yiisoft/yii2-codeception": "*"
```

to the require section of your composer.json.


Usage
-----

When using codeception page-objects they have some similar code, this code was extracted and put into the `BasePage`
class to reduce code duplication. Simply extend your page object from this class, like it is done in `yii2-app-basic` and
`yii2-app-advanced` boilerplates.

For unit testing there is a `TestCase` class which holds some common features like application creation before each test
and application destroy after each test. You can configure a mock application using this class.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
`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:

```php
<?php
#in your unit-test
$this->getModule('CodeHelper'); #or some other module
```

You also can use all guy methods by accessing guy instance like:

```php
<?php
$this->codeGuy->someMethodFromModule();
```

to fire event do this:

```php
<?php
use Codeception\Event\TestEvent;

public function testSomething()
{
	$this->fire('myevent', new TestEvent($this));
}
```
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```.

Execution of special tests methods is (for example on ```UserTest``` class):

```
tests\unit\models\UserTest::setUpBeforeClass();

	tests\unit\models\UserTest::_before();

		tests\unit\models\UserTest::setUp();

			tests\unit\models\UserTest::testSomething();

		tests\unit\models\UserTest::tearDown();

	tests\unit\models\UserTest::_after();

tests\unit\models\UserTest::tearDownAfterClass();
```

If you use special methods dont forget to call its parent.
90 91

```php
92 93 94
<?php

SomeConsoleTest extends \yii\codeception\TestCase
95
{
96 97
	// this is the config file to load as application config
	public static $applicationConfig = '@app/config/web.php';
98

99 100
	// this defines the application class to use for mock applications
	protected $applicationClass = 'yii\web\Application';
101 102 103
}
```

104 105 106 107 108 109 110 111 112 113 114 115 116 117
The `$applicationConfig` property may be set for all tests in a `_bootstrap.php` file like this:

```php
<?php

yii\codeception\TestCase::$applicationConfig = yii\helpers\ArrayHelper::merge(
	require(__DIR__ . '/../../config/web.php'),
	require(__DIR__ . '/../../config/codeception/unit.php')
);
```

Don't forget that you have to include autoload and Yii class in the `_bootstrap.php` file.

You also can reconfigure some components for tests, for this purpose there is a `$config` property in the `TestCase` class.
118 119

```php
120 121 122
<?php

SomeOtherTest extends \yii\codeception\TestCase
123 124 125 126 127 128 129 130 131 132 133
{
	public $config = [
		'components' => [
			'mail' => [
				'useFileTransport' => true,
			],
		]
	];
}
```

134 135
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:
136 137

```php
138
<?php
139

140
use Codeception\Util\Debug;
141

142
SomeDebugTest extends \yii\codeception\TestCase
143 144 145
{
	public function testSmth()
	{
146
		Debug::debug('some string');
147 148 149 150 151 152 153
		Debug::debug($someArray);
		Debug::debug($someObject);
	}

}
```

154
Then run command `php codecept.phar run --debug unit/SomeDebugTest` and you will see in output:
155 156

```html
157
  some string
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

  Array
  (
      [0] => 1
      [1] => 2
      [2] => 3
      [3] => 4
      [4] => 5
  )
  
  yii\web\User Object
  (
      [identityClass] => app\models\User
      [enableAutoLogin] => 
      [loginUrl] => Array
          (
              [0] => site/login
          )
  
      [identityCookie] => Array
          (
              [name] => _identity
              [httpOnly] => 1
          )
  
      [authTimeout] => 
      [autoRenewCookie] => 1
      [idVar] => __id
      [authTimeoutVar] => __expire
      [returnUrlVar] => __returnUrl
      [_access:yii\web\User:private] => Array
          (
          )
  
      [_identity:yii\web\User:private] => 
      [_events:yii\base\Component:private] => 
      [_behaviors:yii\base\Component:private] => 
  )

```

199
For further instructions refer to the testing section in the [Yii Definitive Guide](https://github.com/yiisoft/yii2/blob/master/docs/guide/testing.md).