JsonFileTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Json;
  12. use Seld\JsonLint\ParsingException;
  13. use Composer\Json\JsonFile;
  14. class JsonFileTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testParseErrorDetectExtraComma()
  17. {
  18. $json = '{
  19. "foo": "bar",
  20. }';
  21. $this->expectParseException('Parse error on line 2', $json);
  22. }
  23. public function testParseErrorDetectExtraCommaInArray()
  24. {
  25. $json = '{
  26. "foo": [
  27. "bar",
  28. ]
  29. }';
  30. $this->expectParseException('Parse error on line 3', $json);
  31. }
  32. public function testParseErrorDetectUnescapedBackslash()
  33. {
  34. $json = '{
  35. "fo\o": "bar"
  36. }';
  37. $this->expectParseException('Parse error on line 1', $json);
  38. }
  39. public function testParseErrorSkipsEscapedBackslash()
  40. {
  41. $json = '{
  42. "fo\\\\o": "bar"
  43. "a": "b"
  44. }';
  45. $this->expectParseException('Parse error on line 2', $json);
  46. }
  47. public function testParseErrorDetectSingleQuotes()
  48. {
  49. if (defined('JSON_PARSER_NOTSTRICT') && version_compare(phpversion('json'), '1.3.9', '<')) {
  50. $this->markTestSkipped('jsonc issue, see https://github.com/remicollet/pecl-json-c/issues/23');
  51. }
  52. $json = '{
  53. \'foo\': "bar"
  54. }';
  55. $this->expectParseException('Parse error on line 1', $json);
  56. }
  57. public function testParseErrorDetectMissingQuotes()
  58. {
  59. $json = '{
  60. foo: "bar"
  61. }';
  62. $this->expectParseException('Parse error on line 1', $json);
  63. }
  64. public function testParseErrorDetectArrayAsHash()
  65. {
  66. $json = '{
  67. "foo": ["bar": "baz"]
  68. }';
  69. $this->expectParseException('Parse error on line 2', $json);
  70. }
  71. public function testParseErrorDetectMissingComma()
  72. {
  73. $json = '{
  74. "foo": "bar"
  75. "bar": "foo"
  76. }';
  77. $this->expectParseException('Parse error on line 2', $json);
  78. }
  79. public function testSchemaValidation()
  80. {
  81. $json = new JsonFile(__DIR__.'/Fixtures/composer.json');
  82. $this->assertTrue($json->validateSchema());
  83. }
  84. public function testParseErrorDetectMissingCommaMultiline()
  85. {
  86. $json = '{
  87. "foo": "barbar"
  88. "bar": "foo"
  89. }';
  90. $this->expectParseException('Parse error on line 2', $json);
  91. }
  92. public function testParseErrorDetectMissingColon()
  93. {
  94. $json = '{
  95. "foo": "bar",
  96. "bar" "foo"
  97. }';
  98. $this->expectParseException('Parse error on line 3', $json);
  99. }
  100. public function testSimpleJsonString()
  101. {
  102. $data = array('name' => 'composer/composer');
  103. $json = '{
  104. "name": "composer/composer"
  105. }';
  106. $this->assertJsonFormat($json, $data);
  107. }
  108. public function testTrailingBackslash()
  109. {
  110. $data = array('Metadata\\' => 'src/');
  111. $json = '{
  112. "Metadata\\\\": "src/"
  113. }';
  114. $this->assertJsonFormat($json, $data);
  115. }
  116. public function testFormatEmptyArray()
  117. {
  118. $data = array('test' => array(), 'test2' => new \stdClass);
  119. $json = '{
  120. "test": [],
  121. "test2": {}
  122. }';
  123. $this->assertJsonFormat($json, $data);
  124. }
  125. public function testEscape()
  126. {
  127. $data = array("Metadata\\\"" => 'src/');
  128. $json = '{
  129. "Metadata\\\\\\"": "src/"
  130. }';
  131. $this->assertJsonFormat($json, $data);
  132. }
  133. public function testUnicode()
  134. {
  135. if (!function_exists('mb_convert_encoding') && PHP_VERSION_ID < 50400) {
  136. $this->markTestSkipped('Test requires the mbstring extension');
  137. }
  138. $data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €");
  139. $json = '{
  140. "Žluťoučký \" kůň": "úpěl ďábelské ódy za €"
  141. }';
  142. $this->assertJsonFormat($json, $data);
  143. }
  144. public function testOnlyUnicode()
  145. {
  146. if (!function_exists('mb_convert_encoding') && PHP_VERSION_ID < 50400) {
  147. $this->markTestSkipped('Test requires the mbstring extension');
  148. }
  149. $data = "\\/ƌ";
  150. $this->assertJsonFormat('"\\\\\\/ƌ"', $data, JsonFile::JSON_UNESCAPED_UNICODE);
  151. }
  152. public function testEscapedSlashes()
  153. {
  154. $data = "\\/foo";
  155. $this->assertJsonFormat('"\\\\\\/foo"', $data, 0);
  156. }
  157. public function testEscapedBackslashes()
  158. {
  159. $data = "a\\b";
  160. $this->assertJsonFormat('"a\\\\b"', $data, 0);
  161. }
  162. public function testEscapedUnicode()
  163. {
  164. $data = "ƌ";
  165. $this->assertJsonFormat('"\\u018c"', $data, 0);
  166. }
  167. public function testDoubleEscapedUnicode()
  168. {
  169. $jsonFile = new JsonFile('composer.json');
  170. $data = array("Zdjęcia","hjkjhl\\u0119kkjk");
  171. $encodedData = $jsonFile->encode($data);
  172. $doubleEncodedData = $jsonFile->encode(array('t' => $encodedData));
  173. $decodedData = json_decode($doubleEncodedData, true);
  174. $doubleData = json_decode($decodedData['t'], true);
  175. $this->assertEquals($data, $doubleData);
  176. }
  177. private function expectParseException($text, $json)
  178. {
  179. try {
  180. $result = JsonFile::parseJson($json);
  181. $this->fail(sprintf("Parsing should have failed but didn't.\nExpected:\n\"%s\"\nFor:\n\"%s\"\nGot:\n\"%s\"", $text, $json, var_export($result, true)));
  182. } catch (ParsingException $e) {
  183. $this->assertContains($text, $e->getMessage());
  184. }
  185. }
  186. private function assertJsonFormat($json, $data, $options = null)
  187. {
  188. $file = new JsonFile('composer.json');
  189. $json = str_replace("\r", '', $json);
  190. if (null === $options) {
  191. $this->assertEquals($json, $file->encode($data));
  192. } else {
  193. $this->assertEquals($json, $file->encode($data, $options));
  194. }
  195. }
  196. }