| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Json
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Json
- */
- require_once 'Zend/Json.php';
- /**
- * @see Zend_Json_Expr
- */
- require_once 'Zend/Json/Expr.php';
- /**
- * @see Zend_Json_Encoder
- */
- require_once 'Zend/Json/Encoder.php';
- /**
- * @see Zend_Json_Decoder
- */
- require_once 'Zend/Json/Decoder.php';
- /**
- * @category Zend
- * @package Zend_Json
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Json
- */
- class Zend_JsonTest extends PHPUnit_Framework_TestCase
- {
- private $_originalUseBuiltinEncoderDecoderValue;
- public function setUp()
- {
- $this->_originalUseBuiltinEncoderDecoderValue = Zend_Json::$useBuiltinEncoderDecoder;
- }
- public function tearDown()
- {
- Zend_Json::$useBuiltinEncoderDecoder = $this->_originalUseBuiltinEncoderDecoderValue;
- }
- public function testJsonWithPhpJsonExtension()
- {
- if (!extension_loaded('json')) {
- $this->markTestSkipped('JSON extension is not loaded');
- }
- Zend_Json::$useBuiltinEncoderDecoder = false;
- $this->_testJson(array('string', 327, true, null));
- }
- public function testJsonWithBuiltins()
- {
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $this->_testJson(array('string', 327, true, null));
- }
- /**
- * Test encoding and decoding in a single step
- * @param array $values array of values to test against encode/decode
- */
- protected function _testJson($values)
- {
- $encoded = Zend_Json::encode($values);
- $this->assertEquals($values, Zend_Json::decode($encoded));
- }
- /**
- * test null encoding/decoding
- */
- public function testNull()
- {
- $this->_testEncodeDecode(array(null));
- }
- /**
- * test boolean encoding/decoding
- */
- public function testBoolean()
- {
- $this->assertTrue(Zend_Json_Decoder::decode(Zend_Json_Encoder::encode(true)));
- $this->assertFalse(Zend_Json_Decoder::decode(Zend_Json_Encoder::encode(false)));
- }
- /**
- * test integer encoding/decoding
- */
- public function testInteger()
- {
- $this->_testEncodeDecode(array(-2));
- $this->_testEncodeDecode(array(-1));
- $zero = Zend_Json_Decoder::decode(Zend_Json_Encoder::encode(0));
- $this->assertEquals(0, $zero, 'Failed 0 integer test. Encoded: ' . serialize(Zend_Json_Encoder::encode(0)));
- }
- /**
- * @group ZF-10185
- */
- public function testJsonPrettyPrintWorksWithArrayNotationInStringLiteral()
- {
- $o = new stdClass();
- $o->test = 1;
- $o->faz = 'fubar';
-
- // The escaped double-quote in item 'stringwithjsonchars' ensures that
- // escaped double-quotes don't throw off prettyPrint's string literal detection
- $test = array(
- 'simple'=>'simple test string',
- 'stringwithjsonchars'=>'\"[1,2]',
- 'complex'=>array(
- 'foo'=>'bar',
- 'far'=>'boo',
- 'faz'=>array(
- 'obj'=>$o
- )
- )
- );
- $pretty = Zend_Json::prettyPrint(Zend_Json::encode($test), array("indent" => " "));
- $expected = <<<EOB
- {
- "simple":"simple test string",
- "stringwithjsonchars":"\\\\\\"[1,2]",
- "complex":{
- "foo":"bar",
- "far":"boo",
- "faz":{
- "obj":{
- "test":1,
- "faz":"fubar"
- }
- }
- }
- }
- EOB;
- $this->assertSame($expected, $pretty);
- }
- /**
- * test float encoding/decoding
- */
- public function testFloat()
- {
- $this->_testEncodeDecode(array(-2.1, 1.2));
- }
- /**
- * test string encoding/decoding
- */
- public function testString()
- {
- $this->_testEncodeDecode(array('string'));
- $this->assertEquals('', Zend_Json_Decoder::decode(Zend_Json_Encoder::encode('')), 'Empty string encoded: ' . serialize(Zend_Json_Encoder::encode('')));
- }
- /**
- * Test backslash escaping of string
- */
- public function testString2()
- {
- $string = 'INFO: Path \\\\test\\123\\abc';
- $expected = '"INFO: Path \\\\\\\\test\\\\123\\\\abc"';
- $encoded = Zend_Json_Encoder::encode($string);
- $this->assertEquals($expected, $encoded, 'Backslash encoding incorrect: expected: ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
- $this->assertEquals($string, Zend_Json_Decoder::decode($encoded));
- }
- /**
- * Test newline escaping of string
- */
- public function testString3()
- {
- $expected = '"INFO: Path\nSome more"';
- $string = "INFO: Path\nSome more";
- $encoded = Zend_Json_Encoder::encode($string);
- $this->assertEquals($expected, $encoded, 'Newline encoding incorrect: expected ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
- $this->assertEquals($string, Zend_Json_Decoder::decode($encoded));
- }
- /**
- * Test tab/non-tab escaping of string
- */
- public function testString4()
- {
- $expected = '"INFO: Path\\t\\\\tSome more"';
- $string = "INFO: Path\t\\tSome more";
- $encoded = Zend_Json_Encoder::encode($string);
- $this->assertEquals($expected, $encoded, 'Tab encoding incorrect: expected ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
- $this->assertEquals($string, Zend_Json_Decoder::decode($encoded));
- }
- /**
- * Test double-quote escaping of string
- */
- public function testString5()
- {
- $expected = '"INFO: Path \"Some more\""';
- $string = 'INFO: Path "Some more"';
- $encoded = Zend_Json_Encoder::encode($string);
- $this->assertEquals($expected, $encoded, 'Quote encoding incorrect: expected ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
- $this->assertEquals($string, Zend_Json_Decoder::decode($encoded));
- }
- /**
- * test indexed array encoding/decoding
- */
- public function testArray()
- {
- $array = array(1, 'one', 2, 'two');
- $encoded = Zend_Json_Encoder::encode($array);
- $this->assertSame($array, Zend_Json_Decoder::decode($encoded), 'Decoded array does not match: ' . serialize($encoded));
- }
- /**
- * test associative array encoding/decoding
- */
- public function testAssocArray()
- {
- $this->_testEncodeDecode(array(array('one' => 1, 'two' => 2)));
- }
- /**
- * test associative array encoding/decoding, with mixed key types
- */
- public function testAssocArray2()
- {
- $this->_testEncodeDecode(array(array('one' => 1, 2 => 2)));
- }
- /**
- * test associative array encoding/decoding, with integer keys not starting at 0
- */
- public function testAssocArray3()
- {
- $this->_testEncodeDecode(array(array(1 => 'one', 2 => 'two')));
- }
- /**
- * test object encoding/decoding (decoding to array)
- */
- public function testObject()
- {
- $value = new stdClass();
- $value->one = 1;
- $value->two = 2;
- $array = array('__className' => 'stdClass', 'one' => 1, 'two' => 2);
- $encoded = Zend_Json_Encoder::encode($value);
- $this->assertSame($array, Zend_Json_Decoder::decode($encoded));
- }
- /**
- * test object encoding/decoding (decoding to stdClass)
- */
- public function testObjectAsObject()
- {
- $value = new stdClass();
- $value->one = 1;
- $value->two = 2;
- $encoded = Zend_Json_Encoder::encode($value);
- $decoded = Zend_Json_Decoder::decode($encoded, Zend_Json::TYPE_OBJECT);
- $this->assertTrue(is_object($decoded), 'Not decoded as an object');
- $this->assertTrue($decoded instanceof StdClass, 'Not a StdClass object');
- $this->assertTrue(isset($decoded->one), 'Expected property not set');
- $this->assertEquals($value->one, $decoded->one, 'Unexpected value');
- }
- /**
- * Test that arrays of objects decode properly; see issue #144
- */
- public function testDecodeArrayOfObjects()
- {
- $value = '[{"id":1},{"foo":2}]';
- $expect = array(array('id' => 1), array('foo' => 2));
- $this->assertEquals($expect, Zend_Json_Decoder::decode($value));
- }
- /**
- * Test that objects of arrays decode properly; see issue #107
- */
- public function testDecodeObjectOfArrays()
- {
- $value = '{"codeDbVar" : {"age" : ["int", 5], "prenom" : ["varchar", 50]}, "234" : [22, "jb"], "346" : [64, "francois"], "21" : [12, "paul"]}';
- $expect = array(
- 'codeDbVar' => array(
- 'age' => array('int', 5),
- 'prenom' => array('varchar', 50),
- ),
- 234 => array(22, 'jb'),
- 346 => array(64, 'francois'),
- 21 => array(12, 'paul')
- );
- $this->assertEquals($expect, Zend_Json_Decoder::decode($value));
- }
- /**
- * Test encoding and decoding in a single step
- * @param array $values array of values to test against encode/decode
- */
- protected function _testEncodeDecode($values)
- {
- foreach ($values as $value) {
- $encoded = Zend_Json_Encoder::encode($value);
- $this->assertEquals($value, Zend_Json_Decoder::decode($encoded));
- }
- }
- /**
- * Test that version numbers such as 4.10 are encoded and decoded properly;
- * See ZF-377
- */
- public function testEncodeReleaseNumber()
- {
- $value = '4.10';
- $this->_testEncodeDecode(array($value));
- }
- /**
- * Tests that spaces/linebreaks prior to a closing right bracket don't throw
- * exceptions. See ZF-283.
- */
- public function testEarlyLineBreak()
- {
- $expected = array('data' => array(1, 2, 3, 4));
- $json = '{"data":[1,2,3,4' . "\n]}";
- $this->assertEquals($expected, Zend_Json_Decoder::decode($json));
- $json = '{"data":[1,2,3,4 ]}';
- $this->assertEquals($expected, Zend_Json_Decoder::decode($json));
- }
- /**
- * Tests for ZF-504
- *
- * Three confirmed issues reported:
- * - encoder improperly encoding empty arrays as structs
- * - decoder happily decoding clearly borked JSON
- * - decoder decoding octal values improperly (shouldn't decode them at all, as JSON does not support them)
- */
- public function testZf504()
- {
- $test = array();
- $this->assertSame('[]', Zend_Json_Encoder::encode($test));
- try {
- $json = '[a"],["a],[][]';
- $test = Zend_Json_Decoder::decode($json);
- $this->fail("Should not be able to decode '$json'");
- $json = '[a"],["a]';
- $test = Zend_Json_Decoder::decode($json);
- $this->fail("Should not be able to decode '$json'");
- } catch (Exception $e) {
- // success
- }
- try {
- $expected = 010;
- $test = Zend_Json_Decoder::decode('010');
- $this->fail('Octal values are not supported in JSON notation');
- } catch (Exception $e) {
- // sucess
- }
- }
- /**
- * Tests for ZF-461
- *
- * Check to see that cycling detection works properly
- */
- public function testZf461()
- {
- $item1 = new Zend_JsonTest_Item() ;
- $item2 = new Zend_JsonTest_Item() ;
- $everything = array() ;
- $everything['allItems'] = array($item1, $item2) ;
- $everything['currentItem'] = $item1 ;
- try {
- $encoded = Zend_Json_Encoder::encode($everything);
- } catch (Exception $e) {
- $this->fail('Object cycling checks should check for recursion, not duplicate usage of an item');
- }
- try {
- $encoded = Zend_Json_Encoder::encode($everything, true);
- $this->fail('Object cycling not allowed when cycleCheck parameter is true');
- } catch (Exception $e) {
- // success
- }
- }
- /**
- * Test for ZF-4053
- *
- * Check to see that cyclical exceptions are silenced when
- * $option['silenceCyclicalExceptions'] = true is used
- */
- public function testZf4053()
- {
- $item1 = new Zend_JsonTest_Item() ;
- $item2 = new Zend_JsonTest_Item() ;
- $everything = array() ;
- $everything['allItems'] = array($item1, $item2) ;
- $everything['currentItem'] = $item1 ;
- $options = array('silenceCyclicalExceptions'=>true);
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $encoded = Zend_Json::encode($everything, true, $options);
- $json = '{"allItems":[{"__className":"Zend_JsonTest_Item"},{"__className":"Zend_JsonTest_Item"}],"currentItem":"* RECURSION (Zend_JsonTest_Item) *"}';
- $this->assertEquals($encoded,$json);
- }
- public function testEncodeObject()
- {
- $actual = new Zend_JsonTest_Object();
- $encoded = Zend_Json_Encoder::encode($actual);
- $decoded = Zend_Json_Decoder::decode($encoded, Zend_Json::TYPE_OBJECT);
- $this->assertTrue(isset($decoded->__className));
- $this->assertEquals('Zend_JsonTest_Object', $decoded->__className);
- $this->assertTrue(isset($decoded->foo));
- $this->assertEquals('bar', $decoded->foo);
- $this->assertTrue(isset($decoded->bar));
- $this->assertEquals('baz', $decoded->bar);
- $this->assertFalse(isset($decoded->_foo));
- }
- public function testEncodeClass()
- {
- $encoded = Zend_Json_Encoder::encodeClass('Zend_JsonTest_Object');
- $this->assertContains("Class.create('Zend_JsonTest_Object'", $encoded);
- $this->assertContains("ZAjaxEngine.invokeRemoteMethod(this, 'foo'", $encoded);
- $this->assertContains("ZAjaxEngine.invokeRemoteMethod(this, 'bar'", $encoded);
- $this->assertNotContains("ZAjaxEngine.invokeRemoteMethod(this, 'baz'", $encoded);
- $this->assertContains('variables:{foo:"bar",bar:"baz"}', $encoded);
- $this->assertContains('constants : {FOO: "bar"}', $encoded);
- }
- public function testEncodeClasses()
- {
- $encoded = Zend_Json_Encoder::encodeClasses(array('Zend_JsonTest_Object', 'Zend_JsonTest'));
- $this->assertContains("Class.create('Zend_JsonTest_Object'", $encoded);
- $this->assertContains("Class.create('Zend_JsonTest'", $encoded);
- }
- public function testToJsonSerialization()
- {
- $toJsonObject = new ToJsonClass();
- $result = Zend_Json::encode($toJsonObject);
- $this->assertEquals('{"firstName":"John","lastName":"Doe","email":"john@doe.com"}', $result);
- }
- /**
- * test encoding array with Zend_Json_Expr
- *
- * @group ZF-4946
- */
- public function testEncodingArrayWithExpr()
- {
- $expr = new Zend_Json_Expr('window.alert("Zend Json Expr")');
- $array = array('expr'=>$expr, 'int'=>9, 'string'=>'text');
- $result = Zend_Json::encode($array, false, array('enableJsonExprFinder' => true));
- $expected = '{"expr":window.alert("Zend Json Expr"),"int":9,"string":"text"}';
- $this->assertEquals($expected, $result);
- }
- /**
- * test encoding object with Zend_Json_Expr
- *
- * @group ZF-4946
- */
- public function testEncodingObjectWithExprAndInternalEncoder()
- {
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $expr = new Zend_Json_Expr('window.alert("Zend Json Expr")');
- $obj = new stdClass();
- $obj->expr = $expr;
- $obj->int = 9;
- $obj->string = 'text';
- $result = Zend_Json::encode($obj, false, array('enableJsonExprFinder' => true));
- $expected = '{"__className":"stdClass","expr":window.alert("Zend Json Expr"),"int":9,"string":"text"}';
- $this->assertEquals($expected, $result);
- }
- /**
- * test encoding object with Zend_Json_Expr
- *
- * @group ZF-4946
- */
- public function testEncodingObjectWithExprAndExtJson()
- {
- if(!function_exists('json_encode')) {
- $this->markTestSkipped('Test only works with ext/json enabled!');
- }
- Zend_Json::$useBuiltinEncoderDecoder = false;
- $expr = new Zend_Json_Expr('window.alert("Zend Json Expr")');
- $obj = new stdClass();
- $obj->expr = $expr;
- $obj->int = 9;
- $obj->string = 'text';
- $result = Zend_Json::encode($obj, false, array('enableJsonExprFinder' => true));
- $expected = '{"expr":window.alert("Zend Json Expr"),"int":9,"string":"text"}';
- $this->assertEquals($expected, $result);
- }
- /**
- * test encoding object with ToJson and Zend_Json_Expr
- *
- * @group ZF-4946
- */
- public function testToJsonWithExpr()
- {
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $obj = new Zend_Json_ToJsonWithExpr();
- $result = Zend_Json::encode($obj, false, array('enableJsonExprFinder' => true));
- $expected = '{"expr":window.alert("Zend Json Expr"),"int":9,"string":"text"}';
- $this->assertEquals($expected, $result);
- }
- /**
- * Regression tests for Zend_Json_Expr and mutliple keys with the same name.
- *
- * @group ZF-4946
- */
- public function testEncodingMultipleNestedSwitchingSameNameKeysWithDifferentJsonExprSettings()
- {
- $data = array(
- 0 => array(
- "alpha" => new Zend_Json_Expr("function(){}"),
- "beta" => "gamma",
- ),
- 1 => array(
- "alpha" => "gamma",
- "beta" => new Zend_Json_Expr("function(){}"),
- ),
- 2 => array(
- "alpha" => "gamma",
- "beta" => "gamma",
- )
- );
- $result = Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
- $this->assertEquals(
- '[{"alpha":function(){},"beta":"gamma"},{"alpha":"gamma","beta":function(){}},{"alpha":"gamma","beta":"gamma"}]',
- $result
- );
- }
- /**
- * Regression tests for Zend_Json_Expr and mutliple keys with the same name.
- *
- * @group ZF-4946
- */
- public function testEncodingMultipleNestedIteratedSameNameKeysWithDifferentJsonExprSettings()
- {
- $data = array(
- 0 => array(
- "alpha" => "alpha"
- ),
- 1 => array(
- "alpha" => "beta",
- ),
- 2 => array(
- "alpha" => new Zend_Json_Expr("gamma"),
- ),
- 3 => array(
- "alpha" => "delta",
- ),
- 4 => array(
- "alpha" => new Zend_Json_Expr("epsilon"),
- )
- );
- $result = Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
- $this->assertEquals('[{"alpha":"alpha"},{"alpha":"beta"},{"alpha":gamma},{"alpha":"delta"},{"alpha":epsilon}]', $result);
- }
- public function testDisabledJsonExprFinder()
- {
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $data = array(
- 0 => array(
- "alpha" => new Zend_Json_Expr("function(){}"),
- "beta" => "gamma",
- ),
- );
- $result = Zend_Json::encode($data);
- $this->assertEquals(
- '[{"alpha":{"__className":"Zend_Json_Expr"},"beta":"gamma"}]',
- $result
- );
- }
- /**
- * @group ZF-4054
- */
- public function testEncodeWithUtf8IsTransformedToPackedSyntax()
- {
- $data = array("Отмена");
- $result = Zend_Json_Encoder::encode($data);
- $this->assertEquals('["\u041e\u0442\u043c\u0435\u043d\u0430"]', $result);
- }
- /**
- * @group ZF-4054
- *
- * This test contains assertions from the Solar Framework by Paul M. Jones
- * @link http://solarphp.com
- */
- public function testEncodeWithUtf8IsTransformedSolarRegression()
- {
- $expect = '"h\u00c3\u00a9ll\u00c3\u00b6 w\u00c3\u00b8r\u00c5\u201ad"';
- $this->assertEquals($expect, Zend_Json_Encoder::encode('héllö wørłd'));
- $this->assertEquals('héllö wørłd', Zend_Json_Decoder::decode($expect));
- $expect = '"\u0440\u0443\u0441\u0441\u0438\u0448"';
- $this->assertEquals($expect, Zend_Json_Encoder::encode("руссиш"));
- $this->assertEquals("руссиш", Zend_Json_Decoder::decode($expect));
- }
- /**
- * @group ZF-4054
- */
- public function testEncodeUnicodeStringSolarRegression()
- {
- $value = 'héllö wørłd';
- $expected = 'h\u00c3\u00a9ll\u00c3\u00b6 w\u00c3\u00b8r\u00c5\u201ad';
- $this->assertEquals($expected, Zend_Json_Encoder::encodeUnicodeString($value));
- $value = "\xC3\xA4";
- $expected = '\u00e4';
- $this->assertEquals($expected, Zend_Json_Encoder::encodeUnicodeString($value));
- $value = "\xE1\x82\xA0\xE1\x82\xA8";
- $expected = '\u10a0\u10a8';
- $this->assertEquals($expected, Zend_Json_Encoder::encodeUnicodeString($value));
- }
- /**
- * @group ZF-4054
- */
- public function testDecodeUnicodeStringSolarRegression()
- {
- $expected = 'héllö wørłd';
- $value = 'h\u00c3\u00a9ll\u00c3\u00b6 w\u00c3\u00b8r\u00c5\u201ad';
- $this->assertEquals($expected, Zend_Json_Decoder::decodeUnicodeString($value));
- $expected = "\xC3\xA4";
- $value = '\u00e4';
- $this->assertEquals($expected, Zend_Json_Decoder::decodeUnicodeString($value));
- $value = '\u10a0';
- $expected = "\xE1\x82\xA0";
- $this->assertEquals($expected, Zend_Json_Decoder::decodeUnicodeString($value));
- }
- /**
- * @group ZF-4054
- *
- * This test contains assertions from the Solar Framework by Paul M. Jones
- * @link http://solarphp.com
- */
- public function testEncodeWithUtf8IsTransformedSolarRegressionEqualsJsonExt()
- {
- if(function_exists('json_encode') == false) {
- $this->markTestSkipped('Test can only be run, when ext/json is installed.');
- }
- $this->assertEquals(
- json_encode('héllö wørłd'),
- Zend_Json_Encoder::encode('héllö wørłd')
- );
- $this->assertEquals(
- json_encode("руссиш"),
- Zend_Json_Encoder::encode("руссиш")
- );
- }
- /**
- * @group ZF-4946
- */
- public function testUtf8JsonExprFinder()
- {
- $data = array("Отмена" => new Zend_Json_Expr("foo"));
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $result = Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
- $this->assertEquals('{"\u041e\u0442\u043c\u0435\u043d\u0430":foo}', $result);
- Zend_Json::$useBuiltinEncoderDecoder = false;
- $result = Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
- $this->assertEquals('{"\u041e\u0442\u043c\u0435\u043d\u0430":foo}', $result);
- }
- /**
- * @group ZF-4437
- */
- public function testKommaDecimalIsConvertedToCorrectJsonWithDot()
- {
- $localeInfo = localeconv();
- if($localeInfo['decimal_point'] != ",") {
- $this->markTestSkipped("This test only works for platforms where , is the decimal point separator.");
- }
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $this->assertEquals("[1.20, 1.68]", Zend_Json_Encoder::encode(array(
- (float)"1,20", (float)"1,68"
- )));
- }
- public function testEncodeObjectImplementingIterator()
- {
- $this->markTestIncomplete('Test is not yet finished.');
- }
- /**
- * @group ZF-8663
- */
- public function testNativeJsonEncoderWillProperlyEncodeSolidusInStringValues()
- {
- $source = "</foo><foo>bar</foo>";
- $target = '"<\\/foo><foo>bar<\\/foo>"';
- // first test ext/json
- Zend_Json::$useBuiltinEncoderDecoder = false;
- $this->assertEquals($target, Zend_Json::encode($source));
- }
- /**
- * @group ZF-8663
- */
- public function testBuiltinJsonEncoderWillProperlyEncodeSolidusInStringValues()
- {
- $source = "</foo><foo>bar</foo>";
- $target = '"<\\/foo><foo>bar<\\/foo>"';
- // first test ext/json
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $this->assertEquals($target, Zend_Json::encode($source));
- }
- /**
- * @group ZF-8918
- * @expectedException Zend_Json_Exception
- */
- public function testDecodingInvalidJsonShouldRaiseAnException()
- {
- Zend_Json::decode(' some string ');
- }
- /**
- * @group ZF-9416
- * Encoding an iterator using the internal encoder should handle undefined keys
- */
- public function testIteratorWithoutDefinedKey()
- {
- $inputValue = new ArrayIterator(array('foo'));
- $encoded = Zend_Json_Encoder::encode($inputValue);
- $expectedDecoding = '{"__className":"ArrayIterator","0":"foo"}';
- $this->assertEquals($encoded, $expectedDecoding);
- }
-
- /**
- * @group ZF-11356
- */
- public function testEncoderEscapesNamespacedClassNamesProperly()
- {
- if (version_compare(PHP_VERSION, '5.3.0') === -1) {
- $this->markTestSkipped('Namespaces not available in PHP < 5.3.0');
- }
-
- require_once dirname(__FILE__ ) . "/Json/_files/ZF11356-NamespacedClass.php";
- $className = '\Zend\JsonTest\ZF11356\NamespacedClass';
- $inputValue = new $className(array('foo'));
-
- $encoded = Zend_Json_Encoder::encode($inputValue);
- $this->assertEquals(
- '{"__className":"Zend\\\\JsonTest\\\\ZF11356\\\\NamespacedClass","0":"foo"}',
- $encoded
- );
- }
- /**
- * @group ZF-9577
- */
- public function testJsonPrettyPrintWorksWithTxtOutputFormat()
- {
- $o = new stdClass;
- $o->four = 4;
- $o->foo = array(1,2,3);
- $jsonstr = Zend_Json::encode($o);
- $targetTxtOutput = "{\n\t\"four\":4,\n\t\"foo\":[\n\t\t1,\n\t\t2,\n\t\t3\n\t]\n}";
- $this->assertEquals($targetTxtOutput, Zend_Json::prettyPrint($jsonstr));
- }
- /**
- * @group ZF-9577
- */
- public function testJsonPrettyPrintWorksWithHtmlOutputFormat()
- {
- $o = new stdClass;
- $o->four = 4;
- $o->foo = array(1,2,3);
- $jsonstr = Zend_Json::encode($o);
- $targetHtmlOutput = '{<br /> "four":4,<br /> "foo":[<br /> 1,<br /> 2,<br /> 3<br /> ]<br />}';
- $this->assertEquals($targetHtmlOutput, Zend_Json::prettyPrint($jsonstr, array('format' => 'html')));
- }
- /**
- * @group ZF-11167
- */
- public function testEncodeWillUseToArrayMethodWhenAvailable()
- {
- $o = new ZF11167_ToArrayClass();
- $objJson = Zend_Json::encode($o);
- $arrJson = Zend_Json::encode($o->toArray());
- $this->assertSame($arrJson, $objJson);
- }
- /**
- * @group ZF-11167
- */
- public function testEncodeWillUseToJsonWhenBothToJsonAndToArrayMethodsAreAvailable()
- {
- $o = new ZF11167_ToArrayToJsonClass();
- $objJson = Zend_Json::encode($o);
- $this->assertEquals('"bogus"', $objJson);
- $arrJson = Zend_Json::encode($o->toArray());
- $this->assertNotSame($objJson, $arrJson);
- }
- /**
- * @group ZF-9521
- */
- public function testWillEncodeArrayOfObjectsEachWithToJsonMethod()
- {
- $array = array('one'=>new ToJsonClass());
- $expected = '{"one":{"__className":"ToJsonClass","firstName":"John","lastName":"Doe","email":"john@doe.com"}}';
- Zend_Json::$useBuiltinEncoderDecoder = true;
- $json = Zend_Json::encode($array);
- $this->assertEquals($expected, $json);
- }
-
- /**
- * @group ZF-7586
- */
- public function testWillDecodeStructureWithEmptyKeyToObjectProperly()
- {
- Zend_Json::$useBuiltinEncoderDecoder = true;
-
- $json = '{"":"test"}';
- $object = Zend_Json::decode($json, Zend_Json::TYPE_OBJECT);
- $this->assertTrue(isset($object->_empty_));
- $this->assertEquals('test', $object->_empty_);
- }
- }
- /**
- * Zend_JsonTest_Item: test item for use with testZf461()
- */
- class Zend_JsonTest_Item
- {
- }
- /**
- * Zend_JsonTest_Object: test class for encoding classes
- */
- class Zend_JsonTest_Object
- {
- const FOO = 'bar';
- public $foo = 'bar';
- public $bar = 'baz';
- protected $_foo = 'fooled you';
- public function foo($bar, $baz)
- {
- }
- public function bar($baz)
- {
- }
- protected function baz()
- {
- }
- }
- class ToJsonClass
- {
- private $_firstName = 'John';
- private $_lastName = 'Doe';
- private $_email = 'john@doe.com';
- public function toJson()
- {
- $data = array(
- 'firstName' => $this->_firstName,
- 'lastName' => $this->_lastName,
- 'email' => $this->_email
- );
- return Zend_Json::encode($data);
- }
- }
- /**
- * Serializable class exposing a toArray() method
- * @see ZF-11167
- */
- class ZF11167_ToArrayClass
- {
- private $_firstName = 'John';
- private $_lastName = 'Doe';
- private $_email = 'john@doe.com';
- public function toArray()
- {
- $data = array(
- 'firstName' => $this->_firstName,
- 'lastName' => $this->_lastName,
- 'email' => $this->_email
- );
- return $data;
- }
- }
- /**
- * Serializable class exposing both toArray() and toJson() methods
- * @see ZF-11167
- */
- class ZF11167_ToArrayToJsonClass extends ZF11167_ToArrayClass
- {
- public function toJson()
- {
- return Zend_Json::encode('bogus');
- }
- }
- /**
- * ISSUE ZF-4946
- *
- */
- class Zend_Json_ToJsonWithExpr
- {
- private $_string = 'text';
- private $_int = 9;
- private $_expr = 'window.alert("Zend Json Expr")';
- public function toJson()
- {
- $data = array(
- 'expr' => new Zend_Json_Expr($this->_expr),
- 'int' => $this->_int,
- 'string' => $this->_string
- );
- return Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
- }
- }
|