Commit 564fdf0b by Alexander Makarov

Merge pull request #3753 from Ragazzo/application_templates_tests_fixed

Application templates tests fixed
parents a967dbe9 3c8cdd99
......@@ -100,7 +100,7 @@ TESTING
-------
Install additional composer packages:
* `php composer.phar require --dev "codeception/codeception: 1.8.*@dev" "codeception/specify: *" "codeception/verify: *"`
* `php composer.phar require --dev "codeception/codeception: 1.8.*@dev" "codeception/specify: *" "codeception/verify: *" "yiisoft/yii2-faker: *"`
This application boilerplate use database in testing, so you should create three databases that are used in tests:
* `yii2_advanced_unit` - database for unit tests;
......@@ -124,3 +124,4 @@ After that is done you should be able to run your tests, for example to run `fro
In similar way you can run tests for other application tiers - `backend`, `console`, `common`.
You also can adjust you application suite configs and `_bootstrap.php` settings to use other urls and files, as it is can be done in `yii2-basic`.
Current template also includes [yii2-faker](https://github.com/yiisoft/yii2/tree/master/extensions/faker) extension, that is correctly setup for each application tier.
......@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge(
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit',
],
],
'controllerMap' => [
'fixture' => [
'class' => 'yii\faker\FixtureController',
'fixtureDataPath' => '@backend/tests/unit/fixtures/data',
'templatePath' => '@commmon/tests/templates/fixtures'
],
],
]
);
......@@ -4,5 +4,5 @@ namespace common\tests\unit;
class DbTestCase extends \yii\codeception\DbTestCase
{
public $appConfig = '@frontend/tests/unit/_config.php';
public $appConfig = '@common/tests/unit/_config.php';
}
......@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge(
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit',
],
],
'controllerMap' => [
'fixture' => [
'class' => 'yii\faker\FixtureController',
'fixtureDataPath' => '@common/tests/unit/fixtures/data',
'templatePath' => '@common/tests/templates/fixtures'
],
],
]
);
<?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',
],
];
......@@ -3,14 +3,29 @@
namespace common\tests\unit\models;
use Yii;
use frontend\tests\unit\TestCase;
use common\models\User;
use yii\helpers\Security;
use common\tests\unit\DbTestCase;
use Codeception\Specify;
use common\models\LoginForm;
use common\tests\fixtures\UserFixture;
class LoginFormTest extends TestCase
class LoginFormTest extends DbTestCase
{
use \Codeception\Specify;
use Specify;
public function setUp()
{
parent::setUp();
Yii::configure(Yii::$app, [
'components' => [
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
],
],
]);
}
protected function tearDown()
{
......@@ -20,10 +35,10 @@ class LoginFormTest extends TestCase
public function testLoginNoUser()
{
$model = $this->mockUser(null);
$model->username = 'some_username';
$model->password = 'some_password';
$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();
......@@ -33,10 +48,10 @@ class LoginFormTest extends TestCase
public function testLoginWrongPassword()
{
$model = $this->mockUser(new User(['password_hash' => Security::generatePasswordHash('will-not-match')]));
$model->username = 'demo';
$model->password = 'wrong-password';
$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();
......@@ -47,10 +62,11 @@ class LoginFormTest extends TestCase
public function testLoginCorrect()
{
$model = $this->mockUser(new User(['password_hash' => Security::generatePasswordHash('demo')]));
$model->username = 'demo';
$model->password = 'demo';
$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();
......@@ -59,11 +75,14 @@ class LoginFormTest extends TestCase
});
}
private function mockUser($user)
public function fixtures()
{
$loginForm = $this->getMock('common\models\LoginForm', ['getUser']);
$loginForm->expects($this->any())->method('getUser')->will($this->returnValue($user));
return $loginForm;
return [
'user' => [
'class' => UserFixture::className(),
'dataFile' => '@common/tests/unit/fixtures/data/models/user.php'
],
];
}
}
......@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge(
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit',
],
],
'controllerMap' => [
'fixture' => [
'class' => 'yii\faker\FixtureController',
'fixtureDataPath' => '@console/tests/unit/fixtures/data',
'templatePath' => '@common/tests/templates/fixtures'
],
],
]
);
......@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge(
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit',
],
],
'controllerMap' => [
'fixture' => [
'class' => 'yii\faker\FixtureController',
'fixtureDataPath' => '@console/tests/unit/fixtures/data',
'templatePath' => '@common/tests/templates/fixtures'
],
],
]
);
......@@ -5,7 +5,7 @@ return [
'username' => 'okirlin',
'auth_key' => 'iwTNae9t34OmnK6l4vT4IeaTk-YWI2Rv',
'password_hash' => '$2y$13$CXT0Rkle1EMJ/c1l5bylL.EylfmQ39O5JlHJVFpNn618OUS1HwaIi',
'password_reset_token' => 't5GU9NwpuGYSfb7FEZMAxqtuz2PkEvv_1391885313',
'password_reset_token' => 't5GU9NwpuGYSfb7FEZMAxqtuz2PkEvv_' . time(),
'created_at' => '1391885313',
'updated_at' => '1391885313',
'email' => 'brady.renner@rutherford.com',
......@@ -14,7 +14,7 @@ return [
'username' => 'troy.becker',
'auth_key' => 'EdKfXrx88weFMV0vIxuTMWKgfK2tS3Lp',
'password_hash' => '$2y$13$g5nv41Px7VBqhS3hVsVN2.MKfgT3jFdkXEsMC4rQJLfaMa7VaJqL2',
'password_reset_token' => '4BSNyiZNAuxjs5Mty990c47sVrgllIi_1391885313',
'password_reset_token' => '4BSNyiZNAuxjs5Mty990c47sVrgllIi_' . time(),
'created_at' => '1391885313',
'updated_at' => '1391885313',
'email' => 'nicolas.dianna@hotmail.com',
......
......@@ -7,14 +7,16 @@ use frontend\tests\unit\DbTestCase;
use frontend\models\PasswordResetRequestForm;
use common\tests\fixtures\UserFixture;
use common\models\User;
use Codeception\Specify;
class PasswordResetRequestFormTest extends DbTestCase
{
use \Codeception\Specify;
use Specify;
protected function setUp()
{
parent::setUp();
Yii::$app->mail->fileTransportCallback = function ($mailer, $message) {
return 'testing_message.eml';
};
......@@ -23,23 +25,28 @@ class PasswordResetRequestFormTest extends DbTestCase
protected function tearDown()
{
@unlink($this->getMessageFile());
parent::tearDown();
}
public function testSendEmailWrongUser()
{
$this->specify('no user with such email, message should not be send', function () {
$model = new PasswordResetRequestForm();
$model->email = 'not-existing-email@example.com';
expect('email not send', $model->sendEmail())->false();
});
$this->specify('user is not active, message should not be send', function () {
$model = new PasswordResetRequestForm();
$model->email = $this->user[1]['email'];
expect('email not send', $model->sendEmail())->false();
});
}
......@@ -53,11 +60,13 @@ class PasswordResetRequestFormTest extends DbTestCase
expect('user has valid token', $user->password_reset_token)->notNull();
$this->specify('message has correct format', function () use ($model) {
expect('message file exists', file_exists($this->getMessageFile()))->true();
$message = file_get_contents($this->getMessageFile());
expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']);
expect('message "to" is correct', $message)->contains($model->email);
});
}
......@@ -66,7 +75,7 @@ class PasswordResetRequestFormTest extends DbTestCase
return [
'user' => [
'class' => UserFixture::className(),
'dataFile' => '@frontend/tests/unit/fixtures/data/user.php'
'dataFile' => '@frontend/tests/unit/fixtures/data/models/user.php'
],
];
}
......@@ -75,4 +84,5 @@ class PasswordResetRequestFormTest extends DbTestCase
{
return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml';
}
}
......@@ -9,19 +9,26 @@ use frontend\models\ResetPasswordForm;
class ResetPasswordFormTest extends DbTestCase
{
use \Codeception\Specify;
/**
* @expectedException yii\base\InvalidParamException
*/
public function testResetWrongToken()
{
new ResetPasswordForm('notexistingtoken_1391882543');
}
/**
* @expectedException yii\base\InvalidParamException
*/
public function testResetEmptyToken()
{
new ResetPasswordForm('');
}
public function testResetPassword()
public function testResetCorrectToken()
{
$this->specify('wrong reset token', function () {
$this->setExpectedException('\Exception', 'Wrong password reset token.');
new ResetPasswordForm('notexistingtoken_1391882543');
});
$this->specify('not correct token', function () {
$this->setExpectedException('yii\base\InvalidParamException', 'Password reset token cannot be blank.');
new ResetPasswordForm('');
});
$form = new ResetPasswordForm($this->user[0]['password_reset_token']);
expect('password should be resetted', $form->resetPassword())->true();
}
public function fixtures()
......@@ -29,8 +36,9 @@ class ResetPasswordFormTest extends DbTestCase
return [
'user' => [
'class' => UserFixture::className(),
'dataFile' => '@frontend/tests/unit/fixtures/data/user.php'
'dataFile' => '@frontend/tests/unit/fixtures/data/models/user.php'
],
];
}
}
......@@ -4,23 +4,26 @@ namespace frontend\tests\unit\models;
use frontend\tests\unit\DbTestCase;
use common\tests\fixtures\UserFixture;
use Codeception\Specify;
use frontend\models\SignupForm;
class SignupFormTest extends DbTestCase
{
use \Codeception\Specify;
use Specify;
public function testCorrectSignup()
{
$model = $this->getMock('frontend\models\SignupForm', ['validate']);
$model->expects($this->once())->method('validate')->will($this->returnValue(true));
$model->username = 'some_username';
$model->email = 'some_email@example.com';
$model->password = 'some_password';
$model = new SignupForm([
'username' => 'some_username',
'email' => 'some_email@example.com',
'password' => 'some_password',
]);
$user = $model->signup();
$this->assertInstanceOf('common\models\User', $user);
$this->assertInstanceOf('common\models\User', $user, 'user should be valid');
expect('username should be correct', $user->username)->equals('some_username');
expect('email should be correct', $user->email)->equals('some_email@example.com');
expect('password should be correct', $user->validatePassword('some_password'))->true();
......@@ -28,10 +31,13 @@ class SignupFormTest extends DbTestCase
public function testNotCorrectSignup()
{
$model = $this->getMock('frontend\models\SignupForm', ['validate']);
$model->expects($this->once())->method('validate')->will($this->returnValue(false));
$model = new SignupForm([
'username' => 'troy.becker',
'email' => 'nicolas.dianna@hotmail.com',
'password' => 'some_password',
]);
expect('user should not be created', $model->signup())->null();
expect('username and email are in use, user should not be created', $model->signup())->null();
}
public function fixtures()
......@@ -39,8 +45,9 @@ class SignupFormTest extends DbTestCase
return [
'user' => [
'class' => UserFixture::className(),
'dataFile' => false, //do not load test data, only table cleanup
'dataFile' => '@frontend/tests/unit/fixtures/data/models/user.php',
],
];
}
}
......@@ -4,10 +4,11 @@ namespace tests\unit\models;
use Yii;
use yii\codeception\TestCase;
use Codeception\Specify;
class ContactFormTest extends TestCase
{
use \Codeception\Specify;
use Specify;
protected function setUp()
{
......@@ -55,4 +56,5 @@ class ContactFormTest extends TestCase
{
return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml';
}
}
......@@ -4,11 +4,12 @@ namespace tests\unit\models;
use Yii;
use yii\codeception\TestCase;
use app\models\User;
use app\models\LoginForm;
use Codeception\Specify;
class LoginFormTest extends TestCase
{
use \Codeception\Specify;
use Specify;
protected function tearDown()
{
......@@ -18,10 +19,10 @@ class LoginFormTest extends TestCase
public function testLoginNoUser()
{
$model = $this->mockUser(null);
$model->username = 'some_username';
$model->password = 'some_password';
$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();
......@@ -31,10 +32,10 @@ class LoginFormTest extends TestCase
public function testLoginWrongPassword()
{
$model = $this->mockUser(new User);
$model->username = 'demo';
$model->password = 'wrong-password';
$model = new LoginForm([
'username' => 'demo',
'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();
......@@ -45,10 +46,10 @@ class LoginFormTest extends TestCase
public function testLoginCorrect()
{
$model = $this->mockUser(new User(['password' => 'demo']));
$model->username = 'demo';
$model->password = 'demo';
$model = new LoginForm([
'username' => 'demo',
'password' => 'demo',
]);
$this->specify('user should be able to login with correct credentials', function () use ($model) {
expect('model should login user', $model->login())->true();
......@@ -57,11 +58,4 @@ class LoginFormTest extends TestCase
});
}
private function mockUser($user)
{
$loginForm = $this->getMock('app\models\LoginForm', ['getUser']);
$loginForm->expects($this->any())->method('getUser')->will($this->returnValue($user));
return $loginForm;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment