|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Slavcodev\JsonPointer\Tests; |
| 6 | + |
| 7 | +use Slavcodev\JsonPointer\InvalidArgumentException; |
| 8 | +use Slavcodev\JsonPointer\JsonPointer; |
| 9 | +use function array_shift; |
| 10 | + |
| 11 | +final class JsonPointerTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @test |
| 15 | + * @dataProvider provideInvalidJsonPointers |
| 16 | + */ |
| 17 | + public function validatesValueOnConstruct(string $value): void |
| 18 | + { |
| 19 | + $this->expectExceptionObject(new InvalidArgumentException('Invalid JSON pointer syntax')); |
| 20 | + new JsonPointer($value); |
| 21 | + } |
| 22 | + |
| 23 | + public function provideInvalidJsonPointers(): iterable |
| 24 | + { |
| 25 | + return [ |
| 26 | + 'must start with token prefix' => ['foo'], |
| 27 | + 'must start with `#` representing URI fragment' => ['#foo'], |
| 28 | + 'must start with token prefix or `#`' => [' /'], |
| 29 | + 'must not contain unescaped `~`' => ['/~foo'], |
| 30 | + ]; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @test |
| 35 | + * @dataProvider provideJsonPointersAndUriFragments |
| 36 | + */ |
| 37 | + public function isInstantiatedFromString(string $value, array $expectedTokens, bool $anchored): void |
| 38 | + { |
| 39 | + $pointer = new JsonPointer($value); |
| 40 | + self::assertSame($value, $pointer->toString()); |
| 41 | + self::assertSame($value, (string) $pointer); |
| 42 | + self::assertSame($expectedTokens, $pointer->tokens); |
| 43 | + self::assertSame($anchored, $pointer->anchored); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @test |
| 48 | + */ |
| 49 | + public function isInstantiatedWithDefaultValue(): void |
| 50 | + { |
| 51 | + $pointer = new JsonPointer(); |
| 52 | + self::assertSame('', $pointer->toString()); |
| 53 | + self::assertSame('', (string) $pointer); |
| 54 | + self::assertSame([], $pointer->tokens); |
| 55 | + self::assertFalse($pointer->anchored); |
| 56 | + } |
| 57 | + |
| 58 | + public function provideJsonPointers(): iterable |
| 59 | + { |
| 60 | + return [ |
| 61 | + 'basic' => ['/foo', ['foo']], |
| 62 | + 'basic (1)' => ['/foo/bar', ['foo', 'bar']], |
| 63 | + 'empty' => ['', []], |
| 64 | + 'empty token' => ['/', ['']], |
| 65 | + 'empty tokens' => ['//', ['', '']], |
| 66 | + 'token with space' => ['/ ', [' ']], |
| 67 | + 'token with `%`' => ['/f%o', ['f%o']], |
| 68 | + 'token with `^`' => ['/f^o', ['f^o']], |
| 69 | + 'token with `|`' => ['/f|o', ['f|o']], |
| 70 | + 'token with `\\`' => ['/f\\o', ['f\\o']], |
| 71 | + 'token with `\'`' => ['/f\'o', ['f\'o']], |
| 72 | + 'token with NUL (Unicode U+0000)' => ["/f\0o", ["f\0o"]], |
| 73 | + 'token with `"`' => ['/f"o', ['f"o']], |
| 74 | + 'token with `/`' => ['/~1foo/bar~1/baz', ['/foo', 'bar/', 'baz']], |
| 75 | + 'token with `/` (1)' => ['/f~1o', ['f/o']], |
| 76 | + 'token with `~`' => ['/~0foo/bar~0/baz', ['~foo', 'bar~', 'baz']], |
| 77 | + 'token with `~` (1)' => ['/f~0o', ['f~o']], |
| 78 | + 'numeric token' => ['/foo/0', ['foo', '0']], |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + public function provideJsonPointersAndUriFragments(): iterable |
| 83 | + { |
| 84 | + foreach ($this->provideJsonPointers() as $key => $set) { |
| 85 | + $value = array_shift($set); |
| 86 | + yield "{$key} - {$value}" => [$value, ...$set, false]; |
| 87 | + yield "{$key} - #{$value}" => ["#{$value}", ...$set, true]; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments