Skip to content

Commit ed178a8

Browse files
committed
test: incorrect calls to validators
1 parent 2d4e684 commit ed178a8

File tree

8 files changed

+178
-20
lines changed

8 files changed

+178
-20
lines changed

src/AssociativeArrayValidator.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Escuelait\MagnificValidator;
6+
7+
class AssociativeArrayValidator implements Validator
8+
{
9+
public static function isRuleMatched($rule): bool
10+
{
11+
return $rule === 'associative_array';
12+
}
13+
14+
public function validate(mixed $value): array
15+
{
16+
if(!is_array($value)) {
17+
return ['The input is not an array'];
18+
}
19+
20+
if ([] === $value) {
21+
return ['The input has not keys'];
22+
}
23+
24+
return array_keys($value) !== range(0, count($value) - 1)
25+
? []
26+
: ['The input is not an associative array'];
27+
}
28+
}

src/FieldValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class FieldValidator implements Validator {
1010

1111
public function __construct($rules)
1212
{
13+
assert(count((new StringsArrayValidator())->validate($rules)) === 0, 'Not valid rules for FieldValidator');
14+
1315
$this->rules = $rules;
1416
}
1517

src/FormValidator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Escuelait\MagnificValidator;
66

7+
use InvalidArgumentException;
8+
79
class FormValidator implements Validator
810
{
911
private $rules;
@@ -15,6 +17,10 @@ public function __construct(array $rules)
1517

1618
public function validate($values): array
1719
{
20+
if(count((new AssociativeArrayValidator())->validate($values)) > 0) {
21+
throw(new InvalidArgumentException('Not valid form values'));
22+
}
23+
1824
$formErrors = [];
1925
foreach ($this->rules as $fieldName => $ruleNames) {
2026
$fieldErrors = $this->validateValue($values[$fieldName], $ruleNames);

src/StringsArrayValidator.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Escuelait\MagnificValidator;
6+
7+
class StringsArrayValidator implements Validator
8+
{
9+
public static function isRuleMatched($rule): bool
10+
{
11+
return $rule === 'strings_array';
12+
}
13+
14+
public function validate(mixed $array): array
15+
{
16+
if(!is_array($array)) {
17+
return ['Input is not an array'];
18+
}
19+
20+
// Verifica que las claves sean numéricas consecutivas
21+
if (array_keys($array) !== range(0, count($array) - 1)) {
22+
return ['Input is not a numeric array'];
23+
}
24+
25+
// Verifica que todos los valores sean strings
26+
foreach ($array as $string) {
27+
if (!is_string($string)) {
28+
return ['Not array values are strings'];
29+
}
30+
}
31+
32+
return [];
33+
}
34+
}

src/ValidatorsFactory.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
namespace Escuelait\MagnificValidator;
66

7+
use InvalidArgumentException;
8+
79
class ValidatorsFactory
810
{
911

1012
public function create(mixed $rules) {
13+
if(!is_string($rules) && !$this->isAssociativeArray($rules) && !$this->isNumericArrayOfStrings($rules)) {
14+
throw(new InvalidArgumentException("Not valid input to create a validator"));
15+
}
1116
if(is_string($rules)) {
1217
$validatorStrategyFactory = new ValidatorsStrategyFactory();
1318
return new($validatorStrategyFactory->matchedValidator($rules))($rules);
@@ -20,29 +25,15 @@ public function create(mixed $rules) {
2025
}
2126
}
2227

23-
private function isAssociativeArray(array $array): bool
28+
private function isAssociativeArray($rules): bool
2429
{
25-
if ([] === $array) {
26-
return false;
27-
}
28-
29-
return array_keys($array) !== range(0, count($array) - 1);
30+
$associativeArrayValidator = new AssociativeArrayValidator();
31+
return count($associativeArrayValidator->validate($rules)) === 0;
3032
}
3133

32-
private function isNumericArrayOfStrings(array $array): bool
34+
private function isNumericArrayOfStrings($rules): bool
3335
{
34-
// Verifica que las claves sean numéricas consecutivas
35-
if (array_keys($array) !== range(0, count($array) - 1)) {
36-
return false;
37-
}
38-
39-
// Verifica que todos los valores sean strings
40-
foreach ($array as $value) {
41-
if (!is_string($value)) {
42-
return false;
43-
}
44-
}
45-
46-
return true;
36+
$stringsArrayValidator = new StringsArrayValidator();
37+
return count($stringsArrayValidator->validate($rules)) === 0;
4738
}
4839
}

tests/FieldValidatorTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Escuelait\Tests\MagnificValidator;
6+
7+
use AssertionError;
8+
use PHPUnit\Framework\TestCase;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use Escuelait\MagnificValidator\FieldValidator;
12+
13+
class FieldValidatorTest extends TestCase
14+
{
15+
16+
public static function validValuesDataProvider() {
17+
return [
18+
[
19+
['required', 'email'],
20+
'mik@escuela.it',
21+
],
22+
[
23+
['required'],
24+
'escuela.it',
25+
],
26+
];
27+
}
28+
29+
#[Test]
30+
#[DataProvider('validValuesDataProvider')]
31+
public function itRecivesAnExceptionWhenValidatingWithIncorrectInput($rules, $values)
32+
{
33+
$validator = new FieldValidator($rules);
34+
$errors = $validator->validate($values);
35+
$this->assertEmpty($errors);
36+
}
37+
38+
public static function invalidValuesDataProvider() {
39+
return [
40+
[['something', 'foo']],
41+
[null],
42+
];
43+
}
44+
45+
#[Test]
46+
#[DataProvider('invalidValuesDataProvider')]
47+
public function itDontValidates($values)
48+
{
49+
$validator = new FieldValidator(['required', 'email']);
50+
$errors = $validator->validate($values);
51+
$this->assertNotEmpty($errors);
52+
}
53+
54+
// #[Test]
55+
// public function itCantCreateFieldValidatorWithInvalidRules() {
56+
// $this->expectException(AssertionError::class);
57+
// new FieldValidator(344);
58+
// }
59+
60+
}

tests/FormValidatorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ public function itRecivesAnExceptionWhenRuleIsNotReal()
219219
$validator->validate(['comment' => 'something']);
220220
}
221221

222+
public static function invalidFormValuesDataProvider() {
223+
return [
224+
[2],
225+
['something'],
226+
[['something', 'foo']],
227+
[null],
228+
];
229+
}
230+
231+
#[Test]
232+
#[DataProvider('invalidFormValuesDataProvider')]
233+
public function itRecivesAnExceptionWhenValidatingWithIncorrectInput($values)
234+
{
235+
$validator = new FormValidator(['comment' => ['required', 'not_a_real_rule']]);
236+
$this->expectException(\InvalidArgumentException::class);
237+
$this->expectExceptionMessage("Not valid form values");
238+
$validator->validate($values);
239+
}
240+
222241
#[Test]
223242
public function itInformsValidationErrors()
224243
{

tests/ValidatorsFactoryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111

1212
class ValidatorsFactoryTest extends TestCase {
1313

14+
public static function notValidInputProvider() {
15+
return [
16+
[5],
17+
[true],
18+
[5.09],
19+
[null],
20+
];
21+
}
22+
23+
#[Test]
24+
#[DataProvider('notValidInputProvider')]
25+
public function itThrowsAnErrorWhenTryToCreateValidatorWithInvalidData($input) {
26+
$validatorFactory = new ValidatorsFactory();
27+
$this->expectException(\InvalidArgumentException::class);
28+
$this->expectExceptionMessage("Not valid input to create a validator");
29+
$validatorFactory->create($input);
30+
}
31+
1432
#[Test]
1533
public function itValidatesAForm() {
1634
$validatorFactory = new ValidatorsFactory();

0 commit comments

Comments
 (0)