-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Igor Chepurnoy
committed
Feb 18, 2017
1 parent
2bc1650
commit b79846d
Showing
4 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |