1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the JVal package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace JVal; |
11
|
|
|
|
12
|
|
|
use JVal\Testing\BaseTestCase; |
13
|
|
|
|
14
|
|
|
class WalkerTest extends BaseTestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Walker |
18
|
|
|
*/ |
19
|
|
|
private $walker; |
20
|
|
|
|
21
|
|
|
protected function setUp() |
22
|
|
|
{ |
23
|
|
|
$registry = new Registry(); |
24
|
|
|
$resolver = new Resolver(); |
25
|
|
|
$resolver->setPreFetchHook(function ($uri) { |
26
|
|
|
return str_replace( |
27
|
|
|
'http://localhost:1234', |
28
|
|
|
$this->getLocalUri(__DIR__.'/Data/schemas'), |
29
|
|
|
$uri |
30
|
|
|
); |
31
|
|
|
}); |
32
|
|
|
$this->walker = new Walker($registry, $resolver); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testResolveReferencesWithNoReferences() |
36
|
|
|
{ |
37
|
|
|
$schema = $this->loadSchema('valid/items-array'); |
38
|
|
|
$resolved = $this->walker->resolveReferences($schema, new Uri('file:///foo/bar')); |
39
|
|
|
$this->assertEquals($schema, $resolved); |
40
|
|
|
$this->assertSame($schema, $resolved); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function testResolveReferencesWithLocalReferences() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$schema = $this->loadSchema('valid/local-references'); |
46
|
|
|
$resolved = $this->walker->resolveReferences($schema, new Uri('file:///foo/bar')); |
47
|
|
|
$this->assertObjectHasAttribute('properties', $resolved); |
48
|
|
|
|
49
|
|
|
$this->assertObjectHasAttribute('foo', $resolved->properties); |
50
|
|
|
$this->assertObjectNotHasAttribute('$ref', $resolved->properties->foo); |
51
|
|
|
$this->assertSame($schema->definitions->foo, $resolved->properties->foo); |
52
|
|
|
|
53
|
|
|
$this->assertObjectHasAttribute('bar', $resolved->properties); |
54
|
|
|
$this->assertObjectNotHasAttribute('$ref', $resolved->properties->bar); |
55
|
|
|
$this->assertSame($schema->definitions->bar, $resolved->properties->bar); |
56
|
|
|
|
57
|
|
|
$this->assertObjectHasAttribute('items', $resolved); |
58
|
|
|
$this->assertArrayHasKey(0, $resolved->items); |
59
|
|
|
$this->assertSame($schema->definitions->baz[0], $resolved->items[0]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function testResolveReferencesWithRecursiveReferences() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$schema = $this->loadSchema('valid/recursive-references'); |
65
|
|
|
$resolved = $this->walker->resolveReferences($schema, new Uri('file:///foo/bar')); |
66
|
|
|
|
67
|
|
|
$this->assertObjectHasAttribute('properties', $resolved); |
68
|
|
|
|
69
|
|
|
$this->assertObjectHasAttribute('foo', $resolved->properties); |
70
|
|
|
$this->assertObjectNotHasAttribute('$ref', $resolved->properties->foo); |
71
|
|
|
$this->assertSame($resolved, $resolved->properties->foo); |
72
|
|
|
|
73
|
|
|
$this->assertObjectHasAttribute('bar', $resolved->properties); |
74
|
|
|
$this->assertObjectNotHasAttribute('$ref', $resolved->properties->bar); |
75
|
|
|
$this->assertSame($resolved->properties->bar, $resolved->definitions->bar); |
76
|
|
|
|
77
|
|
|
$this->assertObjectHasAttribute('baz', $resolved->properties); |
78
|
|
|
$this->assertObjectNotHasAttribute('$ref', $resolved->properties->baz); |
79
|
|
|
$this->assertSame($resolved, $resolved->properties->baz); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function testResolveReferencesWithOneAbsoluteRemoteReferenceOnly() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$schema = $this->loadSchema('valid/remote-absolute-reference'); |
85
|
|
|
$resolved = $this->walker->resolveReferences($schema, new Uri('file:///foo/bar')); |
86
|
|
|
$this->assertEquals(1, count(get_object_vars($resolved))); |
87
|
|
|
$this->assertObjectHasAttribute('maximum', $resolved); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function testResolveReferencesWithOneRelativeRemoteReferenceOnly() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$schema = $this->loadSchema('valid/remote-relative-reference'); |
93
|
|
|
$resolved = $this->walker->resolveReferences($schema, new Uri('http://localhost:1234/valid/base.json')); |
94
|
|
|
$this->assertEquals(1, count(get_object_vars($resolved))); |
95
|
|
|
$this->assertObjectHasAttribute('minimum', $resolved); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testResolveReferencesWithScopeChanges() |
99
|
|
|
{ |
100
|
|
|
$schema = $this->loadSchema('valid/scoped-references'); |
101
|
|
|
$resolved = $this->walker->resolveReferences($schema, new Uri('file:///foo/bar')); |
102
|
|
|
|
103
|
|
|
$this->assertObjectHasAttribute('properties', $resolved); |
104
|
|
|
|
105
|
|
|
$this->assertObjectHasAttribute('foo', $resolved->properties); |
106
|
|
|
$this->assertObjectHasAttribute('items', $resolved->properties->foo); |
107
|
|
|
|
108
|
|
|
$this->assertObjectHasAttribute('bar', $resolved->properties); |
109
|
|
|
$this->assertObjectHasAttribute('minimum', $resolved->properties->bar); |
110
|
|
|
|
111
|
|
|
$this->assertObjectHasAttribute('baz', $resolved->properties); |
112
|
|
|
$this->assertObjectHasAttribute('oneOf', $resolved->properties->baz); |
113
|
|
|
$this->assertArrayHasKey(0, $resolved->properties->baz->oneOf); |
114
|
|
|
$this->assertObjectHasAttribute('maximum', $resolved->properties->baz->oneOf[0]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testApplyConstraintsWithRecursiveReference() |
118
|
|
|
{ |
119
|
|
|
$schema = $this->loadSchema('valid/recursive-references'); |
120
|
|
|
$this->walker->parseSchema($schema, new Context()); |
121
|
|
|
|
122
|
|
|
$instance = new \stdClass(); |
123
|
|
|
$instance->foo = new \stdClass(); |
124
|
|
|
$instance->foo->foo = false; |
125
|
|
|
|
126
|
|
|
$this->walker->applyConstraints($instance, $schema, new Context()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.