Skip to content

Commit b79846d

Browse files
author
Igor Chepurnoy
committed
added tests for SettingsAction
1 parent 2bc1650 commit b79846d

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

tests/TestCase.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Yii;
66
use yii\helpers\ArrayHelper;
7+
use yii2mod\settings\tests\data\Controller;
78

89
/**
910
* This is the base class for all yii framework unit tests.
@@ -30,7 +31,7 @@ protected function tearDown()
3031
* @param array $config The application configuration, if needed
3132
* @param string $appClass name of the application class to create
3233
*/
33-
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
34+
protected function mockApplication($config = [], $appClass = '\yii\web\Application')
3435
{
3536
new $appClass(ArrayHelper::merge([
3637
'id' => 'testapp',
@@ -41,6 +42,10 @@ protected function mockApplication($config = [], $appClass = '\yii\console\Appli
4142
'class' => 'yii\db\Connection',
4243
'dsn' => 'sqlite::memory:',
4344
],
45+
'request' => [
46+
'hostInfo' => 'http://domain.com',
47+
'scriptUrl' => 'index.php',
48+
],
4449
'settings' => [
4550
'class' => 'yii2mod\settings\components\Settings',
4651
],
@@ -75,6 +80,16 @@ protected function destroyApplication()
7580
Yii::$app = null;
7681
}
7782

83+
/**
84+
* @param array $config controller config
85+
*
86+
* @return Controller controller instance
87+
*/
88+
protected function createController($config = [])
89+
{
90+
return new Controller('test', Yii::$app, $config);
91+
}
92+
7893
/**
7994
* Setup tables for test ActiveRecord
8095
*/
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace yii2mod\settings\tests\actions;
4+
5+
use Yii;
6+
use yii2mod\settings\actions\SettingsAction;
7+
use yii2mod\settings\tests\data\ConfigurationForm;
8+
use yii2mod\settings\tests\TestCase;
9+
10+
class SettingsActionTest extends TestCase
11+
{
12+
/**
13+
* @expectedException \yii\base\InvalidConfigException
14+
*/
15+
public function testCheckIncorrectConfig()
16+
{
17+
$this->runAction();
18+
}
19+
20+
public function testCheckValidation()
21+
{
22+
Yii::$app->request->bodyParams = [
23+
'ConfigurationForm' => [],
24+
];
25+
26+
$response = $this->runAction(['modelClass' => ConfigurationForm::class]);
27+
$this->assertTrue($response['params']['model']->hasErrors());
28+
}
29+
30+
public function testSaveSettingsSuccess()
31+
{
32+
Yii::$app->request->setUrl('index');
33+
Yii::$app->request->bodyParams = [
34+
'ConfigurationForm' => [
35+
'appName' => 'my-app',
36+
'adminEmail' => 'admin@example.org',
37+
],
38+
];
39+
40+
$response = $this->runAction(['modelClass' => ConfigurationForm::class]);
41+
42+
$this->assertEquals($response->getStatusCode(), 302);
43+
$this->assertTrue(Yii::$app->settings->has('ConfigurationForm', 'appName'));
44+
$this->assertTrue(Yii::$app->settings->has('ConfigurationForm', 'adminEmail'));
45+
$this->assertEquals(Yii::$app->settings->get('ConfigurationForm', 'appName'), 'my-app');
46+
$this->assertEquals(Yii::$app->settings->get('ConfigurationForm', 'adminEmail'), 'admin@example.org');
47+
}
48+
49+
public function testSetPrepareModelCallback()
50+
{
51+
$response = $this->runAction([
52+
'modelClass' => ConfigurationForm::class,
53+
'prepareModel' => function ($model) {
54+
foreach ($model->attributes() as $attribute) {
55+
$model->{$attribute} = 'test-value';
56+
}
57+
},
58+
]);
59+
60+
$this->assertEquals($response['params']['model']->appName, 'test-value');
61+
$this->assertEquals($response['params']['model']->adminEmail, 'test-value');
62+
}
63+
64+
/**
65+
* Runs the action.
66+
*
67+
* @param array $config
68+
*
69+
* @return string
70+
*/
71+
protected function runAction(array $config = [])
72+
{
73+
$action = new SettingsAction('settings', $this->createController(), $config);
74+
75+
return $action->run();
76+
}
77+
}

tests/data/ConfigurationForm.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace yii2mod\settings\tests\data;
4+
5+
use yii\base\Model;
6+
7+
class ConfigurationForm extends Model
8+
{
9+
/**
10+
* @var string application name
11+
*/
12+
public $appName;
13+
14+
/**
15+
* @var string admin email
16+
*/
17+
public $adminEmail;
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function rules()
23+
{
24+
return [
25+
[['appName', 'adminEmail'], 'required'],
26+
];
27+
}
28+
}

tests/data/Controller.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace yii2mod\settings\tests\data;
4+
5+
/**
6+
* Class Controller
7+
*
8+
* @package yii2mod\settings\tests\data
9+
*/
10+
class Controller extends \yii\web\Controller
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function render($view, $params = [])
16+
{
17+
return [
18+
'view' => $view,
19+
'params' => $params,
20+
];
21+
}
22+
}

0 commit comments

Comments
 (0)