7
7
* file that was distributed with this source code.
8
8
*/
9
9
10
+ declare (strict_types=1 );
11
+
10
12
namespace JsonSchema \Tests \Constraints ;
11
13
12
14
use JsonSchema \Constraints \Constraint ;
13
15
use JsonSchema \Constraints \Factory ;
14
16
use JsonSchema \Entity \JsonPointer ;
15
17
use PHPUnit \Framework \TestCase ;
18
+ use JsonSchema \Constraints ;
19
+ use JsonSchema \Constraints \ConstraintInterface ;
20
+ use JsonSchema \Exception \InvalidArgumentException ;
16
21
17
- /**
18
- * Class MyBadConstraint
19
- *
20
- * @package JsonSchema\Tests\Constraints
21
- */
22
22
class MyBadConstraint
23
23
{
24
24
}
25
25
26
- /**
27
- * Class MyStringConstraint
28
- *
29
- * @package JsonSchema\Tests\Constraints
30
- */
31
26
class MyStringConstraint extends Constraint
32
27
{
33
28
public function check (&$ value , $ schema = null , ?JsonPointer $ path = null , $ i = null )
@@ -37,110 +32,125 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
37
32
38
33
class FactoryTest extends TestCase
39
34
{
40
- /**
41
- * @var Factory
42
- */
43
- protected $ factory ;
44
-
45
- protected function setUp (): void
46
- {
47
- $ this ->factory = new Factory ();
48
- }
49
-
50
35
/**
51
36
* @dataProvider constraintNameProvider
52
- *
53
- * @param string $constraintName
54
- * @param string $expectedClass
55
37
*/
56
- public function testCreateInstanceForConstraintName ($ constraintName , $ expectedClass ): void
38
+ public function testCreateInstanceForConstraintName (string $ constraintName , string $ expectedClass ): void
57
39
{
58
- $ constraint = $ this ->factory ->createInstanceFor ($ constraintName );
40
+ $ factory = new Factory ();
41
+ $ constraint = $ factory ->createInstanceFor ($ constraintName );
59
42
60
43
$ this ->assertInstanceOf ($ expectedClass , $ constraint );
61
- $ this ->assertInstanceOf (' JsonSchema\Constraints\ ConstraintInterface' , $ constraint );
44
+ $ this ->assertInstanceOf (ConstraintInterface::class , $ constraint );
62
45
}
63
46
64
- public function constraintNameProvider (): array
47
+ public static function constraintNameProvider (): \ Generator
65
48
{
66
- return [
67
- ['array ' , 'JsonSchema\Constraints\CollectionConstraint ' ],
68
- ['collection ' , 'JsonSchema\Constraints\CollectionConstraint ' ],
69
- ['object ' , 'JsonSchema\Constraints\ObjectConstraint ' ],
70
- ['type ' , 'JsonSchema\Constraints\TypeConstraint ' ],
71
- ['undefined ' , 'JsonSchema\Constraints\UndefinedConstraint ' ],
72
- ['string ' , 'JsonSchema\Constraints\StringConstraint ' ],
73
- ['number ' , 'JsonSchema\Constraints\NumberConstraint ' ],
74
- ['enum ' , 'JsonSchema\Constraints\EnumConstraint ' ],
75
- ['const ' , 'JsonSchema\Constraints\ConstConstraint ' ],
76
- ['format ' , 'JsonSchema\Constraints\FormatConstraint ' ],
77
- ['schema ' , 'JsonSchema\Constraints\SchemaConstraint ' ],
78
- ];
49
+ yield 'Array ' => ['array ' , Constraints \CollectionConstraint::class];
50
+ yield 'Collection ' => ['collection ' , Constraints \CollectionConstraint::class];
51
+ yield 'Object ' => ['object ' , Constraints \ObjectConstraint::class];
52
+ yield 'Type ' => ['type ' , Constraints \TypeConstraint::class];
53
+ yield 'Undefined ' => ['undefined ' , Constraints \UndefinedConstraint::class];
54
+ yield 'String ' => ['string ' , Constraints \StringConstraint::class];
55
+ yield 'Number ' => ['number ' , Constraints \NumberConstraint::class];
56
+ yield 'Enum ' => ['enum ' , Constraints \EnumConstraint::class];
57
+ yield 'Const ' => ['const ' , Constraints \ConstConstraint::class];
58
+ yield 'Format ' => ['format ' , Constraints \FormatConstraint::class];
59
+ yield 'Schema ' => ['schema ' , Constraints \SchemaConstraint::class];
79
60
}
80
61
81
62
/**
82
63
* @dataProvider invalidConstraintNameProvider
83
- *
84
- * @param string $constraintName
85
64
*/
86
- public function testExceptionWhenCreateInstanceForInvalidConstraintName ($ constraintName ): void
65
+ public function testExceptionWhenCreateInstanceForInvalidConstraintName (string $ constraintName ): void
87
66
{
88
- $ this ->expectException ('JsonSchema\Exception\InvalidArgumentException ' );
89
- $ this ->factory ->createInstanceFor ($ constraintName );
67
+ $ factory = new Factory ();
68
+
69
+ $ this ->expectException (InvalidArgumentException::class);
70
+
71
+ $ factory ->createInstanceFor ($ constraintName );
90
72
}
91
73
92
- public function invalidConstraintNameProvider (): array
74
+ public static function invalidConstraintNameProvider (): \ Generator
93
75
{
94
- return [
95
- ['invalidConstraintName ' ],
96
- ];
76
+ yield 'InvalidConstraint ' => ['invalidConstraintName ' ];
97
77
}
98
78
99
79
public function testSetConstraintClassExistsCondition (): void
100
80
{
81
+ $ factory = new Factory ();
82
+
101
83
$ this ->expectException (\JsonSchema \Exception \InvalidArgumentException::class);
102
84
103
- $ this -> factory ->setConstraintClass ('string ' , 'SomeConstraint ' );
85
+ $ factory ->setConstraintClass ('string ' , 'SomeConstraint ' );
104
86
}
105
87
106
88
public function testSetConstraintClassImplementsCondition (): void
107
89
{
90
+ $ factory = new Factory ();
91
+
108
92
$ this ->expectException (\JsonSchema \Exception \InvalidArgumentException::class);
109
93
110
- $ this -> factory ->setConstraintClass ('string ' , ' JsonSchema\Tests\Constraints\ MyBadConstraint' );
94
+ $ factory ->setConstraintClass ('string ' , MyBadConstraint::class );
111
95
}
112
96
113
97
public function testSetConstraintClassInstance (): void
114
98
{
115
- $ this ->factory ->setConstraintClass ('string ' , 'JsonSchema\Tests\Constraints\MyStringConstraint ' );
116
- $ constraint = $ this ->factory ->createInstanceFor ('string ' );
117
- $ this ->assertInstanceOf ('JsonSchema\Tests\Constraints\MyStringConstraint ' , $ constraint );
118
- $ this ->assertInstanceOf ('JsonSchema\Constraints\ConstraintInterface ' , $ constraint );
99
+ $ factory = new Factory ();
100
+ $ factory ->setConstraintClass ('string ' , MyStringConstraint::class);
101
+
102
+ $ constraint = $ factory ->createInstanceFor ('string ' );
103
+
104
+ $ this ->assertInstanceOf (MyStringConstraint::class, $ constraint );
105
+ $ this ->assertInstanceOf (ConstraintInterface::class, $ constraint );
119
106
}
120
107
121
- public function testCheckMode (): void
108
+ public function testCheckModeDefaultConfig (): void
122
109
{
123
110
$ f = new Factory ();
124
111
125
- // test default value
126
112
$ this ->assertEquals (Constraint::CHECK_MODE_NORMAL , $ f ->getConfig ());
113
+ }
114
+
115
+ public function testCheckModeWhenOverridingConfig (): void
116
+ {
117
+ $ f = new Factory ();
127
118
128
- // test overriding config
129
119
$ f ->setConfig (Constraint::CHECK_MODE_COERCE_TYPES );
120
+
130
121
$ this ->assertEquals (Constraint::CHECK_MODE_COERCE_TYPES , $ f ->getConfig ());
122
+ }
123
+
124
+ public function testCheckModeWhenAddingConfig (): void
125
+ {
126
+ $ f = new Factory ();
131
127
132
- // test adding config
128
+ $ f -> setConfig (Constraint:: CHECK_MODE_COERCE_TYPES );
133
129
$ f ->addConfig (Constraint::CHECK_MODE_NORMAL );
130
+
134
131
$ this ->assertEquals (Constraint::CHECK_MODE_NORMAL | Constraint::CHECK_MODE_COERCE_TYPES , $ f ->getConfig ());
132
+ }
133
+
134
+ public function testCheckModeWhenGettingFilteredConfig (): void
135
+ {
136
+ $ f = new Factory ();
135
137
136
- // test getting filtered config
137
138
$ this ->assertEquals (Constraint::CHECK_MODE_NORMAL , $ f ->getConfig (Constraint::CHECK_MODE_NORMAL ));
139
+ }
140
+
141
+ public function testCheckModeWhenRemovingConfig (): void
142
+ {
143
+ $ f = new Factory ();
138
144
139
- // test removing config
140
145
$ f ->removeConfig (Constraint::CHECK_MODE_COERCE_TYPES );
146
+
141
147
$ this ->assertEquals (Constraint::CHECK_MODE_NORMAL , $ f ->getConfig ());
148
+ }
149
+
150
+ public function testCheckModeWhenResettingToDefault (): void
151
+ {
152
+ $ f = new Factory ();
142
153
143
- // test resetting to defaults
144
154
$ f ->setConfig (Constraint::CHECK_MODE_COERCE_TYPES | Constraint::CHECK_MODE_TYPE_CAST );
145
155
$ f ->setConfig ();
146
156
$ this ->assertEquals (Constraint::CHECK_MODE_NORMAL , $ f ->getConfig ());
0 commit comments