Skip to content

Commit

Permalink
[feat] update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunqiang committed Dec 9, 2016
1 parent 2fad326 commit 64e1cd3
Show file tree
Hide file tree
Showing 52 changed files with 2,930 additions and 237 deletions.
15 changes: 15 additions & 0 deletions backend/codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace: backend\tests
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
modules:
config:
Yii2:
configFile: 'config/test-local.php'
9 changes: 9 additions & 0 deletions backend/config/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
return [
'id' => 'app-backend-tests',
'components' => [
'assetManager' => [
'basePath' => __DIR__ . '/../web/assets',
],
],
];
9 changes: 9 additions & 0 deletions backend/tests/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
defined('YII_APP_BASE_PATH') or define('YII_APP_BASE_PATH', __DIR__.'/../../');

require_once(YII_APP_BASE_PATH . '/vendor/autoload.php');
require_once(YII_APP_BASE_PATH . '/vendor/yiisoft/yii2/Yii.php');
require_once(YII_APP_BASE_PATH . '/common/config/bootstrap.php');
require_once(__DIR__ . '/../config/bootstrap.php');
Empty file added backend/tests/_data/.gitignore
Empty file.
13 changes: 13 additions & 0 deletions backend/tests/_data/login_data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
return [
[
'username' => 'erau',
'auth_key' => 'tUu1qHcde0diwUol3xeI-18MuHkkprQI',
// password_0
'password_hash' => '$2y$13$nJ1WDlBaGcbCdbNC5.5l4.sgy.OMEKCqtDQOdQ2OWpgiKRWYyzzne',
'password_reset_token' => 'RkD_Jw0_8HEedzLk7MM-ZKEFfYR7VbMr_1392559490',
'created_at' => '1392559490',
'updated_at' => '1392559490',
'email' => '[email protected]',
],
];
2 changes: 2 additions & 0 deletions backend/tests/_output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
1 change: 1 addition & 0 deletions backend/tests/_support/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_generated
25 changes: 25 additions & 0 deletions backend/tests/_support/FunctionalTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace backend\tests;

/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class FunctionalTester extends \Codeception\Actor
{
use _generated\FunctionalTesterActions;
/**
* Define custom actions here
*/
}
25 changes: 25 additions & 0 deletions backend/tests/_support/UnitTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace backend\tests;

/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \Codeception\Actor
{
use _generated\UnitTesterActions;
/**
* Define custom actions here
*/
}
4 changes: 4 additions & 0 deletions backend/tests/functional.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class_name: FunctionalTester
modules:
enabled:
- Yii2
36 changes: 36 additions & 0 deletions backend/tests/functional/LoginCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace backend\tests\functional;

use \backend\tests\FunctionalTester;
use common\fixtures\User as UserFixture;

/**
* Class LoginCest
*/
class LoginCest
{
public function _before(FunctionalTester $I)
{
$I->haveFixtures([
'user' => [
'class' => UserFixture::className(),
'dataFile' => codecept_data_dir() . 'login_data.php'
]
]);
}
/**
* @param FunctionalTester $I
*/
public function loginUser(FunctionalTester $I)
{
$I->amOnPage('/site/login');
$I->fillField('Username', 'erau');
$I->fillField('Password', 'password_0');
$I->click('login-button');

$I->see('Logout (erau)', 'form button[type=submit]');
$I->dontSeeLink('Login');
$I->dontSeeLink('Signup');
}
}
16 changes: 16 additions & 0 deletions backend/tests/functional/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Here you can initialize variables via \Codeception\Util\Fixtures class
* to store data in global array and use it in Cests.
*
* ```php
* // Here _bootstrap.php
* \Codeception\Util\Fixtures::add('user1', ['name' => 'davert']);
* ```
*
* In Cests
*
* ```php
* \Codeception\Util\Fixtures::get('user1');
* ```
*/
1 change: 1 addition & 0 deletions backend/tests/unit.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class_name: UnitTester
16 changes: 16 additions & 0 deletions backend/tests/unit/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Here you can initialize variables via \Codeception\Util\Fixtures class
* to store data in global array and use it in Tests.
*
* ```php
* // Here _bootstrap.php
* \Codeception\Util\Fixtures::add('user1', ['name' => 'davert']);
* ```
*
* In Tests
*
* ```php
* \Codeception\Util\Fixtures::get('user1');
* ```
*/
9 changes: 9 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# global codeception file to run tests from all apps
include:
- common
- frontend
- backend
paths:
log: console/runtime/logs
settings:
colors: true
15 changes: 15 additions & 0 deletions common/codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace: common\tests
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
modules:
config:
Yii2:
configFile: 'config/test-local.php'
11 changes: 11 additions & 0 deletions common/config/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
return [
'id' => 'app-common-tests',
'basePath' => dirname(__DIR__),
'components' => [
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
],
],
];
9 changes: 9 additions & 0 deletions common/fixtures/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace common\fixtures;

