JsonFileTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\JsonValidationException;
  14. use Composer\Json\JsonFile;
  15. class JsonFileTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testParseErrorDetectExtraComma()
  18. {
  19. $json = '{
  20. "foo": "bar",
  21. }';
  22. $this->expectParseException('Parse error on line 2', $json);
  23. }
  24. public function testParseErrorDetectExtraCommaInArray()
  25. {
  26. $json = '{
  27. "foo": [
  28. "bar",
  29. ]
  30. }';
  31. $this->expectParseException('Parse error on line 3', $json);
  32. }
  33. public function testParseErrorDetectUnescapedBackslash()
  34. {
  35. $json = '{
  36. "fo\o": "bar"
  37. }';
  38. $this->expectParseException('Parse error on line 1', $json);
  39. }
  40. public function testParseErrorSkipsEscapedBackslash()
  41. {
  42. $json = '{
  43. "fo\\\\o": "bar"
  44. "a": "b"
  45. }';
  46. $this->expectParseException('Parse error on line 2', $json);
  47. }
  48. public function testParseErrorDetectSingleQuotes()
  49. {
  50. $json = '{
  51. \'foo\': "bar"
  52. }';
  53. $this->expectParseException('Parse error on line 1', $json);
  54. }
  55. public function testParseErrorDetectMissingQuotes()
  56. {
  57. $json = '{
  58. foo: "bar"
  59. }';
  60. $this->expectParseException('Parse error on line 1', $json);
  61. }
  62. public function testParseErrorDetectArrayAsHash()
  63. {
  64. $json = '{
  65. "foo": ["bar": "baz"]
  66. }';
  67. $this->expectParseException('Parse error on line 2', $json);
  68. }
  69. public function testParseErrorDetectMissingComma()
  70. {
  71. $json = '{
  72. "foo": "bar"
  73. "bar": "foo"
  74. }';
  75. $this->expectParseException('Parse error on line 2', $json);
  76. }
  77. public function testSchemaValidation()
  78. {
  79. $json = new JsonFile(__DIR__.'/../../../../composer.json');
  80. $this->assertTrue($json->validateSchema());
  81. }
  82. public function testParseErrorDetectMissingCommaMultiline()
  83. {
  84. $json = '{
  85. "foo": "barbar"
  86. "bar": "foo"
  87. }';
  88. $this->expectParseException('Parse error on line 2', $json);
  89. }
  90. public function testParseErrorDetectMissingColon()
  91. {
  92. $json = '{
  93. "foo": "bar",
  94. "bar" "foo"
  95. }';
  96. $this->expectParseException('Parse error on line 3', $json);
  97. }
  98. public function testSimpleJsonString()
  99. {
  100. $data = array('name' => 'composer/composer');
  101. $json = '{
  102. "name": "composer/composer"
  103. }';
  104. $this->assertJsonFormat($json, $data);
  105. }
  106. public function testTrailingBackslash()
  107. {
  108. $data = array('Metadata\\' => 'src/');
  109. $json = '{
  110. "Metadata\\\\": "src/"
  111. }';
  112. $this->assertJsonFormat($json, $data);
  113. }
  114. public function testFormatEmptyArray()
  115. {
  116. $data = array('test' => array(), 'test2' => new \stdClass);
  117. $json = '{
  118. "test": [
  119. ],
  120. "test2": {
  121. }
  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') && version_compare(PHP_VERSION, '5.4', '<')) {
  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') && version_compare(PHP_VERSION, '5.4', '<')) {
  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 testEscapedUnicode()
  158. {
  159. $data = "ƌ";
  160. $this->assertJsonFormat('"\\u018c"', $data, 0);
  161. }
  162. private function expectParseException($text, $json)
  163. {
  164. try {
  165. JsonFile::parseJson($json);
  166. $this->fail();
  167. } catch (ParsingException $e) {
  168. $this->assertContains($text, $e->getMessage());
  169. }
  170. }
  171. private function assertJsonFormat($json, $data, $options = null)
  172. {
  173. $file = new JsonFile('composer.json');
  174. if (null === $options) {
  175. $this->assertEquals($json, $file->encode($data));
  176. } else {
  177. $this->assertEquals($json, $file->encode($data, $options));
  178. }
  179. }
  180. }