1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\Zend\Validator; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\Zend\Validator\JsonSchema; |
6
|
|
|
use org\bovigo\vfs\vfsStream; |
7
|
|
|
use org\bovigo\vfs\vfsStreamFile; |
8
|
|
|
|
9
|
|
|
class JsonSchemaTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** @var vfsStreamFile */ |
12
|
|
|
private $schema; |
13
|
|
|
|
14
|
|
|
/** @var JsonSchema */ |
15
|
|
|
private $sut; |
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
$this->schema = vfsStream::newFile('schema.json'); |
22
|
|
|
|
23
|
|
|
$schemas = vfsStream::setup('schemas'); |
24
|
|
|
$schemas->addChild($this->schema); |
25
|
|
|
|
26
|
|
|
file_put_contents($this->schema->url(), json_encode(array( |
27
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
28
|
|
|
'type' => 'object', |
29
|
|
|
'properties' => array( |
30
|
|
|
'Foo' => array( |
31
|
|
|
'type' => 'string' |
32
|
|
|
), |
33
|
|
|
), |
34
|
|
|
))); |
35
|
|
|
|
36
|
|
|
$this->sut = new JsonSchema(array( |
37
|
|
|
'file' => $this->schema->url(), |
38
|
|
|
)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @expectedException \Zend\Validator\Exception\RuntimeException |
43
|
|
|
*/ |
44
|
|
|
public function testNotIsValidDueToUnspecifiedSchemaFile() |
45
|
|
|
{ |
46
|
|
|
$this->sut->setFile(null); |
47
|
|
|
|
48
|
|
|
$this->sut->isValid('{"Foo":"Bar"}'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @expectedException \Zend\Validator\Exception\RuntimeException |
53
|
|
|
*/ |
54
|
|
|
public function testNotIsValidDueToInvalidSchemaFile() |
55
|
|
|
{ |
56
|
|
|
$this->sut->setFile(__FILE__); |
57
|
|
|
|
58
|
|
|
$this->sut->isValid('{"Foo":"Bar"}'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testNotIsValidDueToInvalidSyntax() |
62
|
|
|
{ |
63
|
|
|
$actual = $this->sut->isValid('InvalidJsonString'); |
64
|
|
|
|
65
|
|
|
self::assertFalse($actual); |
66
|
|
|
|
67
|
|
|
self::assertCount(1, $this->sut->getMessages()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testIsValidEncodedJson() |
71
|
|
|
{ |
72
|
|
|
$actual = $this->sut->isValid('{"Foo":"Bar"}'); |
73
|
|
|
|
74
|
|
|
self::assertTrue($actual); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testIsValidDecodedJson() |
78
|
|
|
{ |
79
|
|
|
$actual = $this->sut->isValid(json_decode('{"Foo":"Bar"}')); |
80
|
|
|
|
81
|
|
|
self::assertTrue($actual); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testIsValidJsonDecodedArray() |
85
|
|
|
{ |
86
|
|
|
$actual = $this->sut->isValid(json_decode('{"Foo":"Bar"}', true)); |
87
|
|
|
|
88
|
|
|
self::assertTrue($actual); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testNotIsValidDueToSchemaValidation() |
92
|
|
|
{ |
93
|
|
|
$actual = $this->sut->isValid('{"Foo":1234}'); |
94
|
|
|
|
95
|
|
|
self::assertFalse($actual); |
96
|
|
|
|
97
|
|
|
self::assertCount(1, $this->sut->getMessages()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testDefaultMessageFormat() |
101
|
|
|
{ |
102
|
|
|
$actual = $this->sut->formatError(array( |
103
|
|
|
'property' => 'foo', |
104
|
|
|
'constraint' => 'bar', |
105
|
|
|
'message' => 'error', |
106
|
|
|
)); |
107
|
|
|
|
108
|
|
|
self::assertEquals('[property=foo] [constraint=bar] [message=error]', $actual); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testSetFile() |
112
|
|
|
{ |
113
|
|
|
$sut = new JsonSchema(); |
114
|
|
|
|
115
|
|
|
self::assertNull($sut->getFile()); |
116
|
|
|
|
117
|
|
|
$sut->setFile($expected = $this->schema->url()); |
118
|
|
|
|
119
|
|
|
self::assertEquals($expected, $sut->getFile()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testSetErrorKeys() |
123
|
|
|
{ |
124
|
|
|
self::assertArraySubset( |
125
|
|
|
array('property', 'constraint', 'message'), |
126
|
|
|
$this->sut->getErrorKeys() |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$this->sut->setErrorKeys(array('foo', 'bar')); |
130
|
|
|
|
131
|
|
|
self::assertArraySubset( |
132
|
|
|
array('foo', 'bar'), |
133
|
|
|
$this->sut->getErrorKeys() |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testSetMessagePrefix() |
138
|
|
|
{ |
139
|
|
|
self::assertEquals('[', $this->sut->getMessagePrefix()); |
140
|
|
|
|
141
|
|
|
$this->sut->setMessagePrefix($expected = '{'); |
142
|
|
|
|
143
|
|
|
self::assertEquals($expected, $this->sut->getMessagePrefix()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testSetMessageSuffix() |
147
|
|
|
{ |
148
|
|
|
self::assertEquals(']', $this->sut->getMessageSuffix()); |
149
|
|
|
|
150
|
|
|
$this->sut->setMessageSuffix($expected = '}'); |
151
|
|
|
|
152
|
|
|
self::assertEquals($expected, $this->sut->getMessageSuffix()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testSetMessageAttributeDelimiter() |
156
|
|
|
{ |
157
|
|
|
self::assertEquals('] [', $this->sut->getMessageAttributeDelimiter()); |
158
|
|
|
|
159
|
|
|
$this->sut->setMessageAttributeDelimiter($expected = '} {'); |
160
|
|
|
|
161
|
|
|
self::assertEquals($expected, $this->sut->getMessageAttributeDelimiter()); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|