ExtJsonDecoderTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. namespace JsonMachineTest\JsonDecoder;
  4. use JsonMachine\JsonDecoder\ExtJsonDecoder;
  5. use PHPUnit_Framework_TestCase;
  6. /**
  7. * @covers \JsonMachine\JsonDecoder\ExtJsonDecoder
  8. */
  9. class ExtJsonDecoderTest extends PHPUnit_Framework_TestCase
  10. {
  11. public function testDefaultOptions()
  12. {
  13. $json = '{"bigint": 123456789123456789123456789, "deep": [["deeper"]]}';
  14. $noOptsDecoder = new ExtJsonDecoder();
  15. $defaultResult = $noOptsDecoder->decode($json);
  16. $this->assertTrue('object' === gettype($defaultResult->getValue()));
  17. $this->assertFalse('string' === gettype($defaultResult->getValue()->bigint));
  18. $this->assertSame([['deeper']], $defaultResult->getValue()->deep);
  19. }
  20. public function testPassesAssocTrueOptionToJsonDecode()
  21. {
  22. $json = '{"bigint": 123456789123456789123456789, "deep": [["deeper"]]}';
  23. $assocDecoder = new ExtJsonDecoder(true);
  24. $assocResult = $assocDecoder->decode($json);
  25. $this->assertTrue('array' === gettype($assocResult->getValue()));
  26. }
  27. public function testPassesAssocFalseOptionToJsonDecode()
  28. {
  29. $json = '{"bigint": 123456789123456789123456789, "deep": [["deeper"]]}';
  30. $objDecoder = new ExtJsonDecoder(false);
  31. $objResult = $objDecoder->decode($json);
  32. $this->assertTrue('object' === gettype($objResult->getValue()));
  33. }
  34. public function testPassesPassesDepthOptionToJsonDecode()
  35. {
  36. $json = '{"bigint": 123456789123456789123456789, "deep": [["deeper"]]}';
  37. $depthDecoder = new ExtJsonDecoder(true, 1);
  38. $depthResult = $depthDecoder->decode($json);
  39. $this->assertFalse($depthResult->isOk());
  40. $this->assertSame('Maximum stack depth exceeded', $depthResult->getErrorMessage());
  41. }
  42. public function testPassesPassesBigIntOptionToJsonDecode()
  43. {
  44. $bigintDecoder = new ExtJsonDecoder(false, 1, JSON_BIGINT_AS_STRING);
  45. $bigintResult = $bigintDecoder->decode('123123123123123123123');
  46. $this->assertSame('123123123123123123123', $bigintResult->getValue());
  47. }
  48. }