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