Browse Source

tests improved, aspect mock changes reverted

tags/2.0.0-beta
Mark 11 years ago
parent
commit
11c42fa7c1
  1. 3
      apps/basic/models/LoginForm.php
  2. 4
      apps/basic/tests/_bootstrap.php
  3. 3
      apps/basic/tests/acceptance/_bootstrap.php
  4. 3
      apps/basic/tests/functional/_bootstrap.php
  5. 4
      apps/basic/tests/unit/_bootstrap.php
  6. 16
      apps/basic/tests/unit/aspect_mock.php
  7. 9
      apps/basic/tests/unit/models/ContactFormTest.php
  8. 34
      apps/basic/tests/unit/models/LoginFormTest.php

3
apps/basic/models/LoginForm.php

@ -38,6 +38,7 @@ class LoginForm extends Model
public function validatePassword() public function validatePassword()
{ {
$user = $this->getUser(); $user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) { if (!$user || !$user->validatePassword($this->password)) {
$this->addError('password', 'Incorrect username or password.'); $this->addError('password', 'Incorrect username or password.');
} }
@ -61,7 +62,7 @@ class LoginForm extends Model
* *
* @return User|null * @return User|null
*/ */
private function getUser() public function getUser()
{ {
if ($this->_user === false) { if ($this->_user === false) {
$this->_user = User::findByUsername($this->username); $this->_user = User::findByUsername($this->username);

4
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/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/yii/Yii.php');
// set correct script paths // set correct script paths
$_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE; $_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL; $_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
Yii::setAlias('@tests', __DIR__);

3
apps/basic/tests/acceptance/_bootstrap.php

@ -1,6 +1,3 @@
<?php <?php
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/yii/Yii.php');
Yii::setAlias('@tests', __DIR__ . '/../');
new yii\web\Application(require(__DIR__ . '/_config.php')); new yii\web\Application(require(__DIR__ . '/_config.php'));

3
apps/basic/tests/functional/_bootstrap.php

@ -1,6 +1,3 @@
<?php <?php
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/yii/Yii.php');
Yii::setAlias('@tests', __DIR__ . '/../');
new yii\web\Application(require(__DIR__ . '/_config.php')); new yii\web\Application(require(__DIR__ . '/_config.php'));

4
apps/basic/tests/unit/_bootstrap.php

@ -1,5 +1,3 @@
<?php <?php
#aspect-mock should be included only once. Codeception calls this bootstrap file per each test file. // add unit testing specific bootstrap code here
require_once __DIR__ . '/aspect_mock.php';
Yii::setAlias('@tests', __DIR__ . '/../');

16
apps/basic/tests/unit/aspect_mock.php

@ -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');

9
apps/basic/tests/unit/models/ContactFormTest.php

@ -4,8 +4,6 @@ namespace tests\unit\models;
use Yii; use Yii;
use yii\codeception\TestCase; use yii\codeception\TestCase;
use app\models\ContactForm;
use AspectMock\Test as test;
class ContactFormTest extends TestCase class ContactFormTest extends TestCase
{ {
@ -23,16 +21,15 @@ class ContactFormTest extends TestCase
protected function tearDown() protected function tearDown()
{ {
unlink(Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml'); unlink($this->getMessageFile());
test::clean();
parent::tearDown(); parent::tearDown();
} }
public function testContact() 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 = [ $model->attributes = [
'name' => 'Tester', 'name' => 'Tester',
'email' => 'tester@example.com', 'email' => 'tester@example.com',

34
apps/basic/tests/unit/models/LoginFormTest.php

@ -2,69 +2,61 @@
namespace tests\unit\models; namespace tests\unit\models;
use Yii;
use yii\codeception\TestCase; use yii\codeception\TestCase;
use app\models\LoginForm;
use app\models\User; use app\models\User;
use AspectMock\Test as test;
class LoginFormTest extends TestCase class LoginFormTest extends TestCase
{ {
use \Codeception\Specify; use \Codeception\Specify;
protected function tearDown()
{
test::clean();
parent::tearDown();
}
public function testLoginNoUser() public function testLoginNoUser()
{ {
$user = $this->mockUser(null); $model = $this->mockUser(null);
$model = new LoginForm();
$model->username = 'some_username'; $model->username = 'some_username';
$model->password = 'some_password'; $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()); $this->assertFalse($model->login());
$user->verifyInvoked('findByUsername',['some_username']); $this->assertTrue(Yii::$app->user->isGuest,'user should not be logged in');
}); });
} }
public function testLoginWrongPassword() public function testLoginWrongPassword()
{ {
$this->mockUser(new User); $model = $this->mockUser(new User);
$model = new LoginForm();
$model->username = 'demo'; $model->username = 'demo';
$model->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) {
$this->assertFalse($model->login()); $this->assertFalse($model->login());
$this->assertArrayHasKey('password',$model->errors); $this->assertArrayHasKey('password',$model->errors);
$this->assertTrue(Yii::$app->user->isGuest,'user should not be logged in');
}); });
} }
public function testLoginCorrect() public function testLoginCorrect()
{ {
$this->mockUser(new User(['password' => 'demo'])); $model = $this->mockUser(new User(['password' => 'demo']));
$model = new LoginForm();
$model->username = 'demo'; $model->username = 'demo';
$model->password = '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->assertTrue($model->login());
$this->assertArrayNotHasKey('password',$model->errors); $this->assertArrayNotHasKey('password',$model->errors);
$this->assertFalse(Yii::$app->user->isGuest,'user should be logged in');
}); });
} }
private function mockUser($user) private function mockUser($user)
{ {
return test::double('app\models\User', [ $loginForm = $this->getMock('app\models\LoginForm',['getUser']);
'findByUsername' => $user, $loginForm->expects($this->any())->method('getUser')->will($this->returnValue($user));
]); return $loginForm;
} }
} }
Loading…
Cancel
Save