Commit f1798268 by Mark

application advanced template tests fixed

parent 4498ccd4
......@@ -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'
],
],
]
);
......@@ -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;
public function testResetPassword()
/**
* @expectedException yii\base\InvalidParamException
*/
public function testResetWrongToken()
{
$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.');
/**
* @expectedException yii\base\InvalidParamException
*/
public function testResetEmptyToken()
{
new ResetPasswordForm('');
});
}
public function testResetCorrectToken()
{
$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',
],
];
}
}
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