Skip to content

Commit

Permalink
adds support for NAN and INF rendered as zero
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingmedia committed Oct 22, 2016
1 parent fbe0bf0 commit 4c8374d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/View/JsonView.php
Expand Up @@ -135,7 +135,9 @@ protected function _serialize($serialize)
{
$data = $this->_dataToSerialize($serialize);

$jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
$jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT |
JSON_ERROR_INF_OR_NAN | JSON_PARTIAL_OUTPUT_ON_ERROR;

if (isset($this->viewVars['_jsonOptions'])) {
if ($this->viewVars['_jsonOptions'] === false) {
$jsonOptions = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/View/View.php
Expand Up @@ -67,6 +67,8 @@
* @property \Cake\View\Helper\TimeHelper $Time
* @property \Cake\View\Helper\UrlHelper $Url
* @property \Cake\View\ViewBlock $Blocks
* @property string $view
* @property string $viewPath
*/
class View implements EventDispatcherInterface
{
Expand Down
31 changes: 25 additions & 6 deletions tests/TestCase/View/JsonViewTest.php
Expand Up @@ -39,7 +39,7 @@ public function setUp()
*
* Note: array($data, $serialize, expected)
*
* @return void
* @return array
*/
public static function renderWithoutViewProvider()
{
Expand Down Expand Up @@ -203,14 +203,33 @@ public static function renderWithoutViewProvider()
JSON_HEX_TAG | JSON_HEX_APOS,
json_encode('<tag> \'quote\' "double-quote" &', JSON_HEX_TAG | JSON_HEX_APOS)
],

// Test render of NAN
[
['value' => NAN],
true,
null,
'{"value":0}'
],

// Test render of INF
[
['value' => INF],
true,
null,
'{"value":0}'
],
];
}

/**
* Test render with a valid string in _serialize.
*
* @param array $data
* @param string|null $serialize
* @param int|bool|null $jsonOptions
* @param string $expected
* @dataProvider renderWithoutViewProvider
* @return void
*/
public function testRenderWithoutView($data, $serialize, $jsonOptions, $expected)
{
Expand All @@ -221,7 +240,7 @@ public function testRenderWithoutView($data, $serialize, $jsonOptions, $expected
$Controller->set($data);
$Controller->set('_serialize', $serialize);
$Controller->set('_jsonOptions', $jsonOptions);
$Controller->viewClass = 'Json';
$Controller->viewBuilder()->className('Json');
$View = $Controller->createView();
$output = $View->render(false);

Expand All @@ -244,7 +263,7 @@ public function testRenderSerializeNoHelpers()
'tags' => ['cakephp', 'framework'],
'_serialize' => 'tags'
]);
$Controller->viewClass = 'Json';
$Controller->viewBuilder()->className('Json');
$View = $Controller->createView();
$View->render();

Expand All @@ -268,7 +287,7 @@ public function testJsonpResponse()
'_serialize' => 'data',
'_jsonp' => true
]);
$Controller->viewClass = 'Json';
$Controller->viewBuilder()->className('Json');
$View = $Controller->createView();
$output = $View->render(false);

Expand Down Expand Up @@ -310,7 +329,7 @@ public function testRenderWithView()
]
];
$Controller->set('user', $data);
$Controller->viewClass = 'Json';
$Controller->viewBuilder()->className('Json');
$View = $Controller->createView();
$View->viewPath = $Controller->name;
$output = $View->render('index');
Expand Down

0 comments on commit 4c8374d

Please sign in to comment.