Commit f1798268 by Mark

application advanced template tests fixed

parent 4498ccd4
...@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge( ...@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge(
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit', '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; ...@@ -4,5 +4,5 @@ namespace common\tests\unit;
class DbTestCase extends \yii\codeception\DbTestCase 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( ...@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge(
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit', '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 @@ ...@@ -3,14 +3,29 @@
namespace common\tests\unit\models; namespace common\tests\unit\models;
use Yii; use Yii;
use frontend\tests\unit\TestCase; use common\tests\unit\DbTestCase;
use common\models\User; use Codeception\Specify;
use yii\helpers\Security; 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() protected function tearDown()
{ {
...@@ -20,10 +35,10 @@ class LoginFormTest extends TestCase ...@@ -20,10 +35,10 @@ class LoginFormTest extends TestCase
public function testLoginNoUser() public function testLoginNoUser()
{ {
$model = $this->mockUser(null); $model = new LoginForm([
'username' => 'not_existing_username',
$model->username = 'some_username'; 'password' => 'not_existing_password',
$model->password = 'some_password'; ]);
$this->specify('user should not be able to login, when there is no identity', function () use ($model) { $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('model should not login user', $model->login())->false();
...@@ -33,10 +48,10 @@ class LoginFormTest extends TestCase ...@@ -33,10 +48,10 @@ class LoginFormTest extends TestCase
public function testLoginWrongPassword() public function testLoginWrongPassword()
{ {
$model = $this->mockUser(new User(['password_hash' => Security::generatePasswordHash('will-not-match')])); $model = new LoginForm([
'username' => 'bayer.hudson',
$model->username = 'demo'; 'password' => 'wrong_password',
$model->password = 'wrong-password'; ]);
$this->specify('user should not be able to login with wrong password', function () use ($model) { $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('model should not login user', $model->login())->false();
...@@ -47,10 +62,11 @@ class LoginFormTest extends TestCase ...@@ -47,10 +62,11 @@ class LoginFormTest extends TestCase
public function testLoginCorrect() public function testLoginCorrect()
{ {
$model = $this->mockUser(new User(['password_hash' => Security::generatePasswordHash('demo')]));
$model->username = 'demo'; $model = new LoginForm([
$model->password = 'demo'; 'username' => 'bayer.hudson',
'password' => 'password_0',
]);
$this->specify('user should be able to login with correct credentials', function () use ($model) { $this->specify('user should be able to login with correct credentials', function () use ($model) {
expect('model should login user', $model->login())->true(); expect('model should login user', $model->login())->true();
...@@ -59,11 +75,14 @@ class LoginFormTest extends TestCase ...@@ -59,11 +75,14 @@ class LoginFormTest extends TestCase
}); });
} }
private function mockUser($user) public function fixtures()
{ {
$loginForm = $this->getMock('common\models\LoginForm', ['getUser']); return [
$loginForm->expects($this->any())->method('getUser')->will($this->returnValue($user)); 'user' => [
'class' => UserFixture::className(),
return $loginForm; 'dataFile' => '@common/tests/unit/fixtures/data/models/user.php'
],
];
} }
} }
...@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge( ...@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge(
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit', '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( ...@@ -11,5 +11,12 @@ return yii\helpers\ArrayHelper::merge(
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_unit', '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; ...@@ -7,14 +7,16 @@ use frontend\tests\unit\DbTestCase;
use frontend\models\PasswordResetRequestForm; use frontend\models\PasswordResetRequestForm;
use common\tests\fixtures\UserFixture; use common\tests\fixtures\UserFixture;
use common\models\User; use common\models\User;
use Codeception\Specify;
class PasswordResetRequestFormTest extends DbTestCase class PasswordResetRequestFormTest extends DbTestCase
{ {
use \Codeception\Specify; use Specify;
protected function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
Yii::$app->mail->fileTransportCallback = function ($mailer, $message) { Yii::$app->mail->fileTransportCallback = function ($mailer, $message) {
return 'testing_message.eml'; return 'testing_message.eml';
}; };
...@@ -23,23 +25,28 @@ class PasswordResetRequestFormTest extends DbTestCase ...@@ -23,23 +25,28 @@ class PasswordResetRequestFormTest extends DbTestCase
protected function tearDown() protected function tearDown()
{ {
@unlink($this->getMessageFile()); @unlink($this->getMessageFile());
parent::tearDown(); parent::tearDown();
} }
public function testSendEmailWrongUser() public function testSendEmailWrongUser()
{ {
$this->specify('no user with such email, message should not be send', function () { $this->specify('no user with such email, message should not be send', function () {
$model = new PasswordResetRequestForm(); $model = new PasswordResetRequestForm();
$model->email = 'not-existing-email@example.com'; $model->email = 'not-existing-email@example.com';
expect('email not send', $model->sendEmail())->false(); expect('email not send', $model->sendEmail())->false();
}); });
$this->specify('user is not active, message should not be send', function () { $this->specify('user is not active, message should not be send', function () {
$model = new PasswordResetRequestForm(); $model = new PasswordResetRequestForm();
$model->email = $this->user[1]['email']; $model->email = $this->user[1]['email'];
expect('email not send', $model->sendEmail())->false(); expect('email not send', $model->sendEmail())->false();
}); });
} }
...@@ -53,11 +60,13 @@ class PasswordResetRequestFormTest extends DbTestCase ...@@ -53,11 +60,13 @@ class PasswordResetRequestFormTest extends DbTestCase
expect('user has valid token', $user->password_reset_token)->notNull(); expect('user has valid token', $user->password_reset_token)->notNull();
$this->specify('message has correct format', function () use ($model) { $this->specify('message has correct format', function () use ($model) {
expect('message file exists', file_exists($this->getMessageFile()))->true(); expect('message file exists', file_exists($this->getMessageFile()))->true();
$message = file_get_contents($this->getMessageFile()); $message = file_get_contents($this->getMessageFile());
expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']); expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']);
expect('message "to" is correct', $message)->contains($model->email); expect('message "to" is correct', $message)->contains($model->email);
}); });
} }
...@@ -66,7 +75,7 @@ class PasswordResetRequestFormTest extends DbTestCase ...@@ -66,7 +75,7 @@ class PasswordResetRequestFormTest extends DbTestCase
return [ return [
'user' => [ 'user' => [
'class' => UserFixture::className(), '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 ...@@ -75,4 +84,5 @@ class PasswordResetRequestFormTest extends DbTestCase
{ {
return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml'; return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml';
} }
} }
...@@ -9,19 +9,26 @@ use frontend\models\ResetPasswordForm; ...@@ -9,19 +9,26 @@ use frontend\models\ResetPasswordForm;
class ResetPasswordFormTest extends DbTestCase 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 () { $form = new ResetPasswordForm($this->user[0]['password_reset_token']);
$this->setExpectedException('\Exception', 'Wrong password reset token.'); expect('password should be resetted', $form->resetPassword())->true();
new ResetPasswordForm('notexistingtoken_1391882543');
});
$this->specify('not correct token', function () {
$this->setExpectedException('yii\base\InvalidParamException', 'Password reset token cannot be blank.');
new ResetPasswordForm('');
});
} }
public function fixtures() public function fixtures()
...@@ -29,8 +36,9 @@ class ResetPasswordFormTest extends DbTestCase ...@@ -29,8 +36,9 @@ class ResetPasswordFormTest extends DbTestCase
return [ return [
'user' => [ 'user' => [
'class' => UserFixture::className(), '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; ...@@ -4,23 +4,26 @@ namespace frontend\tests\unit\models;
use frontend\tests\unit\DbTestCase; use frontend\tests\unit\DbTestCase;
use common\tests\fixtures\UserFixture; use common\tests\fixtures\UserFixture;
use Codeception\Specify;
use frontend\models\SignupForm;
class SignupFormTest extends DbTestCase class SignupFormTest extends DbTestCase
{ {
use \Codeception\Specify; use Specify;
public function testCorrectSignup() public function testCorrectSignup()
{ {
$model = $this->getMock('frontend\models\SignupForm', ['validate']); $model = new SignupForm([
$model->expects($this->once())->method('validate')->will($this->returnValue(true)); 'username' => 'some_username',
'email' => 'some_email@example.com',
$model->username = 'some_username'; 'password' => 'some_password',
$model->email = 'some_email@example.com'; ]);
$model->password = 'some_password';
$user = $model->signup(); $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('username should be correct', $user->username)->equals('some_username');
expect('email should be correct', $user->email)->equals('some_email@example.com'); expect('email should be correct', $user->email)->equals('some_email@example.com');
expect('password should be correct', $user->validatePassword('some_password'))->true(); expect('password should be correct', $user->validatePassword('some_password'))->true();
...@@ -28,10 +31,13 @@ class SignupFormTest extends DbTestCase ...@@ -28,10 +31,13 @@ class SignupFormTest extends DbTestCase
public function testNotCorrectSignup() public function testNotCorrectSignup()
{ {
$model = $this->getMock('frontend\models\SignupForm', ['validate']); $model = new SignupForm([
$model->expects($this->once())->method('validate')->will($this->returnValue(false)); '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() public function fixtures()
...@@ -39,8 +45,9 @@ class SignupFormTest extends DbTestCase ...@@ -39,8 +45,9 @@ class SignupFormTest extends DbTestCase
return [ return [
'user' => [ 'user' => [
'class' => UserFixture::className(), '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