From b79846da6da4e14d275747b9fcc7e135c1a94645 Mon Sep 17 00:00:00 2001 From: Igor Chepurnoy Date: Sat, 18 Feb 2017 12:48:43 +0200 Subject: [PATCH] added tests for SettingsAction --- tests/TestCase.php | 17 +++++- tests/actions/SettingsActionTest.php | 77 ++++++++++++++++++++++++++++ tests/data/ConfigurationForm.php | 28 ++++++++++ tests/data/Controller.php | 22 ++++++++ 4 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 tests/actions/SettingsActionTest.php create mode 100644 tests/data/ConfigurationForm.php create mode 100644 tests/data/Controller.php diff --git a/tests/TestCase.php b/tests/TestCase.php index f62cffa..9023db7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,6 +4,7 @@ use Yii; use yii\helpers\ArrayHelper; +use yii2mod\settings\tests\data\Controller; /** * This is the base class for all yii framework unit tests. @@ -30,7 +31,7 @@ protected function tearDown() * @param array $config The application configuration, if needed * @param string $appClass name of the application class to create */ - protected function mockApplication($config = [], $appClass = '\yii\console\Application') + protected function mockApplication($config = [], $appClass = '\yii\web\Application') { new $appClass(ArrayHelper::merge([ 'id' => 'testapp', @@ -41,6 +42,10 @@ protected function mockApplication($config = [], $appClass = '\yii\console\Appli 'class' => 'yii\db\Connection', 'dsn' => 'sqlite::memory:', ], + 'request' => [ + 'hostInfo' => 'http://domain.com', + 'scriptUrl' => 'index.php', + ], 'settings' => [ 'class' => 'yii2mod\settings\components\Settings', ], @@ -75,6 +80,16 @@ protected function destroyApplication() Yii::$app = null; } + /** + * @param array $config controller config + * + * @return Controller controller instance + */ + protected function createController($config = []) + { + return new Controller('test', Yii::$app, $config); + } + /** * Setup tables for test ActiveRecord */ diff --git a/tests/actions/SettingsActionTest.php b/tests/actions/SettingsActionTest.php new file mode 100644 index 0000000..57146db --- /dev/null +++ b/tests/actions/SettingsActionTest.php @@ -0,0 +1,77 @@ +runAction(); + } + + public function testCheckValidation() + { + Yii::$app->request->bodyParams = [ + 'ConfigurationForm' => [], + ]; + + $response = $this->runAction(['modelClass' => ConfigurationForm::class]); + $this->assertTrue($response['params']['model']->hasErrors()); + } + + public function testSaveSettingsSuccess() + { + Yii::$app->request->setUrl('index'); + Yii::$app->request->bodyParams = [ + 'ConfigurationForm' => [ + 'appName' => 'my-app', + 'adminEmail' => 'admin@example.org', + ], + ]; + + $response = $this->runAction(['modelClass' => ConfigurationForm::class]); + + $this->assertEquals($response->getStatusCode(), 302); + $this->assertTrue(Yii::$app->settings->has('ConfigurationForm', 'appName')); + $this->assertTrue(Yii::$app->settings->has('ConfigurationForm', 'adminEmail')); + $this->assertEquals(Yii::$app->settings->get('ConfigurationForm', 'appName'), 'my-app'); + $this->assertEquals(Yii::$app->settings->get('ConfigurationForm', 'adminEmail'), 'admin@example.org'); + } + + public function testSetPrepareModelCallback() + { + $response = $this->runAction([ + 'modelClass' => ConfigurationForm::class, + 'prepareModel' => function ($model) { + foreach ($model->attributes() as $attribute) { + $model->{$attribute} = 'test-value'; + } + }, + ]); + + $this->assertEquals($response['params']['model']->appName, 'test-value'); + $this->assertEquals($response['params']['model']->adminEmail, 'test-value'); + } + + /** + * Runs the action. + * + * @param array $config + * + * @return string + */ + protected function runAction(array $config = []) + { + $action = new SettingsAction('settings', $this->createController(), $config); + + return $action->run(); + } +} diff --git a/tests/data/ConfigurationForm.php b/tests/data/ConfigurationForm.php new file mode 100644 index 0000000..cdb216c --- /dev/null +++ b/tests/data/ConfigurationForm.php @@ -0,0 +1,28 @@ + $view, + 'params' => $params, + ]; + } +}