JsonFileTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 testParseErrorDetectSingleQuotes()
  23. {
  24. $json = '{
  25. \'foo\': "bar"
  26. }';
  27. $this->expectParseException('use double quotes (") instead of single quotes (\') on line 2, char 9', $json);
  28. }
  29. public function testParseErrorDetectMissingQuotes()
  30. {
  31. $json = '{
  32. foo: "bar"
  33. }';
  34. $this->expectParseException('must use double quotes (") around keys on line 2, char 9', $json);
  35. }
  36. public function testParseErrorDetectArrayAsHash()
  37. {
  38. $json = '{
  39. "foo": ["bar": "baz"]
  40. }';
  41. $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);
  42. }
  43. public function testParseErrorDetectMissingComma()
  44. {
  45. $json = '{
  46. "foo": "bar"
  47. "bar": "foo"
  48. }';
  49. $this->expectParseException('missing comma on line 2, char 21', $json);
  50. }
  51. private function expectParseException($text, $json)
  52. {
  53. try {
  54. JsonFile::parseJson($json);
  55. $this->fail();
  56. } catch (\UnexpectedValueException $e) {
  57. $this->assertContains($text, $e->getMessage());
  58. }
  59. }
  60. }