mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Codeception test adjustments for basic and advanced applications
- Moved everything test-related into `tests` directory. Codeception tests are in `codeception`. - Removed database module since we're using fixtures and migrations. - Moved console entry points and bootstrap into `tests/codeception/bin`. - Adjusted travis build scripts. - Adjusted documentation to be consistent and reflect changes made.
This commit is contained in:
4
apps/advanced/tests/codeception/common/.gitignore
vendored
Normal file
4
apps/advanced/tests/codeception/common/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# these files are auto generated by codeception build
|
||||
/unit/CodeGuy.php
|
||||
/functional/TestGuy.php
|
||||
/acceptance/WebGuy.php
|
16
apps/advanced/tests/codeception/common/_bootstrap.php
Normal file
16
apps/advanced/tests/codeception/common/_bootstrap.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
defined('YII_DEBUG') or define('YII_DEBUG', true);
|
||||
|
||||
defined('YII_ENV') or define('YII_ENV', 'test');
|
||||
|
||||
defined('ROOT_DIR') or define('ROOT_DIR', dirname(dirname(dirname(__DIR__))));
|
||||
|
||||
require_once(ROOT_DIR . '/vendor/autoload.php');
|
||||
require_once(ROOT_DIR . '/vendor/yiisoft/yii2/Yii.php');
|
||||
require(ROOT_DIR . '/common/config/aliases.php');
|
||||
|
||||
// set correct script paths
|
||||
$_SERVER['SERVER_NAME'] = 'localhost';
|
||||
|
||||
Yii::setAlias('@codeception', dirname(__DIR__));
|
14
apps/advanced/tests/codeception/common/_config.php
Normal file
14
apps/advanced/tests/codeception/common/_config.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* application configurations shared by all test types
|
||||
*/
|
||||
return [
|
||||
'components' => [
|
||||
'mailer' => [
|
||||
'useFileTransport' => true,
|
||||
],
|
||||
'urlManager' => [
|
||||
'showScriptName' => true,
|
||||
],
|
||||
],
|
||||
];
|
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace codeception\common\_helpers;
|
||||
|
||||
use codeception\common\fixtures\UserFixture;
|
||||
use Codeception\Module;
|
||||
use yii\test\FixtureTrait;
|
||||
|
||||
/**
|
||||
* This helper is used to populate database with needed fixtures before any tests should be run.
|
||||
* For example - populate database with demo login user that should be used in acceptance and functional tests.
|
||||
* All fixtures will be loaded before suite will be starded and unloaded after it.
|
||||
*/
|
||||
class FixtureHelper extends Module
|
||||
{
|
||||
|
||||
/**
|
||||
* Redeclare visibility because codeception includes all public methods that not starts from "_"
|
||||
* and not excluded by module settings, in guy class.
|
||||
*/
|
||||
use FixtureTrait {
|
||||
loadFixtures as protected;
|
||||
fixtures as protected;
|
||||
globalFixtures as protected;
|
||||
unloadFixtures as protected;
|
||||
getFixtures as protected;
|
||||
getFixture as protected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method called before any suite tests run. Loads User fixture login user
|
||||
* to use in acceptance and functional tests.
|
||||
* @param array $settings
|
||||
*/
|
||||
public function _beforeSuite($settings = [])
|
||||
{
|
||||
$this->loadFixtures();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method is called after all suite tests run
|
||||
*/
|
||||
public function _afterSuite()
|
||||
{
|
||||
$this->unloadFixtures();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function fixtures()
|
||||
{
|
||||
return [
|
||||
'user' => [
|
||||
'class' => UserFixture::className(),
|
||||
'dataFile' => '@codeception/common/fixtures/data/init_login.php',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
2
apps/advanced/tests/codeception/common/_log/.gitignore
vendored
Normal file
2
apps/advanced/tests/codeception/common/_log/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
21
apps/advanced/tests/codeception/common/_pages/LoginPage.php
Normal file
21
apps/advanced/tests/codeception/common/_pages/LoginPage.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace codeception\common\_pages;
|
||||
|
||||
use yii\codeception\BasePage;
|
||||
|
||||
class LoginPage extends BasePage
|
||||
{
|
||||
public $route = 'site/login';
|
||||
|
||||
/**
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*/
|
||||
public function login($username, $password)
|
||||
{
|
||||
$this->guy->fillField('input[name="LoginForm[username]"]', $username);
|
||||
$this->guy->fillField('input[name="LoginForm[password]"]', $password);
|
||||
$this->guy->click('login-button');
|
||||
}
|
||||
}
|
13
apps/advanced/tests/codeception/common/codeception.yml
Normal file
13
apps/advanced/tests/codeception/common/codeception.yml
Normal file
@ -0,0 +1,13 @@
|
||||
namespace: codeception_common
|
||||
actor: Tester
|
||||
paths:
|
||||
tests: .
|
||||
log: _log
|
||||
data: _data
|
||||
helpers: _helpers
|
||||
settings:
|
||||
bootstrap: _bootstrap.php
|
||||
suite_class: \PHPUnit_Framework_TestSuite
|
||||
colors: true
|
||||
memory_limit: 1024M
|
||||
log: true
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace codeception\common\fixtures;
|
||||
|
||||
use yii\test\ActiveFixture;
|
||||
|
||||
class UserFixture extends ActiveFixture
|
||||
{
|
||||
public $modelClass = 'common\models\User';
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<?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' => 'sfriesen@jenkins.info',
|
||||
],
|
||||
];
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* @var $faker \Faker\Generator
|
||||
* @var $index integer
|
||||
*/
|
||||
|
||||
$security = Yii::$app->getSecurity();
|
||||
|
||||
return [
|
||||
'username' => $faker->userName,
|
||||
'email' => $faker->email,
|
||||
'auth_key' => $security->generateRandomString(),
|
||||
'password_hash' => $security->generatePasswordHash('password_' . $index),
|
||||
'password_reset_token' => $security->generateRandomString() . '_' . time(),
|
||||
'created_at' => time(),
|
||||
'updated_at' => time(),
|
||||
];
|
6
apps/advanced/tests/codeception/common/unit.suite.yml
Normal file
6
apps/advanced/tests/codeception/common/unit.suite.yml
Normal file
@ -0,0 +1,6 @@
|
||||
# Codeception Test Suite Configuration
|
||||
|
||||
# suite for unit (internal) tests.
|
||||
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
|
||||
|
||||
class_name: CodeGuy
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace codeception\common\unit;
|
||||
|
||||
class DbTestCase extends \yii\codeception\DbTestCase
|
||||
{
|
||||
public $appConfig = '@codeception/common/unit/_config.php';
|
||||
}
|
8
apps/advanced/tests/codeception/common/unit/TestCase.php
Normal file
8
apps/advanced/tests/codeception/common/unit/TestCase.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace common\tests\unit;
|
||||
|
||||
class TestCase extends \yii\codeception\TestCase
|
||||
{
|
||||
public $appConfig = '@common/tests/unit/_config.php';
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
// Here you can initialize variables that will for your tests
|
16
apps/advanced/tests/codeception/common/unit/_config.php
Normal file
16
apps/advanced/tests/codeception/common/unit/_config.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
return yii\helpers\ArrayHelper::merge(
|
||||
require(ROOT_DIR . '/common/config/main.php'),
|
||||
require(ROOT_DIR . '/common/config/main-local.php'),
|
||||
require(__DIR__ . '/../_config.php'),
|
||||
[
|
||||
'components' => [
|
||||
'db' => [
|
||||
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit',
|
||||
],
|
||||
],
|
||||
'id' => 'app-common',
|
||||
'basePath' => dirname(__DIR__),
|
||||
]
|
||||
);
|
@ -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' => 'nicole.paucek@schultz.info',
|
||||
],
|
||||
];
|
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace codeception\common\unit\models;
|
||||
|
||||
use Yii;
|
||||
use codeception\common\unit\DbTestCase;
|
||||
use Codeception\Specify;
|
||||
use common\models\LoginForm;
|
||||
use codeception\common\fixtures\UserFixture;
|
||||
|
||||
class LoginFormTest extends DbTestCase
|
||||
{
|
||||
|
||||
use Specify;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Yii::configure(Yii::$app, [
|
||||
'components' => [
|
||||
'user' => [
|
||||
'class' => 'yii\web\User',
|
||||
'identityClass' => 'common\models\User',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
Yii::$app->user->logout();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testLoginNoUser()
|
||||
{
|
||||
$model = new LoginForm([
|
||||
'username' => 'not_existing_username',
|
||||
'password' => 'not_existing_password',
|
||||
]);
|
||||
|
||||
$this->specify('user should not be able to login, when there is no identity', function () use ($model) {
|
||||
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',
|
||||
]);
|
||||
|
||||
$this->specify('user should not be able to login with wrong password', function () use ($model) {
|
||||
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',
|
||||
]);
|
||||
|
||||
$this->specify('user should be able to login with correct credentials', function () use ($model) {
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
public function fixtures()
|
||||
{
|
||||
return [
|
||||
'user' => [
|
||||
'class' => UserFixture::className(),
|
||||
'dataFile' => '@codeception/common/unit/fixtures/data/models/user.php'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user