@@ -67,18 +67,58 @@ public function testValidateWithoutConstraints(): void
6767 $ this ->assertCount (0 , $ violations );
6868 }
6969
70+ public function testValidateWithGroups (): void
71+ {
72+ $ validator = Validator::notBlank (groups: ['Default ' ])
73+ ->email (groups: ['registration ' ]);
74+
75+ $ violations = $ validator ->validate ('invalid-email ' , groups: ['Default ' ]);
76+ $ this ->assertCount (0 , $ violations );
77+
78+ $ violations = $ validator ->validate ('invalid-email ' , groups: ['registration ' ]);
79+ $ this ->assertCount (1 , $ violations );
80+ }
81+
7082 public function testAssertFail (): void
7183 {
7284 $ this ->expectException (ValidationFailedException::class);
7385 $ this ->validator ->assert (16 );
7486 }
7587
88+ public function testAssertFailWithName (): void
89+ {
90+ $ this ->expectException (ValidationFailedException::class);
91+ $ this ->expectExceptionMessage ('age: This value should be greater than or equal to 18. ' );
92+
93+ $ this ->validator ->assert (16 , 'age ' );
94+ }
95+
7696 public function testAssertSuccess (): void
7797 {
7898 $ this ->validator ->assert (18 );
7999 $ this ->assertTrue (true );
80100 }
81101
102+ public function testAssertWithGroups (): void
103+ {
104+ $ validator = Validator::notBlank (groups: ['Default ' ])
105+ ->email (groups: ['registration ' ]);
106+
107+ $ validator ->assert ('invalid-email ' , groups: ['Default ' ]);
108+ $ this ->assertTrue (true );
109+ }
110+
111+ public function testAssertWithGroupsFail (): void
112+ {
113+ $ validator = Validator::notBlank (groups: ['Default ' ])
114+ ->email (groups: ['registration ' ]);
115+
116+ $ this ->expectException (ValidationFailedException::class);
117+ $ this ->expectExceptionMessage ('This value is not a valid email address. ' );
118+
119+ $ validator ->assert ('invalid-email ' , groups: ['registration ' ]);
120+ }
121+
82122 public function testIsValid (): void
83123 {
84124 $ this ->assertFalse ($ this ->validator ->isValid (16 ));
@@ -90,6 +130,15 @@ public function testIsValidWithoutConstraints(): void
90130 $ this ->assertTrue ((new Validator ())->isValid ('anything ' ));
91131 }
92132
133+ public function testIsValidWithGroups (): void
134+ {
135+ $ validator = Validator::notBlank (groups: ['Default ' ])
136+ ->email (groups: ['registration ' ]);
137+
138+ $ this ->assertTrue ($ validator ->isValid ('invalid-email ' , groups: ['Default ' ]));
139+ $ this ->assertFalse ($ validator ->isValid ('invalid-email ' , groups: ['registration ' ]));
140+ }
141+
93142 public function testToArray (): void
94143 {
95144 $ constraints = $ this ->validator ->toArray ();
0 commit comments