Skip to content

Commit 92820fa

Browse files
committed
test: add tests for current validation behaviors
1 parent c06012a commit 92820fa

2 files changed

Lines changed: 162 additions & 0 deletions

File tree

tests/system/Validation/FormatRulesTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,73 @@ public function numericProvider(): Generator
797797
];
798798
}
799799

800+
/**
801+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5374
802+
*
803+
* @dataProvider integerInvalidTypeDataProvider
804+
*
805+
* @param mixed $value
806+
*/
807+
public function testIntegerWithInvalidTypeData($value, bool $expected): void
808+
{
809+
$this->validation->setRules([
810+
'foo' => 'integer',
811+
]);
812+
813+
$data = [
814+
'foo' => $value,
815+
];
816+
$this->assertsame($expected, $this->validation->run($data));
817+
}
818+
819+
/**
820+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5374
821+
*
822+
* @dataProvider integerInvalidTypeDataProvider
823+
*
824+
* @param mixed $value
825+
*/
826+
public function testNumericWithInvalidTypeData($value, bool $expected): void
827+
{
828+
$this->validation->setRules([
829+
'foo' => 'numeric',
830+
]);
831+
832+
$data = [
833+
'foo' => $value,
834+
];
835+
$this->assertsame($expected, $this->validation->run($data));
836+
}
837+
838+
public function integerInvalidTypeDataProvider(): Generator
839+
{
840+
yield 'array with int' => [
841+
[555],
842+
true, // incorrect
843+
];
844+
845+
// TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given
846+
// yield 'empty array' => [
847+
// [],
848+
// false,
849+
// ];
850+
851+
yield 'bool true' => [
852+
true,
853+
true, // incorrect
854+
];
855+
856+
yield 'bool false' => [
857+
false,
858+
false,
859+
];
860+
861+
yield 'null' => [
862+
null,
863+
false,
864+
];
865+
}
866+
800867
/**
801868
* @dataProvider integerProvider
802869
*/

tests/system/Validation/ValidationTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,101 @@ public function testRunDoesTheBasics(): void
100100
$this->assertFalse($this->validation->run($data));
101101
}
102102

103+
/**
104+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5368
105+
*
106+
* @dataProvider arrayDataProvider
107+
*
108+
* @param mixed $value
109+
*/
110+
public function testCanValidatetArrayData($value, bool $expected): void
111+
{
112+
$this->validation->setRules(['arr' => 'is_array']);
113+
114+
$data['arr'] = $value;
115+
$this->assertSame($expected, $this->validation->run($data));
116+
}
117+
118+
public function arrayDataProvider(): Generator
119+
{
120+
yield 'list array' => [
121+
[1, 2, 3, 4, 5],
122+
false, // incorrect
123+
];
124+
125+
yield 'associative array' => [
126+
[
127+
'username' => 'admin001',
128+
'role' => 'administrator',
129+
'usepass' => 0,
130+
],
131+
false, // incorrect
132+
];
133+
134+
yield 'int' => [
135+
0,
136+
false,
137+
];
138+
139+
yield 'string int' => [
140+
'0',
141+
false,
142+
];
143+
144+
yield 'bool' => [
145+
false,
146+
false,
147+
];
148+
149+
yield 'null' => [
150+
null,
151+
false,
152+
];
153+
}
154+
155+
/**
156+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5374
157+
*
158+
* @dataProvider isIntInvalidTypeDataProvider
159+
*
160+
* @param mixed $value
161+
*/
162+
public function testIsIntWithInvalidTypeData($value, bool $expected): void
163+
{
164+
$this->validation->setRules(['foo' => 'is_int']);
165+
166+
$data = ['foo' => $value];
167+
$this->assertSame($expected, $this->validation->run($data));
168+
}
169+
170+
public function isIntInvalidTypeDataProvider(): Generator
171+
{
172+
yield 'array with int' => [
173+
[555],
174+
true, // incorrect
175+
];
176+
177+
yield 'empty array' => [
178+
[],
179+
false,
180+
];
181+
182+
yield 'bool true' => [
183+
true,
184+
false,
185+
];
186+
187+
yield 'bool false' => [
188+
false,
189+
false,
190+
];
191+
192+
yield 'null' => [
193+
null,
194+
false,
195+
];
196+
}
197+
103198
public function testRunReturnsLocalizedErrors(): void
104199
{
105200
$data = ['foo' => 'notanumber'];

0 commit comments

Comments
 (0)