JsonFileTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Composer\Json\JsonFile;
  13. class JsonFileTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testParseErrorDetectExtraComma()
  16. {
  17. $json = '{
  18. "foo": "bar",
  19. }';
  20. $this->expectParseException('extra comma on line 2, char 21', $json);
  21. }
  22. public function testParseErrorDetectExtraCommaInArray()
  23. {
  24. $json = '{
  25. "foo": [
  26. "bar",
  27. ]
  28. }';
  29. $this->expectParseException('extra comma on line 3, char 18', $json);
  30. }
  31. public function testParseErrorDetectUnescapedBackslash()
  32. {
  33. $json = '{
  34. "fo\o": "bar"
  35. }';
  36. $this->expectParseException('unescaped backslash (\\) on line 2, char 12', $json);
  37. }
  38. public function testParseErrorSkipsEscapedBackslash()
  39. {
  40. $json = '{
  41. "fo\\\\o": "bar"
  42. "a": "b"
  43. }';
  44. $this->expectParseException('missing comma on line 2, char 23', $json);
  45. }
  46. public function testParseErrorDetectSingleQuotes()
  47. {
  48. $json = '{
  49. \'foo\': "bar"
  50. }';
  51. $this->expectParseException('use double quotes (") instead of single quotes (\') on line 2, char 9', $json);
  52. }
  53. public function testParseErrorDetectMissingQuotes()
  54. {
  55. $json = '{
  56. foo: "bar"
  57. }';
  58. $this->expectParseException('must use double quotes (") around keys on line 2, char 9', $json);
  59. }
  60. public function testParseErrorDetectArrayAsHash()
  61. {
  62. $json = '{
  63. "foo": ["bar": "baz"]
  64. }';
  65. $this->expectParseException('you must use the hash syntax (e.g. {"foo": "bar"}) instead of array syntax (e.g. ["foo", "bar"]) on line 2, char 16', $json);
  66. }
  67. public function testParseErrorDetectMissingComma()
  68. {
  69. $json = '{
  70. "foo": "bar"
  71. "bar": "foo"
  72. }';
  73. $this->expectParseException('missing comma on line 2, char 21', $json);
  74. }
  75. public function testParseErrorDetectMissingCommaMultiline()
  76. {
  77. $json = '{
  78. "foo": "barbar"
  79. "bar": "foo"
  80. }';
  81. $this->expectParseException('missing comma on line 2, char 24', $json);
  82. }
  83. public function testParseErrorDetectMissingColon()
  84. {
  85. $json = '{
  86. "foo": "bar",
  87. "bar" "foo"
  88. }';
  89. $this->expectParseException('missing colon on line 3, char 14', $json);
  90. }
  91. public function testSimpleJsonString()
  92. {
  93. $data = array('name' => 'composer/composer');
  94. $json = '{
  95. "name": "composer/composer"
  96. }';
  97. $this->assertJsonFormat($json, $data);
  98. }
  99. public function testTrailingBackslash()
  100. {
  101. $data = array('Metadata\\' => 'src/');
  102. $json = '{
  103. "Metadata\\\\": "src/"
  104. }';
  105. $this->assertJsonFormat($json, $data);
  106. }
  107. public function testEscape()
  108. {
  109. $data = array("Metadata\\\"" => 'src/');
  110. $json = '{
  111. "Metadata\\\\\\"": "src/"
  112. }';
  113. $this->assertJsonFormat($json, $data);
  114. }
  115. public function testUnicode()
  116. {
  117. if (!function_exists('mb_convert_encoding')) {
  118. $this->markTestSkipped('Test requires the mbstring extension');
  119. }
  120. $data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €");
  121. $json = '{
  122. "Žluťoučký \" kůň": "úpěl ďábelské ódy za €"
  123. }';
  124. $this->assertJsonFormat($json, $data);
  125. }
  126. public function testEscapedSlashes()
  127. {
  128. if (!function_exists('mb_convert_encoding')) {
  129. $this->markTestSkipped('Test requires the mbstring extension');
  130. }
  131. $data = "\\/fooƌ";
  132. $this->assertJsonFormat('"\\\\\\/fooƌ"', $data, JSON_UNESCAPED_UNICODE);
  133. }
  134. public function testEscapedUnicode()
  135. {
  136. $data = "ƌ";
  137. $this->assertJsonFormat('"\\u018c"', $data, 0);
  138. }
  139. private function expectParseException($text, $json)
  140. {
  141. try {
  142. JsonFile::parseJson($json);
  143. $this->fail();
  144. } catch (\UnexpectedValueException $e) {
  145. $this->assertContains($text, $e->getMessage());
  146. }
  147. }
  148. private function assertJsonFormat($json, $data, $options = null)
  149. {
  150. $file = new JsonFile('composer.json');
  151. if (null === $options) {
  152. $this->assertEquals($json, $file->encode($data));
  153. } else {
  154. $this->assertEquals($json, $file->encode($data, $options));
  155. }
  156. }
  157. }