JsonFileTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 testSimpleJsonString()
  76. {
  77. $data = array('name' => 'composer/composer');
  78. $json = '{
  79. "name": "composer\/composer"
  80. }';
  81. $this->assertJsonFormat($json, $data);
  82. }
  83. private function expectParseException($text, $json)
  84. {
  85. try {
  86. JsonFile::parseJson($json);
  87. $this->fail();
  88. } catch (\UnexpectedValueException $e) {
  89. $this->assertContains($text, $e->getMessage());
  90. }
  91. }
  92. private function assertJsonFormat($json, $data)
  93. {
  94. $file = new JsonFile('composer.json');
  95. $this->assertEquals($json, $file->encode($data));
  96. }
  97. }