Skip to content

Commit

Permalink
added tests for SettingsAction
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Chepurnoy committed Feb 18, 2017
1 parent 2bc1650 commit b79846d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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',
Expand All @@ -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',
],
Expand Down Expand Up @@ -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
*/
Expand Down
77 changes: 77 additions & 0 deletions tests/actions/SettingsActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace yii2mod\settings\tests\actions;

use Yii;
use yii2mod\settings\actions\SettingsAction;
use yii2mod\settings\tests\data\ConfigurationForm;
use yii2mod\settings\tests\TestCase;

class SettingsActionTest extends TestCase
{
/**
* @expectedException \yii\base\InvalidConfigException
*/
public function testCheckIncorrectConfig()
{
$this->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' => '[email protected]',
],
];

$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'), '[email protected]');
}

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();
}
}
28 changes: 28 additions & 0 deletions tests/data/ConfigurationForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace yii2mod\settings\tests\data;

use yii\base\Model;

class ConfigurationForm extends Model
{
/**
* @var string application name
*/
public $appName;

/**
* @var string admin email
*/
public $adminEmail;

/**
* @inheritdoc
*/
public function rules()
{
return [
[['appName', 'adminEmail'], 'required'],
];
}
}
22 changes: 22 additions & 0 deletions tests/data/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace yii2mod\settings\tests\data;

/**
* Class Controller
*
* @package yii2mod\settings\tests\data
*/
class Controller extends \yii\web\Controller
{
/**
* @inheritdoc
*/
public function render($view, $params = [])
{
return [
'view' => $view,
'params' => $params,
];
}
}

0 comments on commit b79846d

Please sign in to comment.