Skip to content

Commit 0ef627f

Browse files
committed
refactored tests
1 parent 4f8f207 commit 0ef627f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1688
-1511
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ phpunit.phar
2626
# local phpunit config
2727
/phpunit.xml
2828

29-
tests/_output/*
29+
tests/_output/*
30+
tests/_support/_generated

README.md

+80
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,83 @@ return [
100100
- Yii won't create the database for you, this has to be done manually before you can access it.
101101
- Check and edit the other files in the `config/` directory to customize your application as required.
102102
- Refer to the README in the `tests` directory for information specific to basic application tests.
103+
104+
105+
106+
TESTING
107+
-------
108+
109+
Tests are located in `tests` directory, developed with [Codeception PHP Testing Framework](http://codeception.com/).
110+
By default there are 3 test suites: `unit`, `functional` and `acceptance`. Tests can be executed by running
111+
112+
```
113+
composer exec codecept run
114+
```
115+
116+
This will execute unit and functional tests. Unit tests are testing the system components, while functional tests are for testing user interaction.
117+
Acceptance tests are disabled by default as they require additional setup as they perform testing in real browser.
118+
119+
To execute acceptance tests do the following:
120+
121+
1. Rename `tests/acceptance.suite.yml.example` to `tests/acceptance.suite.yml` to enable suite configuration
122+
123+
2. Replace `codeception/base` package in `composer.json` with `codeception/codeception` to install full featured version of Codeception.
124+
125+
3. Update dependencies with Composer
126+
127+
```
128+
composer update
129+
```
130+
131+
4. Download [Selenium Server](http://www.seleniumhq.org/download/) and launch it:
132+
133+
```
134+
java -jar java -jar ~/selenium-server-standalone-x.xx.x.jar
135+
```
136+
137+
5. (Optional) Create `yii2_basic_tests` database and update it by applying migrations if you have them.
138+
139+
```
140+
tests/bin/yii migrate
141+
```
142+
143+
The database configuration can be found at `config/test_db.php`.
144+
145+
146+
6. Start web server:
147+
148+
```
149+
tests/bin/yii serve
150+
```
151+
152+
7. Now you can run all available tests
153+
154+
```
155+
# run all available tests
156+
composer exec codecept run
157+
158+
# run acceptance tests
159+
composer exec codecept run acceptance
160+
161+
# run only unit and functional tests
162+
composer exec codecept run unit,functional
163+
```
164+
165+
Code coverage support
166+
---------------------
167+
168+
By default, code coverage is disabled in `codeception.yml` configuration file, you should uncomment needed rows to be able
169+
to collect code coverage. You can run your tests and collect coverage with the following command:
170+
171+
```
172+
#collect coverage for all tests
173+
composer exec codecept run --coverage-html --coverage-xml
174+
175+
#collect coverage only for unit tests
176+
composer exec codecept run unit --coverage-html --coverage-xml
177+
178+
#collect coverage for unit and functional tests
179+
composer exec codecept run functional,unit --coverage-html --coverage-xml
180+
```
181+
182+
You can see code coverage output under the `tests/_output` directory.

codeception.yml

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
helpers: tests/_support
7+
settings:
8+
bootstrap: _bootstrap.php
9+
memory_limit: 1024M
10+
colors: true
11+
modules:
12+
config:
13+
Yii2:
14+
configFile: 'config/test.php'
15+
216
# To enable code coverage:
317
#coverage:
418
# #c3_url: http://localhost:8080/index-test.php/
@@ -19,13 +33,4 @@ actor: Tester
1933
# - ../vendor/*
2034
# - ../views/*
2135
# - ../web/*
22-
# - ../tests/*
23-
paths:
24-
tests: tests
25-
log: tests/_output
26-
data: tests/_data
27-
helpers: tests/_support
28-
settings:
29-
bootstrap: _bootstrap.php
30-
memory_limit: 1024M
31-
colors: true
36+
# - ../tests/*

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
"yiisoft/yii2-swiftmailer": "*"
2121
},
2222
"require-dev": {
23-
"yiisoft/yii2-codeception": "*",
24-
"codeception/specify": "*",
23+
"codeception/base": "^2.2.3",
2524
"codeception/verify": "*",
2625
"yiisoft/yii2-debug": "*",
2726
"yiisoft/yii2-gii": "*",

config/console.php

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
Yii::setAlias('@tests', dirname(__DIR__) . '/tests/codeception');
4-
53
$params = require(__DIR__ . '/params.php');
64
$db = require(__DIR__ . '/db.php');
75

config/test.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?php
22
$params = require(__DIR__ . '/params.php');
3-
$dbParams = require(__DIR__ . '/db.php');
4-
5-
// test database! Important not to run tests on production or development databases
6-
$dbParams['dsn'] = 'mysql:host=localhost;dbname=yii2_basic_tests';
3+
$dbParams = require(__DIR__ . '/test_db.php');
74

85
/**
96
* Application configuration shared by all test types
@@ -20,8 +17,11 @@
2017
'urlManager' => [
2118
'showScriptName' => true,
2219
],
20+
'user' => [
21+
'identityClass' => 'app\models\User',
22+
],
2323
'request' => [
24-
// it's not recommended to run functional tests with CSRF validation enabled
24+
'cookieValidationKey' => 'test',
2525
'enableCsrfValidation' => false,
2626
// but if you absolutely need it set cookie domain to localhost
2727
/*

config/test_db.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
$db = require(__DIR__ . '/db.php');
3+
// test database! Important not to run tests on production or development databases
4+
$db['dsn'] = 'mysql:host=localhost;dbname=yii2_basic_tests';
5+
6+
return $db;

tests/README.md

-128
This file was deleted.

tests/_bootstrap.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
2-
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
3-
2+
define('YII_ENV', 'test');
43
defined('YII_DEBUG') or define('YII_DEBUG', true);
5-
defined('YII_ENV') or define('YII_ENV', 'test');
64

7-
$_SERVER['SCRIPT_FILENAME'] = codecept_root_dir() . '/web/index.php';
8-
$_SERVER['SCRIPT_NAME'] = 'http://localhost:8080/index-test.php';
5+
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
6+
require __DIR__ .'/../vendor/autoload.php';
97

108
\Codeception\Specify\Config::setDeepClone(false);
File renamed without changes.

tests/_pages/AboutPage.php

-14
This file was deleted.

tests/_pages/ContactPage.php

-26
This file was deleted.

tests/_pages/LoginPage.php

-25
This file was deleted.

tests/_support/FunctionalTester.php

-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,4 @@ class FunctionalTester extends \Codeception\Actor
2020
{
2121
use _generated\FunctionalTesterActions;
2222

23-
/**
24-
* Define custom actions here
25-
*/
2623
}

0 commit comments

Comments
 (0)