From 1afe198c1900fa5a245cf14043f7fb99de64d281 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 31 Dec 2013 00:17:36 +0400 Subject: [PATCH 1/4] modified tests, improved configuration, introduced aspect mock and specify. --- apps/basic/composer.json | 5 +- apps/basic/tests/_bootstrap.php | 4 -- apps/basic/tests/acceptance/_bootstrap.php | 3 ++ apps/basic/tests/functional/_bootstrap.php | 3 ++ apps/basic/tests/unit/_bootstrap.php | 4 +- apps/basic/tests/unit/aspect_mock.php | 16 ++++++ apps/basic/tests/unit/models/ContactFormTest.php | 55 ++++++++++++++++++++- apps/basic/tests/unit/models/LoginFormTest.php | 62 +++++++++++++++++++++++- 8 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 apps/basic/tests/unit/aspect_mock.php diff --git a/apps/basic/composer.json b/apps/basic/composer.json index 63e3d92..f771aba 100644 --- a/apps/basic/composer.json +++ b/apps/basic/composer.json @@ -20,7 +20,10 @@ "yiisoft/yii2-codeception": "*", "yiisoft/yii2-debug": "*", "yiisoft/yii2-gii": "*", - "yiisoft/yii2-swiftmailer": "*" + "yiisoft/yii2-swiftmailer": "*", + "codeception/codeception": "*", + "codeception/aspect-mock": "*", + "codeception/specify": "*" }, "scripts": { "post-create-project-cmd": [ diff --git a/apps/basic/tests/_bootstrap.php b/apps/basic/tests/_bootstrap.php index 07e86c7..78e28e6 100644 --- a/apps/basic/tests/_bootstrap.php +++ b/apps/basic/tests/_bootstrap.php @@ -13,10 +13,6 @@ 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 6ce3d17..9f0467b 100644 --- a/apps/basic/tests/acceptance/_bootstrap.php +++ b/apps/basic/tests/acceptance/_bootstrap.php @@ -1,3 +1,6 @@ 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 6cf9769..04185ce 100644 --- a/apps/basic/tests/unit/models/ContactFormTest.php +++ b/apps/basic/tests/unit/models/ContactFormTest.php @@ -2,9 +2,62 @@ namespace tests\unit\models; +use Yii; use yii\codeception\TestCase; +use app\models\ContactForm; +use AspectMock\Test as test; 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(Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml'); + test::clean(); + parent::tearDown(); + } + + public function testContact() + { + test::double('app\models\ContactForm',['validate' => true]); + + $model = new ContactForm(); + $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'; + } + } diff --git a/apps/basic/tests/unit/models/LoginFormTest.php b/apps/basic/tests/unit/models/LoginFormTest.php index efdd242..4549d36 100644 --- a/apps/basic/tests/unit/models/LoginFormTest.php +++ b/apps/basic/tests/unit/models/LoginFormTest.php @@ -3,8 +3,68 @@ namespace tests\unit\models; use yii\codeception\TestCase; +use app\models\LoginForm; +use app\models\User; +use AspectMock\Test as test; class LoginFormTest extends TestCase { - // TODO add test methods here + + use \Codeception\Specify; + + protected function tearDown() + { + test::clean(); + parent::tearDown(); + } + + public function testLoginNoUser() + { + $user = $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->assertFalse($model->login()); + $user->verifyInvoked('findByUsername',['some_username']); + }); + } + + public function testLoginWrongPassword() + { + $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->assertFalse($model->login()); + $this->assertArrayHasKey('password',$model->errors); + }); + } + + public function testLoginCorrect() + { + $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->assertTrue($model->login()); + $this->assertArrayNotHasKey('password',$model->errors); + }); + } + + private function mockUser($user) + { + return test::double('app\models\User', [ + 'findByUsername' => $user, + ]); + } + } \ No newline at end of file From 11c42fa7c1e8ef14b3b82d644975cb0c11dce138 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 31 Dec 2013 23:02:10 +0400 Subject: [PATCH 2/4] tests improved, aspect mock changes reverted --- apps/basic/models/LoginForm.php | 3 ++- apps/basic/tests/_bootstrap.php | 4 +++ apps/basic/tests/acceptance/_bootstrap.php | 3 --- apps/basic/tests/functional/_bootstrap.php | 3 --- apps/basic/tests/unit/_bootstrap.php | 4 +-- apps/basic/tests/unit/aspect_mock.php | 16 ----------- apps/basic/tests/unit/models/ContactFormTest.php | 9 +++---- apps/basic/tests/unit/models/LoginFormTest.php | 34 +++++++++--------------- 8 files changed, 23 insertions(+), 53 deletions(-) delete mode 100644 apps/basic/tests/unit/aspect_mock.php 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 From 1c1a310a5c7e5ea0af24c8caefec4aa41ee1a6dd Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 31 Dec 2013 23:04:02 +0400 Subject: [PATCH 3/4] aspect mock requirement removed --- apps/basic/composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/basic/composer.json b/apps/basic/composer.json index f771aba..dbdcd35 100644 --- a/apps/basic/composer.json +++ b/apps/basic/composer.json @@ -22,7 +22,6 @@ "yiisoft/yii2-gii": "*", "yiisoft/yii2-swiftmailer": "*", "codeception/codeception": "*", - "codeception/aspect-mock": "*", "codeception/specify": "*" }, "scripts": { From 139fbcbc9386fe8d7fcfd317359b33ca5a9ff0f2 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 31 Dec 2013 23:13:47 +0400 Subject: [PATCH 4/4] CS fix --- apps/basic/tests/unit/models/ContactFormTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/basic/tests/unit/models/ContactFormTest.php b/apps/basic/tests/unit/models/ContactFormTest.php index 906a1c9..8c67567 100644 --- a/apps/basic/tests/unit/models/ContactFormTest.php +++ b/apps/basic/tests/unit/models/ContactFormTest.php @@ -13,8 +13,7 @@ class ContactFormTest extends TestCase protected function setUp() { parent::setUp(); - Yii::$app->mail->fileTransportCallback = function ($mailer, $message) - { + Yii::$app->mail->fileTransportCallback = function ($mailer, $message) { return 'testing_message.eml'; }; }