README.md 3.7 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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
- a base class for unit-tests: `yii\codeception\TestCase
- a base class for codeception page-objects: `yii\codeception\BasePage`.
- a solution for testing emails


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

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

Either run

```
php composer.phar require yiisoft/yii2-codeception "*"
```

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.
`TestCase` is extended from `PHPUnit_Framework_TestCase` so all methods and assertions are available.
43 44

```php
45 46 47
<?php

SomeConsoleTest extends \yii\codeception\TestCase
48
{
49 50
	// this is the config file to load as application config
	public static $applicationConfig = '@app/config/web.php';
51

52 53
	// this defines the application class to use for mock applications
	protected $applicationClass = 'yii\web\Application';
54 55 56
}
```

57 58 59 60 61 62 63 64 65 66 67 68 69 70
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.
71 72

```php
73 74 75
<?php

SomeOtherTest extends \yii\codeception\TestCase
76 77 78 79 80 81 82 83 84 85 86
{
	public $config = [
		'components' => [
			'mail' => [
				'useFileTransport' => true,
			],
		]
	];
}
```

87 88
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:
89 90

```php
91
<?php
92

93
use Codeception\Util\Debug;
94

95
SomeDebugTest extends \yii\codeception\TestCase
96 97 98
{
	public function testSmth()
	{
99
		Debug::debug('some string');
100 101 102 103 104 105 106
		Debug::debug($someArray);
		Debug::debug($someObject);
	}

}
```

107
Then run command `php codecept.phar run --debug unit/SomeDebugTest` and you will see in output:
108 109

```html
110
  some string
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

  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] => 
  )

```

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