use yii\test\ActiveFixture;

class User extends ActiveFixture
{
public $modelClass = 'common\models\User';
}
9 changes: 9 additions & 0 deletions common/tests/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
defined('YII_APP_BASE_PATH') or define('YII_APP_BASE_PATH', __DIR__.'/../../');

require_once(__DIR__ . '/../../vendor/autoload.php');
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../config/bootstrap.php');

14 changes: 14 additions & 0 deletions common/tests/_data/user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
[
'username' => 'bayer.hudson',
'auth_key' => 'HP187Mvq7Mmm3CTU80dLkGmni_FUH_lR',
//password_0
'password_hash' => '$2y$13$EjaPFBnZOQsHdGuHI.xvhuDp1fHpo8hKRSk6yshqa9c5EG8s3C3lO',
'password_reset_token' => 'ExzkCOaYc1L8IOBs4wdTGGbgNiG3Wz1I_1402312317',
'created_at' => '1402312317',
'updated_at' => '1402312317',
'email' => '[email protected]',
],
];
2 changes: 2 additions & 0 deletions common/tests/_output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
1 change: 1 addition & 0 deletions common/tests/_support/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_generated
25 changes: 25 additions & 0 deletions common/tests/_support/UnitTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace common\tests;

/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \Codeception\Actor
{
use _generated\UnitTesterActions;
/**
* Define custom actions here
*/
}
6 changes: 6 additions & 0 deletions common/tests/unit.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class_name: UnitTester
bootstrap: false
modules:
enabled:
- Yii2:
part: fixtures
64 changes: 64 additions & 0 deletions common/tests/unit/models/LoginFormTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace common\tests\unit\models;

use Yii;
use common\models\LoginForm;
use common\fixtures\User as UserFixture;

/**
* Login form test
*/
class LoginFormTest extends \Codeception\Test\Unit
{
/**
* @var \frontend\tests\UnitTester
*/
protected $tester;


public function _before()
{
$this->tester->haveFixtures([
'user' => [
'class' => UserFixture::className(),
'dataFile' => codecept_data_dir() . 'user.php'
]
]);
}

public function testLoginNoUser()
{
$model = new LoginForm([
'username' => 'not_existing_username',
'password' => 'not_existing_password',
]);

expect('model should not login user', $model->login())->false();
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
}

public function testLoginWrongPassword()
{
$model = new LoginForm([
'username' => 'bayer.hudson',
'password' => 'wrong_password',
]);

expect('model should not login user', $model->login())->false();
expect('error message should be set', $model->errors)->hasKey('password');
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
}

public function testLoginCorrect()
{
$model = new LoginForm([
'username' => 'bayer.hudson',
'password' => 'password_0',
]);

expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->hasntKey('password');
expect('user should be logged in', Yii::$app->user->isGuest)->false();
}
}
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
"bocharsky-bw/file-naming-resolver": "^0.3.1"
},
"require-dev": {
"yiisoft/yii2-codeception": "~2.0.5",
"yiisoft/yii2-debug": "dev-master",
"yiisoft/yii2-gii": "dev-master",
"yiisoft/yii2-faker": "~2.0.3",
"light/yii2-generators": "~0.1.2",
"phpbu/phpbu": "2.1.*",
"light/yii2-swagger": "~1.0.0"
"light/yii2-swagger": "~1.0.0",
"codeception/base": "^2.2.3",
"codeception/verify": "~0.3.1"
},
"suggest": {
"bocharsky-bw/file-naming-resolver": "A lightweight library to resolve uploaded files and directories naming using various naming strategies"
Expand Down
Loading

0 comments on commit 64e1cd3

Please sign in to comment.