Skip to content

Commit

Permalink
Minor tests cleanup (#18811)
Browse files Browse the repository at this point in the history
  • Loading branch information
WinterSilence committed Sep 3, 2021
1 parent ac373c0 commit ffe7904
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 52 deletions.
79 changes: 28 additions & 51 deletions tests/framework/helpers/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@

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
{
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);
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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 () {}';
Expand All @@ -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));
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -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()
{
Expand All @@ -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;
Expand Down Expand Up @@ -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');
}
}
50 changes: 50 additions & 0 deletions tests/framework/models/JsonModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yiiunit\framework\models;

use yii\base\DynamicModel;

/**
* JSON serializable model for tests.
*
* {@inheritdoc}
*/
class JsonModel extends DynamicModel implements \JsonSerializable
{
/**
* @var array
*/
public $data = ['json' => '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]
];
}
}
18 changes: 17 additions & 1 deletion tests/framework/web/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit ffe7904

Please sign in to comment.