1+ <?php
2+ /**
3+ * Symfony Validator
4+ *
5+ * Type sensitive validating Software for Symfony Applications.
6+ *
7+ * Copyright (c) 2020 Fabian Fröhlich <[email protected] > https://f-froehlich.de 8+ *
9+ *
10+ * This program is free software: you can redistribute it and/or modify
11+ * it under the terms of the GNU Affero General Public License as
12+ * published by the Free Software Foundation, either version 3 of the
13+ * License, or (at your option) any later version.
14+ *
15+ * This program is distributed in the hope that it will be useful,
16+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+ * GNU Affero General Public License for more details.
19+ *
20+ * You should have received a copy of the GNU Affero General Public License
21+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
22+ *
23+ * For all license terms see README.md and LICENSE Files in root directory of this Project.
24+ *
25+ */
26+
27+ namespace FabianFroehlich \Validator \Tests \Validator \Integration ;
28+
29+
30+ use FabianFroehlich \Validator \Constraints \AbstractConstraint ;
31+ use FabianFroehlich \Validator \Constraints \IsNotNull ;
32+ use FabianFroehlich \Validator \Constraints \IsNull ;
33+ use FabianFroehlich \Validator \Constraints \LogicalXOr ;
34+ use FabianFroehlich \Validator \Enums \ErrorCodes ;
35+ use FabianFroehlich \Validator \Tests \ValidatorTestCase ;
36+ use FabianFroehlich \Validator \Validator \AbstractConstraintValidator ;
37+
38+ /**
39+ * Class LogicalXOrValidatorTest
40+ *
41+ * @author Fabian Fröhlich <[email protected] > 42+ * @package FabianFroehlich\Validator\Tests\Validator\Integration
43+ */
44+ class LogicalXOrValidatorTest
45+ extends ValidatorTestCase {
46+
47+ /**
48+ * @test
49+ */
50+ public function checkBothValuesValid (): void {
51+ $ constraint = new LogicalXOr (['left ' => new IsNull (), 'right ' => new IsNull ()]);
52+
53+ $ this ->assertFalse ($ this ->validator ->validate (null , $ constraint ));
54+
55+ $ this ->assertCount (1 , $ this ->validatorBuilder ->getViolations ());
56+ $ violation = $ this ->validatorBuilder ->getViolations ()[0 ];
57+ $ this ->assertEquals (ErrorCodes::LOGICAL_XOR_INVALID ()->value (), $ violation ->getCode ());
58+
59+ }
60+
61+ /**
62+ * @test
63+ */
64+ public function passIfLeftValueIsValid (): void {
65+ $ constraint = new LogicalXOr (['left ' => new IsNull (), 'right ' => new IsNotNull ()]);
66+
67+ $ this ->assertTrue ($ this ->validator ->validate (null , $ constraint ));
68+ }
69+
70+ /**
71+ * @test
72+ */
73+ public function passIfRightValueIsPassed (): void {
74+ $ constraint = new LogicalXOr (['left ' => new IsNotNull (), 'right ' => new IsNull ()]);
75+
76+ $ this ->assertTrue ($ this ->validator ->validate (null , $ constraint ));
77+ }
78+
79+ /**
80+ * @test
81+ */
82+ public function failIfBothValueIsInvalid (): void {
83+ $ constraint = new LogicalXOr (['left ' => new IsNotNull (), 'right ' => new IsNotNull ()]);
84+
85+ $ this ->assertFalse ($ this ->validator ->validate (null , $ constraint ));
86+
87+ $ this ->assertCount (3 , $ this ->validatorBuilder ->getViolations ());
88+ $ violation = $ this ->validatorBuilder ->getViolations ()[2 ];
89+ $ this ->assertEquals (ErrorCodes::LOGICAL_XOR_INVALID ()->value (), $ violation ->getCode ());
90+ }
91+
92+ protected function getRequiredType (): string {
93+ return AbstractConstraintValidator::ALL ;
94+ }
95+
96+ /**
97+ * Get the types that are valid for the Validator
98+ *
99+ * @return array
100+ */
101+ protected function getValidTypes (): array {
102+
103+ return $ this ->getAllTypes ();
104+ }
105+
106+ /**
107+ * {@inheritDoc}
108+ */
109+ protected function getConstraint (): AbstractConstraint {
110+
111+ $ class = $ this ->getConstraintClass ();
112+
113+ return new $ class (['left ' => new IsNotNull (), 'right ' => new IsNotNull ()]);
114+ }
115+
116+ /**
117+ * {@inheritDoc}
118+ */
119+ protected function getConstraintClass (): string {
120+
121+ return LogicalXOr::class;
122+ }
123+
124+ }
0 commit comments