Skip to content

Commit c31991b

Browse files
committed
update test suite
1 parent 98ffcb9 commit c31991b

File tree

9 files changed

+241
-6
lines changed

9 files changed

+241
-6
lines changed

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[submodule "spec/JSON-Schema-Test-Suite"]
22
path = spec/JSON-Schema-Test-Suite
3-
url = https://github.com/swaggest/JSON-Schema-Test-Suite.git
3+
url = https://github.com/json-schema-org/JSON-Schema-Test-Suite.git
44
[submodule "spec/ajv"]
55
path = spec/ajv
66
url = https://github.com/epoberezkin/ajv.git

src/Structure/ClassStructureTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function schema()
2828
$schema->objectItemClass = $className;
2929
$schemaWrapper = new Wrapper($schema);
3030
static::setUpProperties($properties, $schema);
31-
if (null === $schema->getFromRef()) {
31+
if (null === $schema->getFromRefs()) {
3232
$schema->setFromRef('#/definitions/' . $className);
3333
}
3434
if ($properties->isEmpty()) {

src/Structure/ObjectItemTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function getFromRef()
117117

118118
/**
119119
* @see ObjectItemContract::getFromRef
120-
* @return string
120+
* @return string[]|null
121121
*/
122122
public function getFromRefs()
123123
{
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"integer": {
3+
"type": "integer"
4+
},
5+
"refToInteger": {
6+
"$ref": "#/integer"
7+
},
8+
"indirectRefToInteger": {
9+
"allOf": [
10+
{},
11+
{
12+
"$ref": "#/refToInteger"
13+
}
14+
]
15+
},
16+
"refToExternalInteger": {
17+
"$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveIntegerDefault0"
18+
}
19+
}

tests/resources/suite/ref.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"schema": {
4+
"$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveIntegerDefault0"
5+
},
6+
"tests": [
7+
{
8+
"description": "valid against remote schema",
9+
"data": 1,
10+
"valid": true
11+
},
12+
{
13+
"description": "valid against remote schema",
14+
"data": -1,
15+
"valid": false
16+
}
17+
]
18+
}
19+
]

tests/resources/suite/refRemote.json

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
[
2+
{
3+
"description": "base URI change - change folder",
4+
"schema": {
5+
"id": "http://localhost:1234/scope_change_defs1.json",
6+
"type": "object",
7+
"properties": {
8+
"list": {
9+
"$ref": "#/definitions/baz"
10+
}
11+
},
12+
"definitions": {
13+
"baz": {
14+
"id": "folder/",
15+
"type": "array",
16+
"items": {
17+
"$ref": "folderInteger.json"
18+
}
19+
}
20+
}
21+
},
22+
"tests": [
23+
{
24+
"description": "number is valid",
25+
"data": {
26+
"list": [
27+
1
28+
]
29+
},
30+
"valid": true
31+
},
32+
{
33+
"description": "string is invalid",
34+
"data": {
35+
"list": [
36+
"a"
37+
]
38+
},
39+
"valid": false
40+
}
41+
]
42+
},
43+
{
44+
"description": "base URI change - change folder in subschema",
45+
"schema": {
46+
"id": "http://localhost:1234/scope_change_defs2.json",
47+
"type": "object",
48+
"properties": {
49+
"list": {
50+
"$ref": "#/definitions/baz/definitions/bar"
51+
}
52+
},
53+
"definitions": {
54+
"baz": {
55+
"id": "folder/",
56+
"definitions": {
57+
"bar": {
58+
"type": "array",
59+
"items": {
60+
"$ref": "folderInteger.json"
61+
}
62+
}
63+
}
64+
}
65+
}
66+
},
67+
"tests": [
68+
{
69+
"description": "number is valid",
70+
"data": {
71+
"list": [
72+
1
73+
]
74+
},
75+
"valid": true
76+
},
77+
{
78+
"description": "string is invalid",
79+
"data": {
80+
"list": [
81+
"a"
82+
]
83+
},
84+
"valid": false
85+
}
86+
]
87+
},
88+
{
89+
"description": "root ref in remote ref",
90+
"schema": {
91+
"id": "http://localhost:1234/object",
92+
"type": "object",
93+
"properties": {
94+
"name": {
95+
"$ref": "name.json#/definitions/orNull"
96+
}
97+
}
98+
},
99+
"tests": [
100+
{
101+
"description": "string is valid",
102+
"data": {
103+
"name": "foo"
104+
},
105+
"valid": true
106+
},
107+
{
108+
"description": "null is valid",
109+
"data": {
110+
"name": null
111+
},
112+
"valid": true
113+
},
114+
{
115+
"description": "object is invalid",
116+
"data": {
117+
"name": {
118+
"name": null
119+
}
120+
},
121+
"valid": false
122+
}
123+
]
124+
},
125+
{
126+
"description": "indirect ref within remote ref",
127+
"schema": {
128+
"$ref": "http://localhost:1234/subSchemas.json#/indirectRefToInteger"
129+
},
130+
"tests": [
131+
{
132+
"description": "indirect ref within ref valid",
133+
"data": 1,
134+
"valid": true
135+
},
136+
{
137+
"description": "indirect ref within ref invalid",
138+
"data": "a",
139+
"valid": false
140+
}
141+
]
142+
},
143+
{
144+
"description": "external ref within remote ref",
145+
"schema": {
146+
"$ref": "http://localhost:1234/subSchemas.json#/refToExternalInteger"
147+
},
148+
"tests": [
149+
{
150+
"description": "external ref within ref valid",
151+
"data": 1,
152+
"valid": true
153+
},
154+
{
155+
"description": "external ref within ref invalid",
156+
"data": "a",
157+
"valid": false
158+
}
159+
]
160+
},
161+
{
162+
"schema": {
163+
"$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveIntegerDefault0"
164+
},
165+
"tests": [
166+
{
167+
"description": "valid against remote schema",
168+
"data": 1,
169+
"valid": true
170+
},
171+
{
172+
"description": "valid against remote schema",
173+
"data": -1,
174+
"valid": false
175+
}
176+
]
177+
}
178+
]

tests/src/PHPUnit/Ref/RefTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Swaggest\JsonSchema\RemoteRef\Preloaded;
1212
use Swaggest\JsonSchema\Schema;
1313
use Swaggest\JsonSchema\Tests\PHPUnit\Spec\Draft4Test;
14+
use Swaggest\JsonSchema\Tests\PHPUnit\Suite\SuiteTest;
1415

1516
class RefTest extends \PHPUnit_Framework_TestCase
1617
{
@@ -355,7 +356,7 @@ public function testScopeChangeSubschema()
355356
JSON
356357
);
357358
$options = new Context();
358-
$options->remoteRefProvider = Draft4Test::getProvider();
359+
$options->remoteRefProvider = SuiteTest::getProvider();
359360
$schema = Schema::import($testData->schema, $options);
360361

361362
$schema->in($testData->tests[0]->data);
@@ -391,7 +392,7 @@ public function testExtRef()
391392
);
392393

393394
$schema = Schema::import($testData->schema, new Context(
394-
Draft4Test::getProvider())
395+
SuiteTest::getProvider())
395396
);
396397
$schema->in($testData->tests[0]->data);
397398
$this->setExpectedException(get_class(new InvalidValue()));

tests/src/PHPUnit/Suite/SuiteTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,28 @@
33
namespace Swaggest\JsonSchema\Tests\PHPUnit\Suite;
44

55

6+
use Swaggest\JsonSchema\RemoteRef\Preloaded;
67
use Swaggest\JsonSchema\Tests\PHPUnit\Spec\SchemaTestSuite;
78

89
class SuiteTest extends SchemaTestSuite
910
{
11+
public static function getProvider()
12+
{
13+
static $refProvider = null;
14+
15+
if (null === $refProvider) {
16+
$refProvider = parent::getProvider();
17+
$refProvider
18+
->setSchemaData(
19+
'http://localhost:1234/subSchemas.json',
20+
json_decode(file_get_contents(__DIR__
21+
. '/../../../resources/remotes/subSchemas.json')));
22+
}
23+
24+
return $refProvider;
25+
}
26+
27+
1028
protected function skipTest($name)
1129
{
1230
return false;

0 commit comments

Comments
 (0)