2016-05-16 04:03:19 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\behaviors;
|
2016-05-16 04:03:19 +05:30
|
|
|
|
|
|
|
use Codeception\Specify;
|
|
|
|
use common\behaviors\DataBehavior;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\_support\ProtectedCaller;
|
|
|
|
use common\tests\unit\TestCase;
|
2016-05-16 04:03:19 +05:30
|
|
|
use yii\base\ErrorException;
|
|
|
|
use yii\base\Model;
|
|
|
|
|
|
|
|
class DataBehaviorTest extends TestCase {
|
|
|
|
use Specify;
|
|
|
|
use ProtectedCaller;
|
|
|
|
|
|
|
|
public function testSetKey() {
|
2016-10-29 05:53:29 +05:30
|
|
|
$model = $this->createModel();
|
|
|
|
/** @var DataBehavior $behavior */
|
|
|
|
$behavior = $model->behaviors['dataBehavior'];
|
|
|
|
$this->callProtected($behavior, 'setKey', 'my-key', 'my-value');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(serialize(['my-key' => 'my-value']), $model->_data);
|
2016-05-16 04:03:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetKey() {
|
2016-10-29 05:53:29 +05:30
|
|
|
$model = $this->createModel();
|
|
|
|
$model->_data = serialize(['some-key' => 'some-value']);
|
|
|
|
/** @var DataBehavior $behavior */
|
|
|
|
$behavior = $model->behaviors['dataBehavior'];
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('some-value', $this->callProtected($behavior, 'getKey', 'some-key'));
|
2016-05-16 04:03:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetData() {
|
|
|
|
$this->specify('getting value from null field should return empty array', function() {
|
|
|
|
$model = $this->createModel();
|
|
|
|
/** @var DataBehavior $behavior */
|
|
|
|
$behavior = $model->behaviors['dataBehavior'];
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame([], $this->callProtected($behavior, 'getData'));
|
2016-05-16 04:03:19 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('getting value from serialized data field should return encoded value', function() {
|
|
|
|
$model = $this->createModel();
|
|
|
|
$data = ['foo' => 'bar'];
|
|
|
|
$model->_data = serialize($data);
|
|
|
|
/** @var DataBehavior $behavior */
|
|
|
|
$behavior = $model->behaviors['dataBehavior'];
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame($data, $this->callProtected($behavior, 'getData'));
|
2016-05-16 04:03:19 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('getting value from invalid serialization string', function() {
|
|
|
|
$model = $this->createModel();
|
|
|
|
$model->_data = 'this is invalid serialization of string';
|
|
|
|
/** @var DataBehavior $behavior */
|
|
|
|
$behavior = $model->behaviors['dataBehavior'];
|
|
|
|
$this->expectException(ErrorException::class);
|
|
|
|
$this->callProtected($behavior, 'getData');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Model
|
|
|
|
*/
|
|
|
|
private function createModel() {
|
|
|
|
return new class extends Model {
|
|
|
|
public $_data;
|
|
|
|
|
|
|
|
public function behaviors() {
|
|
|
|
return [
|
|
|
|
'dataBehavior' => [
|
|
|
|
'class' => DataBehavior::class,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|