for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace tests\unit\models;
use app\models\ContactForm;
use yii\mail\MessageInterface;
final class ContactFormTest extends \Codeception\Test\Unit
{
public \UnitTester $tester;
public function testEmailIsSentOnContact(): void
$model = new ContactForm();
$model->attributes = [
'name' => 'Tester',
'email' => '[email protected]',
'subject' => 'very important letter subject',
'body' => 'body of current message',
'verifyCode' => 'testme',
];
verify($model->contact('[email protected]'))->notEmpty();
// using Yii2 module actions to check email was sent
$this->tester->seeEmailIsSent();
/** @var MessageInterface $emailMessage */
$emailMessage = $this->tester->grabLastSentEmail();
verify($emailMessage)->instanceOf('yii\mail\MessageInterface');
verify($emailMessage->getTo())->arrayHasKey('[email protected]');
verify($emailMessage->getFrom())->arrayHasKey('[email protected]');
verify($emailMessage->getReplyTo())->arrayHasKey('[email protected]');
verify($emailMessage->getSubject())->equals('very important letter subject');
verify($emailMessage->toString())->stringContainsString('body of current message');
}