From b7526436aa29887e66bd690fdcbffbf5ae0fd804 Mon Sep 17 00:00:00 2001 From: Davert Date: Tue, 19 Jul 2016 03:43:26 +0300 Subject: [PATCH] added email verification for unit and functional tests --- tests/acceptance.suite.yml.example | 1 + tests/functional/ContactFormCest.php | 1 + tests/functional/LoginFormCest.php | 14 +++++------ tests/unit.suite.yml | 2 +- tests/unit/models/ContactFormTest.php | 35 +++++++++------------------ 5 files changed, 22 insertions(+), 31 deletions(-) diff --git a/tests/acceptance.suite.yml.example b/tests/acceptance.suite.yml.example index 7214034..7bb4986 100644 --- a/tests/acceptance.suite.yml.example +++ b/tests/acceptance.suite.yml.example @@ -7,3 +7,4 @@ modules: - Yii2: part: orm entryScript: index-test.php + cleanup: false diff --git a/tests/functional/ContactFormCest.php b/tests/functional/ContactFormCest.php index 07ab503..a874317 100644 --- a/tests/functional/ContactFormCest.php +++ b/tests/functional/ContactFormCest.php @@ -49,6 +49,7 @@ class ContactFormCest 'ContactForm[body]' => 'test content', 'ContactForm[verifyCode]' => 'testme', ]); + $I->seeEmailIsSent(); $I->dontSeeElement('#contact-form'); $I->see('Thank you for contacting us. We will respond to you as soon as possible.'); } diff --git a/tests/functional/LoginFormCest.php b/tests/functional/LoginFormCest.php index 47b1b0d..9ed74b1 100644 --- a/tests/functional/LoginFormCest.php +++ b/tests/functional/LoginFormCest.php @@ -3,7 +3,7 @@ class LoginFormCest { public function _before(\FunctionalTester $I) { - $I->amOnPage(['site/login']); + $I->amOnRoute('site/login'); } public function openLoginPage(\FunctionalTester $I) @@ -12,18 +12,18 @@ class LoginFormCest } - // demonstrates `amLoggedAs` method + // demonstrates `amLoggedInAs` method public function internalLoginById(\FunctionalTester $I) { - $I->amLoggedAs(100); + $I->amLoggedInAs(100); $I->amOnPage('/'); $I->see('Logout (admin)'); } - // demonstrates `amLoggedAs` method - public function internalLoginBy(\FunctionalTester $I) + // demonstrates `amLoggedInAs` method + public function internalLoginByInstance(\FunctionalTester $I) { - $I->amLoggedAs(\app\models\User::findByUsername('admin')); + $I->amLoggedInAs(\app\models\User::findByUsername('admin')); $I->amOnPage('/'); $I->see('Logout (admin)'); } @@ -36,7 +36,7 @@ class LoginFormCest $I->see('Password cannot be blank.'); } - public function loginWithWringCredentials(\FunctionalTester $I) + public function loginWithWrongCredentials(\FunctionalTester $I) { $I->submitForm('#login-form', [ 'LoginForm[username]' => 'admin', diff --git a/tests/unit.suite.yml b/tests/unit.suite.yml index b944d5c..b1efaba 100644 --- a/tests/unit.suite.yml +++ b/tests/unit.suite.yml @@ -8,4 +8,4 @@ modules: enabled: - Asserts - Yii2: - part: orm \ No newline at end of file + part: [orm, email] \ No newline at end of file diff --git a/tests/unit/models/ContactFormTest.php b/tests/unit/models/ContactFormTest.php index 445b442..81a67e9 100644 --- a/tests/unit/models/ContactFormTest.php +++ b/tests/unit/models/ContactFormTest.php @@ -7,18 +7,10 @@ use app\models\ContactForm; class ContactFormTest extends \Codeception\Test\Unit { private $model; - - protected function _before() - { - \Yii::$app->mailer->fileTransportCallback = function () { - return 'testing_message.eml'; - }; - } - - protected function _after() - { - unlink($this->getMessageFile()); - } + /** + * @var \UnitTester + */ + public $tester; public function testEmailIsSentOnContact() { @@ -39,18 +31,15 @@ class ContactFormTest extends \Codeception\Test\Unit ]; expect_that($this->model->contact('admin@example.com')); - expect_that(file_exists($this->getMessageFile())); - $emailMessage = file_get_contents($this->getMessageFile()); + // using Yii2 module actions to check email was sent + $this->tester->seeEmailIsSent(); - expect($emailMessage)->contains($this->model->name); - expect($emailMessage)->contains($this->model->email); - expect($emailMessage)->contains($this->model->subject); - expect($emailMessage)->contains($this->model->body); - } - - private function getMessageFile() - { - return \Yii::getAlias(\Yii::$app->mailer->fileTransportPath) . '/testing_message.eml'; + $emailMessage = $this->tester->grabLastSentEmail(); + expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface'); + expect($emailMessage->getTo())->hasKey('admin@example.com'); + expect($emailMessage->getFrom())->hasKey('tester@example.com'); + expect($emailMessage->getSubject())->equals('very important letter subject'); + expect($emailMessage->toString())->contains('body of current message'); } }