JsonFileTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 = file_get_contents(__DIR__.'/../../../../composer.json');
  79. try {
  80. $this->assertNull(JsonFile::validateSchema($json));
  81. } catch (\UnexpectedValueException $e) {
  82. $this->fail('invalid schema');
  83. }
  84. }
  85. public function testParseErrorDetectMissingCommaMultiline()
  86. {
  87. $json = '{
  88. "foo": "barbar"
  89. "bar": "foo"
  90. }';
  91. $this->expectParseException('Parse error on line 2', $json);
  92. }
  93. public function testParseErrorDetectMissingColon()
  94. {
  95. $json = '{
  96. "foo": "bar",
  97. "bar" "foo"
  98. }';
  99. $this->expectParseException('Parse error on line 3', $json);
  100. }
  101. public function testSimpleJsonString()
  102. {
  103. $data = array('name' => 'composer/composer');
  104. $json = '{
  105. "name": "composer/composer"
  106. }';
  107. $this->assertJsonFormat($json, $data);
  108. }
  109. public function testTrailingBackslash()
  110. {
  111. $data = array('Metadata\\' => 'src/');
  112. $json = '{
  113. "Metadata\\\\": "src/"
  114. }';
  115. $this->assertJsonFormat($json, $data);
  116. }
  117. public function testEscape()
  118. {
  119. $data = array("Metadata\\\"" => 'src/');
  120. $json = '{
  121. "Metadata\\\\\\"": "src/"
  122. }';
  123. $this->assertJsonFormat($json, $data);
  124. }
  125. public function testUnicode()
  126. {
  127. if (!function_exists('mb_convert_encoding')) {
  128. $this->markTestSkipped('Test requires the mbstring extension');
  129. }
  130. $data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €");
  131. $json = '{
  132. "Žluťoučký \" kůň": "úpěl ďábelské ódy za €"
  133. }';
  134. $this->assertJsonFormat($json, $data);
  135. }
  136. public function testEscapedSlashes()
  137. {
  138. if (!function_exists('mb_convert_encoding')) {
  139. $this->markTestSkipped('Test requires the mbstring extension');
  140. }
  141. $data = "\\/fooƌ";
  142. $this->assertJsonFormat('"\\\\\\/fooƌ"', $data, JSON_UNESCAPED_UNICODE);
  143. }
  144. public function testEscapedUnicode()
  145. {
  146. $data = "ƌ";
  147. $this->assertJsonFormat('"\\u018c"', $data, 0);
  148. }
  149. private function expectParseException($text, $json)
  150. {
  151. try {
  152. JsonFile::parseJson($json);
  153. $this->fail();
  154. } catch (ParsingException $e) {
  155. $this->assertContains($text, $e->getMessage());
  156. }
  157. }
  158. private function assertJsonFormat($json, $data, $options = null)
  159. {
  160. $file = new JsonFile('composer.json');
  161. if (null === $options) {
  162. $this->assertEquals($json, $file->encode($data));
  163. } else {
  164. $this->assertEquals($json, $file->encode($data, $options));
  165. }
  166. }
  167. }