Skip to content

Commit 82382da

Browse files
committed
test: add tests for current behavior to matches and differs
1 parent b0d8fd8 commit 82382da

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

tests/system/Validation/RulesTest.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,26 @@ public static function providePermitEmpty(): iterable
290290
}
291291

292292
/**
293-
* @dataProvider provideMatchesCases
293+
* @dataProvider provideMatches
294294
*/
295295
public function testMatches(array $data, bool $expected): void
296296
{
297297
$this->validation->setRules(['foo' => 'matches[bar]']);
298298
$this->assertSame($expected, $this->validation->run($data));
299299
}
300300

301-
public static function provideMatchesCases(): iterable
301+
public static function provideMatches(): iterable
302302
{
303303
yield from [
304-
[['foo' => null, 'bar' => null], true],
305-
[['foo' => 'match', 'bar' => 'match'], true],
306-
[['foo' => 'match', 'bar' => 'nope'], false],
304+
'foo bar not exist' => [[], false],
305+
'bar not exist' => [['foo' => null], false],
306+
'foo not exist' => [['bar' => null], true], // should be false?
307+
'foo bar null' => [['foo' => null, 'bar' => null], true],
308+
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], true],
309+
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], false],
310+
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false], // should be true
311+
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
312+
'foo bar bool match' => [['foo' => true, 'bar' => true], false], // should be true
307313
];
308314
}
309315

@@ -325,12 +331,27 @@ public static function provideMatchesNestedCases(): iterable
325331
}
326332

327333
/**
328-
* @dataProvider provideMatchesCases
334+
* @dataProvider provideDiffers
329335
*/
330336
public function testDiffers(array $data, bool $expected): void
331337
{
332338
$this->validation->setRules(['foo' => 'differs[bar]']);
333-
$this->assertSame(! $expected, $this->validation->run($data));
339+
$this->assertSame($expected, $this->validation->run($data));
340+
}
341+
342+
public static function provideDiffers(): iterable
343+
{
344+
yield from [
345+
'foo bar not exist' => [[], false],
346+
'bar not exist' => [['foo' => null], false],
347+
'foo not exist' => [['bar' => null], false],
348+
'foo bar null' => [['foo' => null, 'bar' => null], false],
349+
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], false],
350+
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], true],
351+
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true], // should be false
352+
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], true],
353+
'foo bar bool match' => [['foo' => true, 'bar' => true], true], // should be false
354+
];
334355
}
335356

336357
/**

tests/system/Validation/StrictRules/RulesTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,55 @@ public static function provideLessEqualThanStrict(): iterable
198198
[true, '0', false],
199199
];
200200
}
201+
202+
/**
203+
* @dataProvider provideMatches
204+
*/
205+
public function testMatches(array $data, bool $expected): void
206+
{
207+
$this->validation->setRules(['foo' => 'matches[bar]']);
208+
$this->assertSame($expected, $this->validation->run($data));
209+
}
210+
211+
public static function provideMatches(): iterable
212+
{
213+
yield from [
214+
'foo bar not exist' => [[], false],
215+
'bar not exist' => [['foo' => null], false],
216+
'foo not exist' => [['bar' => null], true],
217+
'foo bar null' => [['foo' => null, 'bar' => null], true],
218+
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], true],
219+
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], false],
220+
// TypeError: CodeIgniter\Validation\Rules::matches(): Argument #1 ($str) must be of type ?string, float given
221+
// 'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true],
222+
// TypeError: CodeIgniter\Validation\Rules::matches(): Argument #1 ($str) must be of type ?string, float given
223+
// 'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
224+
// TypeError: CodeIgniter\Validation\Rules::matches(): Argument #1 ($str) must be of type ?string, float given
225+
// 'foo bar bool match' => [['foo' => true, 'bar' => true], true],
226+
];
227+
}
228+
229+
/**
230+
* @dataProvider provideDiffers
231+
*/
232+
public function testDiffers(array $data, bool $expected): void
233+
{
234+
$this->validation->setRules(['foo' => 'differs[bar]']);
235+
$this->assertSame($expected, $this->validation->run($data));
236+
}
237+
238+
public static function provideDiffers(): iterable
239+
{
240+
yield from [
241+
'foo bar not exist' => [[], false],
242+
'bar not exist' => [['foo' => null], false],
243+
'foo not exist' => [['bar' => null], false],
244+
'foo bar null' => [['foo' => null, 'bar' => null], false],
245+
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], false],
246+
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], true],
247+
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false],
248+
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
249+
'foo bar bool match' => [['foo' => true, 'bar' => true], false],
250+
];
251+
}
201252
}

0 commit comments

Comments
 (0)