diff --git a/apps/basic/models/LoginForm.php b/apps/basic/models/LoginForm.php index a365db7..d536443 100644 --- a/apps/basic/models/LoginForm.php +++ b/apps/basic/models/LoginForm.php @@ -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); diff --git a/apps/basic/tests/_bootstrap.php b/apps/basic/tests/_bootstrap.php index 78e28e6..07e86c7 100644 --- a/apps/basic/tests/_bootstrap.php +++ b/apps/basic/tests/_bootstrap.php @@ -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__); diff --git a/apps/basic/tests/acceptance/_bootstrap.php b/apps/basic/tests/acceptance/_bootstrap.php index 9f0467b..6ce3d17 100644 --- a/apps/basic/tests/acceptance/_bootstrap.php +++ b/apps/basic/tests/acceptance/_bootstrap.php @@ -1,6 +1,3 @@ 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'); diff --git a/apps/basic/tests/unit/models/ContactFormTest.php b/apps/basic/tests/unit/models/ContactFormTest.php index 04185ce..906a1c9 100644 --- a/apps/basic/tests/unit/models/ContactFormTest.php +++ b/apps/basic/tests/unit/models/ContactFormTest.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', diff --git a/apps/basic/tests/unit/models/LoginFormTest.php b/apps/basic/tests/unit/models/LoginFormTest.php index 4549d36..f232a47 100644 --- a/apps/basic/tests/unit/models/LoginFormTest.php +++ b/apps/basic/tests/unit/models/LoginFormTest.php @@ -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; } } \ No newline at end of file