77 * file that was distributed with this source code.
88 */
99
10+ declare (strict_types=1 );
11+
1012namespace JsonSchema \Tests \Constraints ;
1113
14+ use JsonSchema \Constraints ;
1215use JsonSchema \Constraints \Constraint ;
16+ use JsonSchema \Constraints \ConstraintInterface ;
1317use JsonSchema \Constraints \Factory ;
1418use JsonSchema \Entity \JsonPointer ;
19+ use JsonSchema \Exception \InvalidArgumentException ;
1520use PHPUnit \Framework \TestCase ;
1621
17- /**
18- * Class MyBadConstraint
19- *
20- * @package JsonSchema\Tests\Constraints
21- */
2222class MyBadConstraint
2323{
2424}
2525
26- /**
27- * Class MyStringConstraint
28- *
29- * @package JsonSchema\Tests\Constraints
30- */
3126class MyStringConstraint extends Constraint
3227{
3328 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
3732
3833class FactoryTest extends TestCase
3934{
40- /**
41- * @var Factory
42- */
43- protected $ factory ;
44-
45- protected function setUp (): void
46- {
47- $ this ->factory = new Factory ();
48- }
49-
5035 /**
5136 * @dataProvider constraintNameProvider
52- *
53- * @param string $constraintName
54- * @param string $expectedClass
5537 */
56- public function testCreateInstanceForConstraintName ($ constraintName , $ expectedClass ): void
38+ public function testCreateInstanceForConstraintName (string $ constraintName , string $ expectedClass ): void
5739 {
58- $ constraint = $ this ->factory ->createInstanceFor ($ constraintName );
40+ $ factory = new Factory ();
41+ $ constraint = $ factory ->createInstanceFor ($ constraintName );
5942
6043 $ this ->assertInstanceOf ($ expectedClass , $ constraint );
61- $ this ->assertInstanceOf (' JsonSchema\Constraints\ ConstraintInterface' , $ constraint );
44+ $ this ->assertInstanceOf (ConstraintInterface::class , $ constraint );
6245 }
6346
64- public function constraintNameProvider (): array
47+ public static function constraintNameProvider (): \ Generator
6548 {
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];
7960 }
8061
8162 /**
8263 * @dataProvider invalidConstraintNameProvider
83- *
84- * @param string $constraintName
8564 */
86- public function testExceptionWhenCreateInstanceForInvalidConstraintName ($ constraintName ): void
65+ public function testExceptionWhenCreateInstanceForInvalidConstraintName (string $ constraintName ): void
8766 {
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 );
9072 }
9173
92- public function invalidConstraintNameProvider (): array
74+ public static function invalidConstraintNameProvider (): \ Generator
9375 {
94- return [
95- ['invalidConstraintName ' ],
96- ];
76+ yield 'InvalidConstraint ' => ['invalidConstraintName ' ];
9777 }
9878
9979 public function testSetConstraintClassExistsCondition (): void
10080 {
81+ $ factory = new Factory ();
82+
10183 $ this ->expectException (\JsonSchema \Exception \InvalidArgumentException::class);
10284
103- $ this -> factory ->setConstraintClass ('string ' , 'SomeConstraint ' );
85+ $ factory ->setConstraintClass ('string ' , 'SomeConstraint ' );
10486 }
10587
10688 public function testSetConstraintClassImplementsCondition (): void
10789 {
90+ $ factory = new Factory ();
91+
10892 $ this ->expectException (\JsonSchema \Exception \InvalidArgumentException::class);
10993
110- $ this -> factory ->setConstraintClass ('string ' , ' JsonSchema\Tests\Constraints\ MyBadConstraint' );
94+ $ factory ->setConstraintClass ('string ' , MyBadConstraint::class );
11195 }
11296
11397 public function testSetConstraintClassInstance (): void
11498 {
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 );
119106 }
120107
121- public function testCheckMode (): void
108+ public function testCheckModeDefaultConfig (): void
122109 {
123110 $ f = new Factory ();
124111
125- // test default value
126112 $ this ->assertEquals (Constraint::CHECK_MODE_NORMAL , $ f ->getConfig ());
113+ }
114+
115+ public function testCheckModeWhenOverridingConfig (): void
116+ {
117+ $ f = new Factory ();
127118
128- // test overriding config
129119 $ f ->setConfig (Constraint::CHECK_MODE_COERCE_TYPES );
120+
130121 $ this ->assertEquals (Constraint::CHECK_MODE_COERCE_TYPES , $ f ->getConfig ());
122+ }
123+
124+ public function testCheckModeWhenAddingConfig (): void
125+ {
126+ $ f = new Factory ();
131127
132- // test adding config
128+ $ f -> setConfig (Constraint:: CHECK_MODE_COERCE_TYPES );
133129 $ f ->addConfig (Constraint::CHECK_MODE_NORMAL );
130+
134131 $ 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 ();
135137
136- // test getting filtered config
137138 $ this ->assertEquals (Constraint::CHECK_MODE_NORMAL , $ f ->getConfig (Constraint::CHECK_MODE_NORMAL ));
139+ }
140+
141+ public function testCheckModeWhenRemovingConfig (): void
142+ {
143+ $ f = new Factory ();
138144
139- // test removing config
140145 $ f ->removeConfig (Constraint::CHECK_MODE_COERCE_TYPES );
146+
141147 $ this ->assertEquals (Constraint::CHECK_MODE_NORMAL , $ f ->getConfig ());
148+ }
149+
150+ public function testCheckModeWhenResettingToDefault (): void
151+ {
152+ $ f = new Factory ();
142153
143- // test resetting to defaults
144154 $ f ->setConfig (Constraint::CHECK_MODE_COERCE_TYPES | Constraint::CHECK_MODE_TYPE_CAST );
145155 $ f ->setConfig ();
146156 $ this ->assertEquals (Constraint::CHECK_MODE_NORMAL , $ f ->getConfig ());
0 commit comments