JsonFileTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. $json = '{
  50. \'foo\': "bar"
  51. }';
  52. $this->expectParseException('Parse error on line 1', $json);
  53. }
  54. public function testParseErrorDetectMissingQuotes()
  55. {
  56. $json = '{
  57. foo: "bar"
  58. }';
  59. $this->expectParseException('Parse error on line 1', $json);
  60. }
  61. public function testParseErrorDetectArrayAsHash()
  62. {
  63. $json = '{
  64. "foo": ["bar": "baz"]
  65. }';
  66. $this->expectParseException('Parse error on line 2', $json);
  67. }
  68. public function testParseErrorDetectMissingComma()
  69. {
  70. $json = '{
  71. "foo": "bar"
  72. "bar": "foo"
  73. }';
  74. $this->expectParseException('Parse error on line 2', $json);
  75. }
  76. public function testSchemaValidation()
  77. {
  78. $json = new JsonFile(__DIR__.'/../../../../composer.json');
  79. $this->assertTrue($json->validateSchema());
  80. }
  81. public function testParseErrorDetectMissingCommaMultiline()
  82. {
  83. $json = '{
  84. "foo": "barbar"
  85. "bar": "foo"
  86. }';
  87. $this->expectParseException('Parse error on line 2', $json);
  88. }
  89. public function testParseErrorDetectMissingColon()
  90. {
  91. $json = '{
  92. "foo": "bar",
  93. "bar" "foo"
  94. }';
  95. $this->expectParseException('Parse error on line 3', $json);
  96. }
  97. public function testSimpleJsonString()
  98. {
  99. $data = array('name' => 'composer/composer');
  100. $json = '{
  101. "name": "composer/composer"
  102. }';
  103. $this->assertJsonFormat($json, $data);
  104. }
  105. public function testTrailingBackslash()
  106. {
  107. $data = array('Metadata\\' => 'src/');
  108. $json = '{
  109. "Metadata\\\\": "src/"
  110. }';
  111. $this->assertJsonFormat($json, $data);
  112. }
  113. public function testFormatEmptyArray()
  114. {
  115. $data = array('test' => array(), 'test2' => new \stdClass);
  116. if (
  117. (PHP_VERSION_ID < 50500 && PHP_VERSION_ID >= 50429)
  118. || (PHP_VERSION_ID < 50600 && PHP_VERSION_ID >= 50513)
  119. || (PHP_VERSION_ID >= 50600)
  120. ) {
  121. $json = '{
  122. "test": [],
  123. "test2": {}
  124. }';
  125. } else {
  126. $json = '{
  127. "test": [
  128. ],
  129. "test2": {
  130. }
  131. }';
  132. }
  133. $this->assertJsonFormat($json, $data);
  134. }
  135. public function testEscape()
  136. {
  137. $data = array("Metadata\\\"" => 'src/');
  138. $json = '{
  139. "Metadata\\\\\\"": "src/"
  140. }';
  141. $this->assertJsonFormat($json, $data);
  142. }
  143. public function testUnicode()
  144. {
  145. if (!function_exists('mb_convert_encoding') && version_compare(PHP_VERSION, '5.4', '<')) {
  146. $this->markTestSkipped('Test requires the mbstring extension');
  147. }
  148. $data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €");
  149. $json = '{
  150. "Žluťoučký \" kůň": "úpěl ďábelské ódy za €"
  151. }';
  152. $this->assertJsonFormat($json, $data);
  153. }
  154. public function testOnlyUnicode()
  155. {
  156. if (!function_exists('mb_convert_encoding') && version_compare(PHP_VERSION, '5.4', '<')) {
  157. $this->markTestSkipped('Test requires the mbstring extension');
  158. }
  159. $data = "\\/ƌ";
  160. $this->assertJsonFormat('"\\\\\\/ƌ"', $data, JsonFile::JSON_UNESCAPED_UNICODE);
  161. }
  162. public function testEscapedSlashes()
  163. {
  164. $data = "\\/foo";
  165. $this->assertJsonFormat('"\\\\\\/foo"', $data, 0);
  166. }
  167. public function testEscapedBackslashes()
  168. {
  169. $data = "a\\b";
  170. $this->assertJsonFormat('"a\\\\b"', $data, 0);
  171. }
  172. public function testEscapedUnicode()
  173. {
  174. $data = "ƌ";
  175. $this->assertJsonFormat('"\\u018c"', $data, 0);
  176. }
  177. public function testDoubleEscapedUnicode()
  178. {
  179. $jsonFile = new JsonFile('composer.json');
  180. $data = array("Zdjęcia","hjkjhl\\u0119kkjk");
  181. $encodedData = $jsonFile->encode($data);
  182. $doubleEncodedData = $jsonFile->encode(array('t' => $encodedData));
  183. $decodedData = json_decode($doubleEncodedData, true);
  184. $doubleData = json_decode($decodedData['t'], true);
  185. $this->assertEquals($data, $doubleData);
  186. }
  187. private function expectParseException($text, $json)
  188. {
  189. try {
  190. JsonFile::parseJson($json);
  191. $this->fail();
  192. } catch (ParsingException $e) {
  193. $this->assertContains($text, $e->getMessage());
  194. }
  195. }
  196. private function assertJsonFormat($json, $data, $options = null)
  197. {
  198. $file = new JsonFile('composer.json');
  199. if (null === $options) {
  200. $this->assertEquals($json, $file->encode($data));
  201. } else {
  202. $this->assertEquals($json, $file->encode($data, $options));
  203. }
  204. }
  205. }