Skip to content

Commit

Permalink
refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Jul 16, 2016
1 parent 4f8f207 commit 0ef627f
Show file tree
Hide file tree
Showing 55 changed files with 1,688 additions and 1,511 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -26,4 +26,5 @@ phpunit.phar
# local phpunit config
/phpunit.xml

tests/_output/*
tests/_output/*
tests/_support/_generated
80 changes: 80 additions & 0 deletions README.md
Expand Up @@ -100,3 +100,83 @@ return [
- Yii won't create the database for you, this has to be done manually before you can access it.
- Check and edit the other files in the `config/` directory to customize your application as required.
- Refer to the README in the `tests` directory for information specific to basic application tests.



TESTING
-------

Tests are located in `tests` directory, developed with [Codeception PHP Testing Framework](http://codeception.com/).
By default there are 3 test suites: `unit`, `functional` and `acceptance`. Tests can be executed by running

```
composer exec codecept run
```

This will execute unit and functional tests. Unit tests are testing the system components, while functional tests are for testing user interaction.
Acceptance tests are disabled by default as they require additional setup as they perform testing in real browser.

To execute acceptance tests do the following:

1. Rename `tests/acceptance.suite.yml.example` to `tests/acceptance.suite.yml` to enable suite configuration

2. Replace `codeception/base` package in `composer.json` with `codeception/codeception` to install full featured version of Codeception.

3. Update dependencies with Composer

```
composer update
```

4. Download [Selenium Server](http://www.seleniumhq.org/download/) and launch it:

```
java -jar java -jar ~/selenium-server-standalone-x.xx.x.jar
```

5. (Optional) Create `yii2_basic_tests` database and update it by applying migrations if you have them.

```
tests/bin/yii migrate
```

The database configuration can be found at `config/test_db.php`.


6. Start web server:

```
tests/bin/yii serve
```

7. Now you can run all available tests

```
# run all available tests
composer exec codecept run
# run acceptance tests
composer exec codecept run acceptance
# run only unit and functional tests
composer exec codecept run unit,functional
```

Code coverage support
---------------------

By default, code coverage is disabled in `codeception.yml` configuration file, you should uncomment needed rows to be able
to collect code coverage. You can run your tests and collect coverage with the following command:

```
#collect coverage for all tests
composer exec codecept run --coverage-html --coverage-xml
#collect coverage only for unit tests
composer exec codecept run unit --coverage-html --coverage-xml
#collect coverage for unit and functional tests
composer exec codecept run functional,unit --coverage-html --coverage-xml
```

You can see code coverage output under the `tests/_output` directory.
25 changes: 15 additions & 10 deletions codeception.yml
@@ -1,4 +1,18 @@
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
memory_limit: 1024M
colors: true
modules:
config:
Yii2:
configFile: 'config/test.php'

# To enable code coverage:
#coverage:
# #c3_url: http://localhost:8080/index-test.php/
Expand All @@ -19,13 +33,4 @@ actor: Tester
# - ../vendor/*
# - ../views/*
# - ../web/*
# - ../tests/*
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
memory_limit: 1024M
colors: true
# - ../tests/*
3 changes: 1 addition & 2 deletions composer.json
Expand Up @@ -20,8 +20,7 @@
"yiisoft/yii2-swiftmailer": "*"
},
"require-dev": {
"yiisoft/yii2-codeception": "*",
"codeception/specify": "*",
"codeception/base": "^2.2.3",
"codeception/verify": "*",
"yiisoft/yii2-debug": "*",
"yiisoft/yii2-gii": "*",
Expand Down
2 changes: 0 additions & 2 deletions config/console.php
@@ -1,7 +1,5 @@
<?php

Yii::setAlias('@tests', dirname(__DIR__) . '/tests/codeception');

$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');

Expand Down
10 changes: 5 additions & 5 deletions config/test.php
@@ -1,9 +1,6 @@
<?php
$params = require(__DIR__ . '/params.php');
$dbParams = require(__DIR__ . '/db.php');

// test database! Important not to run tests on production or development databases
$dbParams['dsn'] = 'mysql:host=localhost;dbname=yii2_basic_tests';
$dbParams = require(__DIR__ . '/test_db.php');

/**
* Application configuration shared by all test types
Expand All @@ -20,8 +17,11 @@
'urlManager' => [
'showScriptName' => true,
],
'user' => [
'identityClass' => 'app\models\User',
],
'request' => [
// it's not recommended to run functional tests with CSRF validation enabled
'cookieValidationKey' => 'test',
'enableCsrfValidation' => false,
// but if you absolutely need it set cookie domain to localhost
/*
Expand Down
6 changes: 6 additions & 0 deletions config/test_db.php
@@ -0,0 +1,6 @@
<?php
$db = require(__DIR__ . '/db.php');
// test database! Important not to run tests on production or development databases
$db['dsn'] = 'mysql:host=localhost;dbname=yii2_basic_tests';

return $db;
128 changes: 0 additions & 128 deletions tests/README.md

This file was deleted.

8 changes: 3 additions & 5 deletions tests/_bootstrap.php
@@ -1,10 +1,8 @@
<?php
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

define('YII_ENV', 'test');
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');

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

\Codeception\Specify\Config::setDeepClone(false);
File renamed without changes.
14 changes: 0 additions & 14 deletions tests/_pages/AboutPage.php

This file was deleted.

26 changes: 0 additions & 26 deletions tests/_pages/ContactPage.php

This file was deleted.

25 changes: 0 additions & 25 deletions tests/_pages/LoginPage.php

This file was deleted.

3 changes: 0 additions & 3 deletions tests/_support/FunctionalTester.php
Expand Up @@ -20,7 +20,4 @@ class FunctionalTester extends \Codeception\Actor
{
use _generated\FunctionalTesterActions;

/**
* Define custom actions here
*/
}

0 comments on commit 0ef627f

Please sign in to comment.