Merge pull request #1714 from Ragazzo/tests_improved
modified tests, improved configuration, introduced aspect
This commit is contained in:
commit
6ffc766bed
@ -20,7 +20,9 @@
|
||||
"yiisoft/yii2-codeception": "*",
|
||||
"yiisoft/yii2-debug": "*",
|
||||
"yiisoft/yii2-gii": "*",
|
||||
"yiisoft/yii2-swiftmailer": "*"
|
||||
"yiisoft/yii2-swiftmailer": "*",
|
||||
"codeception/codeception": "*",
|
||||
"codeception/specify": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"post-create-project-cmd": [
|
||||
|
@ -38,6 +38,7 @@ class LoginForm extends Model
|
||||
public function validatePassword()
|
||||
{
|
||||
$user = $this->getUser();
|
||||
|
||||
if (!$user || !$user->validatePassword($this->password)) {
|
||||
$this->addError('password', 'Incorrect username or password.');
|
||||
}
|
||||
@ -61,7 +62,7 @@ class LoginForm extends Model
|
||||
*
|
||||
* @return User|null
|
||||
*/
|
||||
private function getUser()
|
||||
public function getUser()
|
||||
{
|
||||
if ($this->_user === false) {
|
||||
$this->_user = User::findByUsername($this->username);
|
||||
|
@ -2,9 +2,58 @@
|
||||
|
||||
namespace tests\unit\models;
|
||||
|
||||
use Yii;
|
||||
use yii\codeception\TestCase;
|
||||
|
||||
class ContactFormTest extends TestCase
|
||||
{
|
||||
// TODO add test methods here
|
||||
|
||||
use \Codeception\Specify;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
Yii::$app->mail->fileTransportCallback = function ($mailer, $message) {
|
||||
return 'testing_message.eml';
|
||||
};
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
unlink($this->getMessageFile());
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testContact()
|
||||
{
|
||||
$model = $this->getMock('app\models\ContactForm', ['validate']);
|
||||
$model->expects($this->once())->method('validate')->will($this->returnValue(true));
|
||||
|
||||
$model->attributes = [
|
||||
'name' => 'Tester',
|
||||
'email' => 'tester@example.com',
|
||||
'subject' => 'very important letter subject',
|
||||
'body' => 'body of current message',
|
||||
];
|
||||
|
||||
$model->contact('admin@example.com');
|
||||
|
||||
$this->specify('email should be send', function () {
|
||||
$this->assertFileExists($this->getMessageFile(), 'email file should exist');
|
||||
});
|
||||
|
||||
$this->specify('message should contain correct data', function () use($model) {
|
||||
$emailMessage = file_get_contents($this->getMessageFile());
|
||||
$this->assertContains($model->name, $emailMessage, 'email should contain user name');
|
||||
$this->assertContains($model->email, $emailMessage, 'email should contain sender email');
|
||||
$this->assertContains($model->subject, $emailMessage, 'email should contain subject');
|
||||
$this->assertContains($model->body, $emailMessage, 'email should contain body');
|
||||
});
|
||||
}
|
||||
|
||||
private function getMessageFile()
|
||||
{
|
||||
return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml';
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,9 +2,61 @@
|
||||
|
||||
namespace tests\unit\models;
|
||||
|
||||
use Yii;
|
||||
use yii\codeception\TestCase;
|
||||
use app\models\User;
|
||||
|
||||
class LoginFormTest extends TestCase
|
||||
{
|
||||
// TODO add test methods here
|
||||
|
||||
use \Codeception\Specify;
|
||||
|
||||
public function testLoginNoUser()
|
||||
{
|
||||
$model = $this->mockUser(null);
|
||||
|
||||
$model->username = 'some_username';
|
||||
$model->password = 'some_password';
|
||||
|
||||
$this->specify('user should not be able to login, when there is no identity' , function () use ($model) {
|
||||
$this->assertFalse($model->login());
|
||||
$this->assertTrue(Yii::$app->user->isGuest,'user should not be logged in');
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginWrongPassword()
|
||||
{
|
||||
$model = $this->mockUser(new User);
|
||||
|
||||
$model->username = 'demo';
|
||||
$model->password = 'wrong-password';
|
||||
|
||||
$this->specify('user should not be able to login with wrong password', function () use ($model) {
|
||||
$this->assertFalse($model->login());
|
||||
$this->assertArrayHasKey('password',$model->errors);
|
||||
$this->assertTrue(Yii::$app->user->isGuest,'user should not be logged in');
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginCorrect()
|
||||
{
|
||||
$model = $this->mockUser(new User(['password' => 'demo']));
|
||||
|
||||
$model->username = 'demo';
|
||||
$model->password = 'demo';
|
||||
|
||||
$this->specify('user should be able to login with correct credentials', function() use ($model) {
|
||||
$this->assertTrue($model->login());
|
||||
$this->assertArrayNotHasKey('password',$model->errors);
|
||||
$this->assertFalse(Yii::$app->user->isGuest,'user should be logged in');
|
||||
});
|
||||
}
|
||||
|
||||
private function mockUser($user)
|
||||
{
|
||||
$loginForm = $this->getMock('app\models\LoginForm',['getUser']);
|
||||
$loginForm->expects($this->any())->method('getUser')->will($this->returnValue($user));
|
||||
return $loginForm;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user