File: Draft3Test.php

package info (click to toggle)
php-json-schema 6.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,384 kB
  • sloc: php: 11,298; makefile: 153; python: 28; sh: 13
file content (122 lines) | stat: -rw-r--r-- 3,936 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Drafts;

use JsonSchema\Constraints\Factory;
use JsonSchema\DraftIdentifiers;
use JsonSchema\SchemaStorage;
use JsonSchema\Validator;
use PHPUnit\Framework\Attributes\DataProvider;

class Draft3Test extends BaseDraftTestCase
{
    /** @var string */
    protected $schemaSpec = DraftIdentifiers::DRAFT_3;
    /** @var bool */
    protected $validateSchema = true;

    /**
     * This test is a copy of https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft3/ref.json#L203-L225
     *
     * @todo cleanup when #821 gets merged
     *
     * @param mixed $data
     * @dataProvider refPreventsASiblingIdFromChangingTheBaseUriProvider
     */
    #[DataProvider('refPreventsASiblingIdFromChangingTheBaseUriProvider')]
    public function testRefPreventsASiblingIdFromChangingTheBaseUriProvider($data, bool $expectedResult): void
    {
        $schema = json_decode(<<<'JSON'
            {
                "id": "http://localhost:1234/sibling_id/base/",
                "definitions": {
                    "foo": {
                        "id": "http://localhost:1234/sibling_id/foo.json",
                        "type": "string"
                    },
                    "base_foo": {
                        "$comment": "this canonical uri is http://localhost:1234/sibling_id/base/foo.json",
                        "id": "foo.json",
                        "type": "number"
                    }
                },
                "extends": [
                    {
                        "$comment": "$ref resolves to http://localhost:1234/sibling_id/base/foo.json, not http://localhost:1234/sibling_id/foo.json",
                        "id": "http://localhost:1234/sibling_id/",
                        "$ref": "foo.json"
                    }
                ]
            }
JSON
        , false);

        $schemaStorage = new SchemaStorage();
        $schemaStorage->addSchema(property_exists($schema, 'id') ? $schema->id : 'internal://mySchema', $schema);
        $validator = new Validator(new Factory($schemaStorage));
        $validator->validate($data, $schema);

        self::assertEquals($expectedResult, $validator->isValid());
    }

    public function refPreventsASiblingIdFromChangingTheBaseUriProvider(): \Generator
    {
        yield '$ref resolves to /definitions/base_foo, data does not validate' => ['data' => 'a', 'valid' => false];
        yield '$ref resolves to /definitions/base_foo, data validate' => ['data' => 1, 'valid' => true];
    }

    /**
     * {@inheritdoc}
     */
    protected function getFilePaths(): array
    {
        return [
            realpath(__DIR__ . self::RELATIVE_TESTS_ROOT . '/draft3'),
            realpath(__DIR__ . self::RELATIVE_TESTS_ROOT . '/draft3/optional')
        ];
    }

    public function getInvalidTests(): \Generator
    {
        foreach (parent::getInvalidTests() as $name => $testcase) {
            yield $name => $testcase;
        }
    }

    public function getInvalidForAssocTests(): \Generator
    {
        $skip = [
            'type.json / object type matches objects / an array is not an object',
            'type.json / array type matches arrays / an object is not an array',
        ];

        foreach (parent::getInvalidForAssocTests() as $name => $testcase) {
            if (in_array($name, $skip, true)) {
                continue;
            }
            yield $name => $testcase;
        }
    }

    public function getValidForAssocTests(): \Generator
    {
        foreach (parent::getValidForAssocTests() as $name => $testcase) {
            yield $name => $testcase;
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function getSkippedTests(): array
    {
        return [
            // Optional
            'bignum.json',
            'ecmascript-regex.json',
            'zeroTerminatedFloats.json'
        ];
    }
}