forked from craftcms/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayValidatorTest.php
More file actions
140 lines (122 loc) · 4.42 KB
/
Copy pathArrayValidatorTest.php
File metadata and controls
140 lines (122 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace crafttests\unit\validators;
use craft\test\mockclasses\models\ExampleModel;
use craft\test\TestCase;
use craft\validators\ArrayValidator;
/**
* Class ArrayValidator.
*
* @todo Test the validateValue() function using $this->model->validate();
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @author Global Network Group | Giel Tettelaar <giel@yellowflash.net>
* @since 3.2
*/
class ArrayValidatorTest extends TestCase
{
/**
* @var ArrayValidator
*/
protected ArrayValidator $arrayValidator;
/**
* @var ExampleModel
*/
protected ExampleModel $model;
/**
* Test that if messages aren't provided when creating the array validator (i.e. not setting the $tooMany message),
* they will be provided automatically.
*
* @dataProvider paramsToTestOnEmptyDataProvider
* @param ArrayValidator $validator
* @param string $variableName
*/
public function testMessagingOnEmptyInputArray(ArrayValidator $validator, string $variableName): void
{
self::assertTrue(strlen($validator->$variableName) > 2);
self::assertIsString($validator->$variableName);
}
/**
*
*/
public function testCountArrayInputValue(): void
{
$newValidator = new ArrayValidator(['count' => [2, 5]]);
self::assertSame(2, $newValidator->min);
self::assertSame(5, $newValidator->max);
// Make sure if count is empty array. $count is a null variable.
$newValidator = new ArrayValidator(['count' => []]);
self::assertNull($newValidator->count);
}
/**
* @dataProvider arrayValidatorValuesDataProvider
* @param mixed $inputValue
* @param bool $mustValidate
*/
public function testValidation(mixed $inputValue, bool $mustValidate): void
{
$this->arrayValidator->count = null;
$this->model->exampleParam = $inputValue;
$this->arrayValidator->validateAttribute($this->model, 'exampleParam');
if ($mustValidate) {
self::assertArrayNotHasKey('exampleParam', $this->model->getErrors());
} else {
self::assertArrayHasKey('exampleParam', $this->model->getErrors());
}
}
/**
* Here we *specifically* test that if we pass in an array which as more than the minimum(4) and less than the maximum(10)
* BUT that is more than the count(5) an error will still be thrown.
*/
public function testCountValidation(): void
{
$this->model->exampleParam = [1, 2, 3, 4, 5, 6, 7];
$this->arrayValidator->validateAttribute($this->model, 'exampleParam');
self::assertArrayHasKey('exampleParam', $this->model->getErrors());
self::assertSame('aint right', $this->model->getErrors('exampleParam')[0]);
}
/**
* @return array
*/
public static function paramsToTestOnEmptyDataProvider(): array
{
$newValidator = new ArrayValidator(['min' => 1, 'max' => 10, 'count' => 4]);
return [
[$newValidator, 'message'], [$newValidator, 'tooFew'], [$newValidator, 'tooMany'], [$newValidator, 'notEqual'],
];
}
/**
* @return array
*/
public static function arrayValidatorValuesDataProvider(): array
{
return [
[[1, 2, 3, 4], true],
[[1, 2, 3, 4, 5], true],
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], true],
[[[1, 1], [2, 2], [3, 3], 4, 5, 6, 7, 8, 9, 10], true],
['hello', false],
[[1, 2], false],
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], false],
];
}
/**
* @inheritdoc
*/
protected function _before(): void
{
$this->model = new ExampleModel();
$this->arrayValidator = new ArrayValidator(['count' => 5, 'max' => 10, 'min' => 4, 'tooFew' => 'aint got nuff', 'tooMany' => 'staahhpp', 'notEqual' => 'aint right']);
// Test all variables are passed in and all good.
self::assertSame(5, $this->arrayValidator->count);
self::assertSame(10, $this->arrayValidator->max);
self::assertSame(4, $this->arrayValidator->min);
self::assertSame('aint got nuff', $this->arrayValidator->tooFew);
self::assertSame('staahhpp', $this->arrayValidator->tooMany);
self::assertSame('aint right', $this->arrayValidator->notEqual);
}
}