added email verification for unit and functional tests

This commit is contained in:
Davert 2016-07-19 03:43:26 +03:00
parent 95bd8f4409
commit b7526436aa
5 changed files with 22 additions and 31 deletions

View File

@ -7,3 +7,4 @@ modules:
- Yii2: - Yii2:
part: orm part: orm
entryScript: index-test.php entryScript: index-test.php
cleanup: false

View File

@ -49,6 +49,7 @@ class ContactFormCest
'ContactForm[body]' => 'test content', 'ContactForm[body]' => 'test content',
'ContactForm[verifyCode]' => 'testme', 'ContactForm[verifyCode]' => 'testme',
]); ]);
$I->seeEmailIsSent();
$I->dontSeeElement('#contact-form'); $I->dontSeeElement('#contact-form');
$I->see('Thank you for contacting us. We will respond to you as soon as possible.'); $I->see('Thank you for contacting us. We will respond to you as soon as possible.');
} }

View File

@ -3,7 +3,7 @@ class LoginFormCest
{ {
public function _before(\FunctionalTester $I) public function _before(\FunctionalTester $I)
{ {
$I->amOnPage(['site/login']); $I->amOnRoute('site/login');
} }
public function openLoginPage(\FunctionalTester $I) public function openLoginPage(\FunctionalTester $I)
@ -12,18 +12,18 @@ class LoginFormCest
} }
// demonstrates `amLoggedAs` method // demonstrates `amLoggedInAs` method
public function internalLoginById(\FunctionalTester $I) public function internalLoginById(\FunctionalTester $I)
{ {
$I->amLoggedAs(100); $I->amLoggedInAs(100);
$I->amOnPage('/'); $I->amOnPage('/');
$I->see('Logout (admin)'); $I->see('Logout (admin)');
} }
// demonstrates `amLoggedAs` method // demonstrates `amLoggedInAs` method
public function internalLoginBy(\FunctionalTester $I) public function internalLoginByInstance(\FunctionalTester $I)
{ {
$I->amLoggedAs(\app\models\User::findByUsername('admin')); $I->amLoggedInAs(\app\models\User::findByUsername('admin'));
$I->amOnPage('/'); $I->amOnPage('/');
$I->see('Logout (admin)'); $I->see('Logout (admin)');
} }
@ -36,7 +36,7 @@ class LoginFormCest
$I->see('Password cannot be blank.'); $I->see('Password cannot be blank.');
} }
public function loginWithWringCredentials(\FunctionalTester $I) public function loginWithWrongCredentials(\FunctionalTester $I)
{ {
$I->submitForm('#login-form', [ $I->submitForm('#login-form', [
'LoginForm[username]' => 'admin', 'LoginForm[username]' => 'admin',

View File

@ -8,4 +8,4 @@ modules:
enabled: enabled:
- Asserts - Asserts
- Yii2: - Yii2:
part: orm part: [orm, email]

View File

@ -7,18 +7,10 @@ use app\models\ContactForm;
class ContactFormTest extends \Codeception\Test\Unit class ContactFormTest extends \Codeception\Test\Unit
{ {
private $model; private $model;
/**
protected function _before() * @var \UnitTester
{ */
\Yii::$app->mailer->fileTransportCallback = function () { public $tester;
return 'testing_message.eml';
};
}
protected function _after()
{
unlink($this->getMessageFile());
}
public function testEmailIsSentOnContact() public function testEmailIsSentOnContact()
{ {
@ -39,18 +31,15 @@ class ContactFormTest extends \Codeception\Test\Unit
]; ];
expect_that($this->model->contact('admin@example.com')); 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); $emailMessage = $this->tester->grabLastSentEmail();
expect($emailMessage)->contains($this->model->email); expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
expect($emailMessage)->contains($this->model->subject); expect($emailMessage->getTo())->hasKey('admin@example.com');
expect($emailMessage)->contains($this->model->body); expect($emailMessage->getFrom())->hasKey('tester@example.com');
} expect($emailMessage->getSubject())->equals('very important letter subject');
expect($emailMessage->toString())->contains('body of current message');
private function getMessageFile()
{
return \Yii::getAlias(\Yii::$app->mailer->fileTransportPath) . '/testing_message.eml';
} }
} }