Skip to content

Commit e599cfd

Browse files
committed
feat(validator): add global state reset
1 parent 422545a commit e599cfd

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ offering an easy-to-use and intuitive API to validate user input or other data i
3030
- [toArray](#toarray)
3131
- [addNamespace](#addnamespace)
3232
- [setTranslator](#settranslator)
33+
- [reset](#reset)
3334
- [Custom Constraints](#custom-constraints)
3435
- [Translations](#translations)
3536

@@ -209,6 +210,14 @@ Used to add a translator for validation error message translations.
209210

210211
Check the [Translations](#translations) section.
211212

213+
### `reset`
214+
215+
```php
216+
reset(): void
217+
```
218+
219+
Clears globally registered custom constraint namespaces and translator configuration.
220+
212221
## Custom Constraints
213222

214223
If you need a custom constraint, follow the Symfony Validator documentation: [Creating Custom Constraints](https://symfony.com/doc/current/validation/custom_constraint.html).
@@ -291,4 +300,4 @@ Make sure to open a pull request or issue.
291300
## License
292301

293302
This project is licensed under the MIT license.
294-
Please see the [LICENSE](LICENSE) file distributed with this source code for further information regarding copyright and licensing.
303+
Please see the [LICENSE](LICENSE) file distributed with this source code for further information regarding copyright and licensing.

src/Validator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,21 @@ private function addConstraint(Constraint $constraint): void
9999

100100
public static function addNamespace(string $namespace): void
101101
{
102+
if (in_array($namespace, self::$namespaces, true)) {
103+
return;
104+
}
105+
102106
self::$namespaces[] = $namespace;
103107
}
104108

105109
public static function setTranslator(?TranslatorInterface $translator): void
106110
{
107111
self::$translator = $translator;
108112
}
113+
114+
public static function reset(): void
115+
{
116+
self::$namespaces = [];
117+
self::$translator = null;
118+
}
109119
}

tests/ValidatorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ protected function setUp(): void
1919
{
2020
parent::setUp();
2121

22+
Validator::reset();
23+
2224
$this->validator = Validator::notBlank()
2325
->greaterThanOrEqual(18)
2426
->lessThan(25);
2527
}
2628

29+
protected function tearDown(): void
30+
{
31+
Validator::reset();
32+
33+
parent::tearDown();
34+
}
35+
2736
public function testConstraintThatIsInvalid(): void
2837
{
2938
// NotBlankValidator class exists in "Symfony\Component\Validator\Constraints" namespace
@@ -102,6 +111,17 @@ public function testCustomConstraint(): void
102111
$this->assertTrue(Validator::containsAlphanumeric()->isValid('v4l1d'));
103112
}
104113

114+
public function testResetClearsCustomConstraintNamespaces(): void
115+
{
116+
Validator::addNamespace('ProgrammatorDev\FluentValidator\Test\Fixtures\Constraint');
117+
$this->assertTrue(Validator::containsAlphanumeric()->isValid('v4l1d'));
118+
119+
Validator::reset();
120+
121+
$this->expectException(NoSuchConstraintException::class);
122+
Validator::containsAlphanumeric();
123+
}
124+
105125
public function testSetTranslator(): void
106126
{
107127
// by default, the error is in English
@@ -114,4 +134,16 @@ public function testSetTranslator(): void
114134
$violations = $this->validator->validate('');
115135
$this->assertEquals('Este valor não deveria ser vazio.', $violations->get(0)->getMessage());
116136
}
137+
138+
public function testResetClearsTranslator(): void
139+
{
140+
Validator::setTranslator(new Translator('pt'));
141+
$violations = $this->validator->validate('');
142+
$this->assertEquals('Este valor não deveria ser vazio.', $violations->get(0)->getMessage());
143+
144+
Validator::reset();
145+
146+
$violations = $this->validator->validate('');
147+
$this->assertEquals('This value should not be blank.', $violations->get(0)->getMessage());
148+
}
117149
}

0 commit comments

Comments
 (0)