1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
<?php
declare(strict_types=1);
namespace JsonSchema\Tests\Entity;
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class JsonPointerTest extends TestCase
{
#[DataProvider('jsonPointerDataProvider')]
public function testJsonPointer(
string $testValue,
string $expectedFileName,
array $expectedPropertyPaths,
string $expectedPropertyPathAsString,
string $expectedToString
): void {
$jsonPointer = new JsonPointer($testValue);
$this->assertEquals($expectedFileName, $jsonPointer->getFilename());
$this->assertEquals($expectedPropertyPaths, $jsonPointer->getPropertyPaths());
$this->assertEquals($expectedPropertyPathAsString, $jsonPointer->getPropertyPathAsString());
$this->assertEquals($expectedToString, (string) $jsonPointer);
}
public static function jsonPointerDataProvider(): \Generator
{
yield 'testDataSet_01' => [
'testValue' => '#/definitions/date',
'expectedFileName' => '',
'expectedPropertyPaths' => ['definitions', 'date'],
'expectedPropertyPathAsString' => '#/definitions/date',
'expectedToString' => '#/definitions/date'
];
yield 'testDataSet_02' => [
'testValue' => 'https://www.example.com/definitions.json#/definitions/date',
'expectedFileName' => 'https://www.example.com/definitions.json',
'expectedPropertyPaths' => ['definitions', 'date'],
'expectedPropertyPathAsString' => '#/definitions/date',
'expectedToString' => 'https://www.example.com/definitions.json#/definitions/date'
];
yield 'testDataSet_03' => [
'testValue' => '/tmp/schema.json#definitions/common/date/',
'expectedFileName' => '/tmp/schema.json',
'expectedPropertyPaths' => ['definitions', 'common', 'date'],
'expectedPropertyPathAsString' => '#/definitions/common/date',
'expectedToString' => '/tmp/schema.json#/definitions/common/date'
];
yield 'testDataSet_04' => [
'testValue' => './definitions.json#',
'expectedFileName' => './definitions.json',
'expectedPropertyPaths' => [],
'expectedPropertyPathAsString' => '#',
'expectedToString' => './definitions.json#'
];
yield 'testDataSet_05' => [
'testValue' => '/schema.json#~0definitions~1general/%custom%25',
'expectedFileName' => '/schema.json',
'expectedPropertyPaths' => ['~definitions/general', '%custom%'],
'expectedPropertyPathAsString' => '#/~0definitions~1general/%25custom%25',
'expectedToString' => '/schema.json#/~0definitions~1general/%25custom%25'
];
yield 'testDataSet_06' => [
'testValue' => '#/items/0',
'expectedFileName' => '',
'expectedPropertyPaths' => ['items', '0'],
'expectedPropertyPathAsString' => '#/items/0',
'expectedToString' => '#/items/0'
];
}
public function testJsonPointerWithPropertyPaths(): void
{
$initial = new JsonPointer('#/definitions/date');
$this->assertEquals(['definitions', 'date'], $initial->getPropertyPaths());
$this->assertEquals('#/definitions/date', $initial->getPropertyPathAsString());
$modified = $initial->withPropertyPaths(['~definitions/general', '%custom%']);
$this->assertNotSame($initial, $modified);
$this->assertEquals(['definitions', 'date'], $initial->getPropertyPaths());
$this->assertEquals('#/definitions/date', $initial->getPropertyPathAsString());
$this->assertEquals(['~definitions/general', '%custom%'], $modified->getPropertyPaths());
$this->assertEquals('#/~0definitions~1general/%25custom%25', $modified->getPropertyPathAsString());
}
public function testCreateWithInvalidValue(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Ref value must be a string');
new JsonPointer(null);
}
}
|