1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche/Validator component. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Validator\Tests\Units; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Validator\Assert; |
14
|
|
|
use Cubiche\Core\Validator\Exception\ValidationException; |
15
|
|
|
use Cubiche\Core\Validator\Mapping\Driver\StaticDriver; |
16
|
|
|
use Cubiche\Core\Validator\Tests\Fixtures\Blog; |
17
|
|
|
use Cubiche\Core\Validator\Tests\Fixtures\Post; |
18
|
|
|
use Cubiche\Core\Validator\Validator; |
19
|
|
|
use Cubiche\Core\Validator\ValidatorInterface; |
20
|
|
|
use Metadata\Driver\DriverChain; |
21
|
|
|
use Metadata\MetadataFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ValidatorTests class. |
25
|
|
|
* |
26
|
|
|
* Generated by TestGenerator on 2016-05-18 at 15:26:52. |
27
|
|
|
*/ |
28
|
|
|
class ValidatorTests extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @return Validator |
32
|
|
|
*/ |
33
|
|
|
public function creaateValidator() |
34
|
|
|
{ |
35
|
|
|
return Validator::create(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test create method. |
40
|
|
|
*/ |
41
|
|
|
public function testCreate() |
42
|
|
|
{ |
43
|
|
|
$this |
44
|
|
|
->given($validator = $this->creaateValidator()) |
45
|
|
|
->then() |
46
|
|
|
->object($validator) |
47
|
|
|
->isInstanceOf(ValidatorInterface::class) |
48
|
|
|
; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test setDefaultGroup method. |
53
|
|
|
*/ |
54
|
|
|
public function testSetDefaultGroup() |
55
|
|
|
{ |
56
|
|
|
$this |
57
|
|
|
->given($validator = $this->creaateValidator()) |
58
|
|
|
->when($validator->setDefaultGroup('foo')) |
59
|
|
|
->then() |
60
|
|
|
->boolean(true) |
61
|
|
|
->isTrue() |
62
|
|
|
; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test setMetadataFactory method. |
67
|
|
|
*/ |
68
|
|
|
public function testSetMetadataFactory() |
69
|
|
|
{ |
70
|
|
|
$this |
71
|
|
|
->given($validator = $this->creaateValidator()) |
72
|
|
|
->and($metadataFactory = new MetadataFactory(new DriverChain(array(new StaticDriver())))) |
73
|
|
|
->when($validator->setMetadataFactory($metadataFactory)) |
74
|
|
|
->then() |
75
|
|
|
->boolean(true) |
76
|
|
|
->isTrue() |
77
|
|
|
; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Test assert method. |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function testAssertExplicitConstraints() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$this |
86
|
|
|
->given($validator = $this->creaateValidator()) |
87
|
|
|
->then() |
88
|
|
|
->boolean($validator->assert('ivannis', Assert::alnum()->noWhitespace()->length(1, 15))) |
89
|
|
|
->isTrue() |
90
|
|
|
->boolean($validator->assert('ivannis', Assert::alnum()->noWhitespace()->length(1, 15), 'foo')) |
91
|
|
|
->isTrue() |
92
|
|
|
->exception(function () use ($validator) { |
93
|
|
|
$validator->assert('some tests', Assert::alnum()->noWhitespace()->length(1, 15)); |
94
|
|
|
})->isInstanceOf(ValidationException::class) |
95
|
|
|
->exception(function () use ($validator) { |
96
|
|
|
$validator->assert('value'); |
97
|
|
|
})->isInstanceOf(\RuntimeException::class) |
98
|
|
|
; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Test assert method. |
103
|
|
|
*/ |
104
|
|
|
public function testAssertObject() |
105
|
|
|
{ |
106
|
|
|
$this |
107
|
|
|
->given($validator = $this->creaateValidator()) |
108
|
|
|
->and($post = new Post('title', 'some content')) |
109
|
|
|
->and($post1 = new Post()) |
110
|
|
|
->and($post2 = new Post(3, 10)) |
111
|
|
|
->and($blog = new Blog()) |
112
|
|
|
->then() |
113
|
|
|
->boolean($validator->assert($post)) |
114
|
|
|
->isTrue() |
115
|
|
|
->boolean($validator->assert($post, null, 'bar')) |
116
|
|
|
->isTrue() |
117
|
|
|
->exception(function () use ($validator, $post1) { |
118
|
|
|
$validator->assert($post1); |
119
|
|
|
})->isInstanceOf(ValidationException::class) |
120
|
|
|
->exception(function () use ($validator, $post2) { |
121
|
|
|
$validator->assert($post2); |
122
|
|
|
})->isInstanceOf(ValidationException::class) |
123
|
|
|
->boolean($validator->assert($post2, null, 'foo')) |
124
|
|
|
->isTrue() |
125
|
|
|
->boolean($validator->assert($blog)) |
126
|
|
|
->isTrue() |
127
|
|
|
->boolean($validator->assert($blog, null, 'foo')) |
128
|
|
|
->isTrue() |
129
|
|
|
; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test assert method. |
134
|
|
|
*/ |
135
|
|
|
public function testAssertArrayObject() |
136
|
|
|
{ |
137
|
|
|
$this |
138
|
|
|
->given($validator = $this->creaateValidator()) |
139
|
|
|
->and($post = new Post('title', 'some content')) |
140
|
|
|
->and($post1 = new Post()) |
141
|
|
|
->and($post2 = new Post(3, 10)) |
142
|
|
|
->and($blog = new Blog()) |
143
|
|
|
->and($blog1 = new Blog()) |
144
|
|
|
->then() |
145
|
|
|
->boolean($validator->assert([$post, [$blog, $blog1]])) |
146
|
|
|
->isTrue() |
147
|
|
|
->boolean($validator->assert([$post, $blog], null, 'bar')) |
148
|
|
|
->isTrue() |
149
|
|
|
->exception(function () use ($validator, $post1, $blog) { |
150
|
|
|
$validator->assert([$post1, $blog, 300]); |
151
|
|
|
})->isInstanceOf(\RuntimeException::class) |
152
|
|
|
->exception(function () use ($validator, $post1, $blog) { |
153
|
|
|
$validator->assert([$post1, $blog]); |
154
|
|
|
})->isInstanceOf(ValidationException::class) |
155
|
|
|
->exception(function () use ($validator, $post2, $blog) { |
156
|
|
|
$validator->assert([$post2, $blog]); |
157
|
|
|
})->isInstanceOf(ValidationException::class) |
158
|
|
|
->boolean($validator->assert([$post2, $blog], null, 'foo')) |
159
|
|
|
->isTrue() |
160
|
|
|
; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Test validate method. |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function testValidateExplicitConstraints() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this |
169
|
|
|
->given($validator = $this->creaateValidator()) |
170
|
|
|
->then() |
171
|
|
|
->boolean($validator->validate('ivannis', Assert::alnum()->noWhitespace()->length(1, 15))) |
172
|
|
|
->isTrue() |
173
|
|
|
->boolean($validator->validate('ivannis', Assert::alnum()->noWhitespace()->length(1, 15), 'foo')) |
174
|
|
|
->isTrue() |
175
|
|
|
->boolean($validator->validate('some tests', Assert::alnum()->noWhitespace()->length(1, 15))) |
176
|
|
|
->isFalse() |
177
|
|
|
->exception(function () use ($validator) { |
178
|
|
|
$validator->validate('value'); |
179
|
|
|
})->isInstanceOf(\RuntimeException::class) |
180
|
|
|
; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Test validate method. |
185
|
|
|
*/ |
186
|
|
|
public function testValidateObject() |
187
|
|
|
{ |
188
|
|
|
$this |
189
|
|
|
->given($validator = $this->creaateValidator()) |
190
|
|
|
->and($post = new Post('title', 'some content')) |
191
|
|
|
->and($post1 = new Post()) |
192
|
|
|
->and($post2 = new Post(3, 10)) |
193
|
|
|
->and($blog = new Blog()) |
194
|
|
|
->then() |
195
|
|
|
->boolean($validator->validate($post)) |
196
|
|
|
->isTrue() |
197
|
|
|
->boolean($validator->validate($post, null, 'bar')) |
198
|
|
|
->isTrue() |
199
|
|
|
->boolean($validator->validate($post1)) |
200
|
|
|
->isFalse() |
201
|
|
|
->boolean($validator->validate($post2)) |
202
|
|
|
->isFalse() |
203
|
|
|
->boolean($validator->validate($post2, null, 'foo')) |
204
|
|
|
->isTrue() |
205
|
|
|
->boolean($validator->validate($blog)) |
206
|
|
|
->isTrue() |
207
|
|
|
->boolean($validator->validate($blog, null, 'foo')) |
208
|
|
|
->isTrue() |
209
|
|
|
; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Test validate method. |
214
|
|
|
*/ |
215
|
|
|
public function testValidateArrayObject() |
216
|
|
|
{ |
217
|
|
|
$this |
218
|
|
|
->given($validator = $this->creaateValidator()) |
219
|
|
|
->and($post = new Post('title', 'some content')) |
220
|
|
|
->and($post1 = new Post()) |
221
|
|
|
->and($post2 = new Post(3, 10)) |
222
|
|
|
->and($blog = new Blog()) |
223
|
|
|
->and($blog1 = new Blog()) |
224
|
|
|
->then() |
225
|
|
|
->boolean($validator->validate([$post, [$blog, $blog1]])) |
226
|
|
|
->isTrue() |
227
|
|
|
->boolean($validator->validate([$post, $blog], null, 'bar')) |
228
|
|
|
->isTrue() |
229
|
|
|
->exception(function () use ($validator, $post1, $blog) { |
230
|
|
|
$validator->validate([$post1, $blog, 300]); |
231
|
|
|
})->isInstanceOf(\RuntimeException::class) |
232
|
|
|
->boolean($validator->validate([$post1, $blog])) |
233
|
|
|
->isFalse() |
234
|
|
|
->boolean($validator->validate([$post2, $blog])) |
235
|
|
|
->isFalse() |
236
|
|
|
->boolean($validator->validate([$post2, $blog], null, 'foo')) |
237
|
|
|
->isTrue() |
238
|
|
|
; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
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.