1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Annotations\Metadata\Constraint; |
6
|
|
|
|
7
|
|
|
use Doctrine\Annotations\Annotation; |
8
|
|
|
use Doctrine\Annotations\Assembler\Validator\Constraint\Exception\ConstraintNotFulfilled; |
9
|
|
|
use Doctrine\Annotations\Assembler\Validator\Constraint\TypeConstraint; |
10
|
|
|
use Doctrine\Annotations\Metadata\Type\ObjectType; |
11
|
|
|
use Doctrine\Annotations\Metadata\Type\StringType; |
12
|
|
|
use Doctrine\Annotations\Metadata\Type\Type; |
13
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationTargetAll; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
final class TypeConstraintTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param mixed $value |
20
|
|
|
* |
21
|
|
|
* @dataProvider matchingProvider |
22
|
|
|
*/ |
23
|
|
|
public function testValidatesValueMatchingType(Type $type, $value) : void |
24
|
|
|
{ |
25
|
|
|
$constraint = new TypeConstraint($type); |
26
|
|
|
|
27
|
|
|
$constraint->validate($value); |
28
|
|
|
|
29
|
|
|
self::assertTrue(true); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return mixed[] |
34
|
|
|
*/ |
35
|
|
|
public function matchingProvider() : iterable |
36
|
|
|
{ |
37
|
|
|
yield 'a string' => [ |
|
|
|
|
38
|
|
|
new StringType(), |
39
|
|
|
'foo', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
yield 'an object' => [ |
43
|
|
|
new ObjectType(Annotation::class), |
44
|
|
|
new Annotation([]), |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param mixed $value |
50
|
|
|
* |
51
|
|
|
* @dataProvider notMatchingProvider |
52
|
|
|
*/ |
53
|
|
|
public function testValidatesValueNotMatchingTypeAndThrows(Type $type, $value) : void |
54
|
|
|
{ |
55
|
|
|
$constraint = new TypeConstraint($type); |
56
|
|
|
|
57
|
|
|
$this->expectException(ConstraintNotFulfilled::class); |
58
|
|
|
|
59
|
|
|
$constraint->validate($value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return mixed[] |
64
|
|
|
*/ |
65
|
|
|
public function notMatchingProvider() : iterable |
66
|
|
|
{ |
67
|
|
|
yield 'not a string' => [ |
|
|
|
|
68
|
|
|
new StringType(), |
69
|
|
|
42, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
yield 'a different object' => [ |
73
|
|
|
new ObjectType(AnnotationTargetAll::class), |
74
|
|
|
new Annotation([]), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|