| 1 |
<?php |
| 2 |
|
| 3 |
namespace unit\Rules; |
| 4 |
|
| 5 |
use Give\Vendors\StellarWP\Validation\Rules\Boolean; |
| 6 |
use Give\Vendors\StellarWP\Validation\Tests\TestCase; |
| 7 |
|
| 8 |
class BooleanTest extends TestCase |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @since 1.4.0 |
| 12 |
* |
| 13 |
* @dataProvider booleansProvider |
| 14 |
*/ |
| 15 |
public function testRuleValidatesBooleans($value, $pass) |
| 16 |
{ |
| 17 |
$rule = new Boolean(); |
| 18 |
|
| 19 |
if ($pass) { |
| 20 |
self::assertValidationRulePassed($rule, $value); |
| 21 |
} else { |
| 22 |
self::assertValidationRuleFailed($rule, $value); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @since 1.4.1 updates tests to pass false-y values |
| 28 |
* @since 1.4.0 |
| 29 |
*/ |
| 30 |
public function booleansProvider(): array |
| 31 |
{ |
| 32 |
return [ |
| 33 |
// values that pass |
| 34 |
[true, true], |
| 35 |
[1, true], |
| 36 |
['1', true], |
| 37 |
['true', true], |
| 38 |
['yes', true], |
| 39 |
['on', true], |
| 40 |
[false, true], |
| 41 |
[0, true], |
| 42 |
['0', true], |
| 43 |
['false', true], |
| 44 |
['no', true], |
| 45 |
['off', true], |
| 46 |
|
| 47 |
// values that fail |
| 48 |
['abc', false], |
| 49 |
['123', false], |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @since 1.4.0 |
| 55 |
*/ |
| 56 |
public function testCastsToBoolean() |
| 57 |
{ |
| 58 |
$rule = new Boolean(); |
| 59 |
self::assertSame(true, $rule->sanitize('1')); |
| 60 |
self::assertSame(false, $rule->sanitize('0')); |
| 61 |
} |
| 62 |
} |
| 63 |
|