|
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\Constraint; |
|
11
|
|
|
|
|
12
|
|
|
use JVal\Context; |
|
13
|
|
|
use JVal\Testing\ConstraintTestCase; |
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
class RequiredConstraintTest extends ConstraintTestCase |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
public function testNormalizeThrowsIfRequiredIsNotArray() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->expectConstraintException('InvalidTypeException', '/required'); |
|
20
|
|
|
$schema = $this->loadSchema('invalid/required-not-array'); |
|
21
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testNormalizeThrowsIfRequiredIsEmpty() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->expectConstraintException('EmptyArrayException', '/required'); |
|
27
|
|
|
$schema = $this->loadSchema('invalid/required-empty'); |
|
28
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testNormalizeThrowsIfRequiredPropertyIsNotString() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->expectConstraintException('InvalidTypeException', '/required/1'); |
|
34
|
|
|
$schema = $this->loadSchema('invalid/required-property-not-string'); |
|
35
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testNormalizeThrowsIfRequiredPropertyIsNotUnique() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->expectConstraintException('NotUniqueException', '/required'); |
|
41
|
|
|
$schema = $this->loadSchema('invalid/required-property-not-unique'); |
|
42
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function getConstraint() |
|
46
|
|
|
{ |
|
47
|
|
|
return new RequiredConstraint(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function getCaseFileNames() |
|
51
|
|
|
{ |
|
52
|
|
|
return ['required']; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
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.