Jelajahi Sumber

modified tests, improved configuration, introduced aspect mock and specify.

Mark 12 tahun lalu
induk
melakukan
fc0df16f47

+ 4 - 1
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": [

+ 0 - 4
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__);

+ 3 - 0
tests/acceptance/_bootstrap.php

@@ -1,3 +1,6 @@
 <?php
 
+require_once(__DIR__ . '/../../vendor/yiisoft/yii2/yii/Yii.php');
+Yii::setAlias('@tests', __DIR__ . '/../');
+
 new yii\web\Application(require(__DIR__ . '/_config.php'));

+ 3 - 0
tests/functional/_bootstrap.php

@@ -1,3 +1,6 @@
 <?php
 
+require_once(__DIR__ . '/../../vendor/yiisoft/yii2/yii/Yii.php');
+Yii::setAlias('@tests', __DIR__ . '/../');
+
 new yii\web\Application(require(__DIR__ . '/_config.php'));

+ 3 - 1
tests/unit/_bootstrap.php

@@ -1,3 +1,5 @@
 <?php
 
-// add unit testing specific bootstrap code here
+#aspect-mock should be included only once. Codeception calls this bootstrap file per each test file.
+require_once __DIR__ . '/aspect_mock.php';
+Yii::setAlias('@tests', __DIR__ . '/../');

+ 16 - 0
tests/unit/aspect_mock.php

@@ -0,0 +1,16 @@
+<?php
+
+$kernel = AspectMock\Kernel::getInstance();
+$kernel->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');

+ 54 - 1
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';
+	}
+
 }

+ 61 - 1
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,
+		]);
+	}
+
 }