JsonFileTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €");
  118. $json = '{
  119. "Žluťoučký \" kůň": "úpěl ďábelské ódy za €"
  120. }';
  121. $this->assertJsonFormat($json, $data);
  122. }
  123. private function expectParseException($text, $json)
  124. {
  125. try {
  126. JsonFile::parseJson($json);
  127. $this->fail();
  128. } catch (\UnexpectedValueException $e) {
  129. $this->assertContains($text, $e->getMessage());
  130. }
  131. }
  132. private function assertJsonFormat($json, $data)
  133. {
  134. $file = new JsonFile('composer.json');
  135. $this->assertEquals($json, $file->encode($data));
  136. }
  137. }