diff --git a/tests/framework/helpers/JsonTest.php b/tests/framework/helpers/JsonTest.php index 99e3558cda0..3db445f5af8 100644 --- a/tests/framework/helpers/JsonTest.php +++ b/tests/framework/helpers/JsonTest.php @@ -7,15 +7,15 @@ namespace yiiunit\framework\helpers; -use yii\base\DynamicModel; -use yii\helpers\BaseJson; use yii\helpers\Json; use yii\web\JsExpression; use yiiunit\framework\web\Post; +use yiiunit\framework\models\JsonModel; use yiiunit\TestCase; /** * @group helpers + * @coversDefaultClass \yii\helpers\Json */ class JsonTest extends TestCase { @@ -23,14 +23,14 @@ protected function setUp() { parent::setUp(); - // destroy application, Helper must work without Yii::$app + // destroy application, helper must work without Yii::$app $this->destroyApplication(); } public function testEncode() { // Arrayable data encoding - $dataArrayable = $this->getMockBuilder('yii\\base\\Arrayable')->getMock(); + $dataArrayable = $this->getMockBuilder('\yii\base\Arrayable')->getMock(); $dataArrayable->method('toArray')->willReturn([]); $actual = Json::encode($dataArrayable); $this->assertSame('[]', $actual); @@ -46,9 +46,7 @@ public function testEncode() $this->assertSame('{"a":1,"b":2}', Json::encode($data)); // simple object encoding - $data = new \stdClass(); - $data->a = 1; - $data->b = 2; + $data = (object) ['a' => 1, 'b' => 2]; $this->assertSame('{"a":1,"b":2}', Json::encode($data)); // simple object with zero indexed keys encoding @@ -78,9 +76,7 @@ public function testEncode() $expression1 = 'function (a) {}'; $expression2 = 'function (b) {}'; $data = [ - 'a' => [ - 1, new JsExpression($expression1), - ], + 'a' => [1, new JsExpression($expression1)], 'b' => new JsExpression($expression2), ]; $this->assertSame("{\"a\":[1,$expression1],\"b\":$expression2}", Json::encode($data)); @@ -92,7 +88,7 @@ public function testEncode() // JsonSerializable $data = new JsonModel(); $this->assertSame('{"json":"serializable"}', Json::encode($data)); - // @see https://github.com/yiisoft/yii2/issues/12043 + // https://github.com/yiisoft/yii2/issues/12043 $data = new JsonModel(); $data->data = []; $this->assertSame('[]', Json::encode($data)); @@ -118,10 +114,8 @@ public function testHtmlEncode() $this->assertSame('{"a":1,"b":2}', Json::htmlEncode($data)); // simple object encoding - $data = new \stdClass(); - $data->a = 1; - $data->b = 2; - $this->assertSame('{"a":1,"b":2}', Json::htmlEncode($data)); + $data = (object) ['a' => 1, 'b' => 'c']; + $this->assertSame('{"a":1,"b":"c"}', Json::htmlEncode($data)); // expression encoding $expression = 'function () {}'; @@ -132,9 +126,7 @@ public function testHtmlEncode() $expression1 = 'function (a) {}'; $expression2 = 'function (b) {}'; $data = [ - 'a' => [ - 1, new JsExpression($expression1), - ], + 'a' => [1, new JsExpression($expression1)], 'b' => new JsExpression($expression2), ]; $this->assertSame("{\"a\":[1,$expression1],\"b\":$expression2}", Json::htmlEncode($data)); @@ -195,40 +187,44 @@ public function testDecode() // exception $json = '{"a":1,"b":2'; - $this->expectException('yii\base\InvalidParamException'); + $this->expectException('\yii\base\InvalidArgumentException'); Json::decode($json); } /** - * @expectedException \yii\base\InvalidParamException + * @expectedException \yii\base\InvalidArgumentException * @expectedExceptionMessage Invalid JSON data. + * @covers ::decode */ public function testDecodeInvalidParamException() { Json::decode([]); } + /** + * @covers ::decode + */ public function testHandleJsonError() { - // Basic syntax error + // basic syntax error try { $json = "{'a': '1'}"; Json::decode($json); - } catch (\yii\base\InvalidParamException $e) { - $this->assertSame(BaseJson::$jsonErrorMessages['JSON_ERROR_SYNTAX'], $e->getMessage()); + } catch (\yii\base\InvalidArgumentException $e) { + $this->assertSame(Json::$jsonErrorMessages['JSON_ERROR_SYNTAX'], $e->getMessage()); } - // Unsupported type since PHP 5.5 + // unsupported type since PHP 5.5 try { $fp = fopen('php://stdin', 'r'); $data = ['a' => $fp]; Json::encode($data); fclose($fp); - } catch (\yii\base\InvalidParamException $e) { + } catch (\yii\base\InvalidArgumentException $e) { if (PHP_VERSION_ID >= 50500) { - $this->assertSame(BaseJson::$jsonErrorMessages['JSON_ERROR_UNSUPPORTED_TYPE'], $e->getMessage()); + $this->assertSame(Json::$jsonErrorMessages['JSON_ERROR_UNSUPPORTED_TYPE'], $e->getMessage()); } else { - $this->assertSame(BaseJson::$jsonErrorMessages['JSON_ERROR_SYNTAX'], $e->getMessage()); + $this->assertSame(Json::$jsonErrorMessages['JSON_ERROR_SYNTAX'], $e->getMessage()); } } } @@ -246,7 +242,8 @@ public function testErrorSummary() } /** - * @link https://github.com/yiisoft/yii2/issues/17760 + * @see https://github.com/yiisoft/yii2/issues/17760 + * @covers ::encode */ public function testEncodeDateTime() { @@ -255,6 +252,9 @@ public function testEncodeDateTime() $this->assertEquals('{"date":"2014-10-12 00:00:00.000000","timezone_type":3,"timezone":"UTC"}', $output); } + /** + * @covers ::encode + */ public function testPrettyPrint() { $defaultValue = Json::$prettyPrint; @@ -286,26 +286,3 @@ public function testPrettyPrint() Json::$prettyPrint = $defaultValue; } } - -class JsonModel extends DynamicModel implements \JsonSerializable -{ - public $data = ['json' => 'serializable']; - - public function jsonSerialize() - { - return $this->data; - } - - public function rules() - { - return [ - ['name', 'required'], - ['name', 'string', 'max' => 100] - ]; - } - - public function init() - { - $this->defineAttribute('name'); - } -} diff --git a/tests/framework/models/JsonModel.php b/tests/framework/models/JsonModel.php new file mode 100644 index 00000000000..f30dfce67f8 --- /dev/null +++ b/tests/framework/models/JsonModel.php @@ -0,0 +1,50 @@ + 'serializable']; + + /** + * @return array + */ + public function jsonSerialize() + { + return $this->data; + } + + /** + * @inheritdoc + */ + public function init() + { + $this->defineAttribute('name'); + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + ['name', 'required'], + ['name', 'string', 'max' => 100] + ]; + } +} diff --git a/tests/framework/web/Post.php b/tests/framework/web/Post.php index a2b3a6b0110..61255097ba7 100644 --- a/tests/framework/web/Post.php +++ b/tests/framework/web/Post.php @@ -9,14 +9,30 @@ use yii\base\BaseObject; +/** + * @inheritdoc + */ class Post extends BaseObject { + /** + * @var int + */ public $id; + /** + * @var string + */ public $title; - public function __construct($id, $title) + /** + * @param int $id + * @param string $title + * @param array $config + */ + public function __construct($id, $title, $config = []) { $this->id = $id; $this->title = $title; + + parent::__construct($config); } }