tests improved, aspect mock changes reverted
This commit is contained in:
parent
fc0df16f47
commit
ef23e05ba9
@ -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);
|
||||
|
@ -13,6 +13,10 @@ defined('YII_ENV') or define('YII_ENV', 'test');
|
||||
|
||||
require_once(__DIR__ . '/../vendor/autoload.php');
|
||||
|
||||
require_once(__DIR__ . '/../vendor/yiisoft/yii2/yii/Yii.php');
|
||||
|
||||
// set correct script paths
|
||||
$_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE;
|
||||
$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
|
||||
|
||||
Yii::setAlias('@tests', __DIR__);
|
||||
|
@ -1,6 +1,3 @@
|
||||
<?php
|
||||
|
||||
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/yii/Yii.php');
|
||||
Yii::setAlias('@tests', __DIR__ . '/../');
|
||||
|
||||
new yii\web\Application(require(__DIR__ . '/_config.php'));
|
||||
|
@ -1,6 +1,3 @@
|
||||
<?php
|
||||
|
||||
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/yii/Yii.php');
|
||||
Yii::setAlias('@tests', __DIR__ . '/../');
|
||||
|
||||
new yii\web\Application(require(__DIR__ . '/_config.php'));
|
||||
|
@ -1,5 +1,3 @@
|
||||
<?php
|
||||
|
||||
#aspect-mock should be included only once. Codeception calls this bootstrap file per each test file.
|
||||
require_once __DIR__ . '/aspect_mock.php';
|
||||
Yii::setAlias('@tests', __DIR__ . '/../');
|
||||
// add unit testing specific bootstrap code here
|
||||
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
$kernel = AspectMock\Kernel::getInstance();
|
||||
$kernel->init([
|
||||
'debug' => true,
|
||||
'excludePaths' => [
|
||||
__DIR__.'/../tests',
|
||||
__DIR__.'/../mails',
|
||||
__DIR__.'/../runtime',
|
||||
__DIR__.'/../config',
|
||||
__DIR__.'/../controllers',
|
||||
__DIR__.'/../assets',
|
||||
],
|
||||
]);
|
||||
|
||||
$kernel->loadFile(__DIR__ . '/../../vendor/yiisoft/yii2/yii/Yii.php');
|
@ -4,8 +4,6 @@ namespace tests\unit\models;
|
||||
|
||||
use Yii;
|
||||
use yii\codeception\TestCase;
|
||||
use app\models\ContactForm;
|
||||
use AspectMock\Test as test;
|
||||
|
||||
class ContactFormTest extends TestCase
|
||||
{
|
||||
@ -23,16 +21,15 @@ class ContactFormTest extends TestCase
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
unlink(Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml');
|
||||
test::clean();
|
||||
unlink($this->getMessageFile());
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testContact()
|
||||
{
|
||||
test::double('app\models\ContactForm',['validate' => true]);
|
||||
$model = $this->getMock('app\models\ContactForm', ['validate']);
|
||||
$model->expects($this->once())->method('validate')->will($this->returnValue(true));
|
||||
|
||||
$model = new ContactForm();
|
||||
$model->attributes = [
|
||||
'name' => 'Tester',
|
||||
'email' => 'tester@example.com',
|
||||
|
@ -2,69 +2,61 @@
|
||||
|
||||
namespace tests\unit\models;
|
||||
|
||||
use Yii;
|
||||
use yii\codeception\TestCase;
|
||||
use app\models\LoginForm;
|
||||
use app\models\User;
|
||||
use AspectMock\Test as test;
|
||||
|
||||
class LoginFormTest extends TestCase
|
||||
{
|
||||
|
||||
use \Codeception\Specify;
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
test::clean();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testLoginNoUser()
|
||||
{
|
||||
$user = $this->mockUser(null);
|
||||
$model = $this->mockUser(null);
|
||||
|
||||
$model = new LoginForm();
|
||||
$model->username = 'some_username';
|
||||
$model->password = 'some_password';
|
||||
|
||||
$this->specify('user should not be able to login, when there is no identity' , function () use ($user,$model) {
|
||||
$this->specify('user should not be able to login, when there is no identity' , function () use ($model) {
|
||||
$this->assertFalse($model->login());
|
||||
$user->verifyInvoked('findByUsername',['some_username']);
|
||||
$this->assertTrue(Yii::$app->user->isGuest,'user should not be logged in');
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginWrongPassword()
|
||||
{
|
||||
$this->mockUser(new User);
|
||||
$model = $this->mockUser(new User);
|
||||
|
||||
$model = new LoginForm();
|
||||
$model->username = 'demo';
|
||||
$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) {
|
||||
$this->assertFalse($model->login());
|
||||
$this->assertArrayHasKey('password',$model->errors);
|
||||
$this->assertTrue(Yii::$app->user->isGuest,'user should not be logged in');
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginCorrect()
|
||||
{
|
||||
$this->mockUser(new User(['password' => 'demo']));
|
||||
$model = $this->mockUser(new User(['password' => 'demo']));
|
||||
|
||||
$model = new LoginForm();
|
||||
$model->username = 'demo';
|
||||
$model->password = 'demo';
|
||||
|
||||
$this->specify('user should not be able to login with correct credentials', function() use($model) {
|
||||
$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)
|
||||
{
|
||||
return test::double('app\models\User', [
|
||||
'findByUsername' => $user,
|
||||
]);
|
||||
$loginForm = $this->getMock('app\models\LoginForm',['getUser']);
|
||||
$loginForm->expects($this->any())->method('getUser')->will($this->returnValue($user));
|
||||
return $loginForm;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user