ValidJsonPointersTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. namespace JsonMachineTest;
  4. use JsonMachine\Exception\InvalidArgumentException;
  5. use JsonMachine\ValidJsonPointers;
  6. /**
  7. * @covers \JsonMachine\ValidJsonPointers
  8. */
  9. class ValidJsonPointersTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @dataProvider data_testThrowsOnIntersectingPaths
  13. *
  14. * @param $jsonPointers
  15. */
  16. public function testThrowsOnIntersectingPaths($jsonPointers)
  17. {
  18. $this->expectException(InvalidArgumentException::class);
  19. $this->expectExceptionMessage("'$jsonPointers[0]', '$jsonPointers[1]'");
  20. (new ValidJsonPointers($jsonPointers))->toArray();
  21. }
  22. public function data_testThrowsOnIntersectingPaths()
  23. {
  24. return [
  25. [['/companies/-/id', '/companies/0/id']],
  26. [['/companies/-/id', '', '/companies/0/id']],
  27. [['/companies/-/id', '']],
  28. [['/companies/0/id', '']],
  29. [['//in-empty-string-key', '/']],
  30. [['/~0~1/in-escaped-key', '/~0~1']],
  31. ];
  32. }
  33. /**
  34. * @dataProvider data_testThrowsOnMalformedJsonPointer
  35. */
  36. public function testThrowsOnMalformedJsonPointer(array $jsonPointer)
  37. {
  38. $this->expectException(InvalidArgumentException::class);
  39. $this->expectExceptionMessage('not valid');
  40. (new ValidJsonPointers($jsonPointer))->toArray();
  41. }
  42. public function data_testThrowsOnMalformedJsonPointer()
  43. {
  44. return [
  45. [['apple']],
  46. [['/apple/~']],
  47. [['apple/pie']],
  48. [['apple/pie/']],
  49. [[' /apple/pie/']],
  50. [[
  51. '/valid',
  52. '/valid/-',
  53. 'inv/alid',
  54. ]],
  55. ];
  56. }
  57. public function testThrowsOnDuplicatePaths()
  58. {
  59. $this->expectException(InvalidArgumentException::class);
  60. $this->expectExceptionMessage("'/one', '/one'");
  61. (new ValidJsonPointers(['/one', '/one']))->toArray();
  62. }
  63. /**
  64. * @dataProvider data_testToArrayReturnsJsonPointers
  65. */
  66. public function testToArrayReturnsJsonPointers(string $pointerA, string $pointerB)
  67. {
  68. $this->assertSame(
  69. [$pointerA, $pointerB],
  70. (new ValidJsonPointers([$pointerA, $pointerB]))->toArray()
  71. );
  72. }
  73. public function data_testToArrayReturnsJsonPointers()
  74. {
  75. return [
  76. ['/one', '/two'],
  77. ['/companies/-/id', '/companies/0/idempotency_key'],
  78. ['/companies/1/id', '/companies/1/idempotency_key'],
  79. ];
  80. }
  81. }