Skip to content

Commit

Permalink
truthy, falsey tests, docblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed Oct 12, 2016
1 parent adf76ee commit ea96290
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Validation/Validation.php
Expand Up @@ -521,13 +521,13 @@ public static function localizedTime($check, $type = 'datetime', $format = null)
} }


/** /**
* Validates if passed value is boolean. * Validates if passed value is boolean-like.
* *
* The list of what is considered to be boolean values, may be set via $booleanValues. * The list of what is considered to be boolean values, may be set via $booleanValues.
* *
* @param bool|int|string $check Value to check. * @param bool|int|string $check Value to check.
* @param string $booleanValues List of valid boolean values, defaults to `[true, false, 0, 1, '0', '1']`. * @param string $booleanValues List of valid boolean values, defaults to `[true, false, 0, 1, '0', '1']`.
* @return bool Success * @return bool Success.
*/ */
public static function boolean($check, array $booleanValues = []) public static function boolean($check, array $booleanValues = [])
{ {
Expand All @@ -545,7 +545,7 @@ public static function boolean($check, array $booleanValues = [])
* *
* @param bool|int|string $check Value to check. * @param bool|int|string $check Value to check.
* @param array $truthyValues List of valid truthy values, defaults to `[true, 1, '1']`. * @param array $truthyValues List of valid truthy values, defaults to `[true, 1, '1']`.
* @return bool Success * @return bool Success.
*/ */
public static function truthy($check, array $truthyValues = []) public static function truthy($check, array $truthyValues = [])
{ {
Expand All @@ -563,7 +563,7 @@ public static function truthy($check, array $truthyValues = [])
* *
* @param bool|int|string $check Value to check. * @param bool|int|string $check Value to check.
* @param array $falseyValues List of valid falsey values, defaults to `[false, 0, '0']`. * @param array $falseyValues List of valid falsey values, defaults to `[false, 0, '0']`.
* @return bool Success * @return bool Success.
*/ */
public static function falsey($check, array $falseyValues = []) public static function falsey($check, array $falseyValues = [])
{ {
Expand Down
113 changes: 113 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -1633,6 +1633,119 @@ public function testBoolean()
$this->assertFalse(Validation::boolean('Boo!')); $this->assertFalse(Validation::boolean('Boo!'));
} }


/**
* testBooleanWithOptions method
*
* @return void
*/
public function testBooleanWithOptions()
{
$this->assertTrue(Validation::boolean('0', ['0', '1']));
$this->assertTrue(Validation::boolean('1', ['0', '1']));
$this->assertFalse(Validation::boolean(0, ['0', '1']));
$this->assertFalse(Validation::boolean(1, ['0', '1']));
$this->assertFalse(Validation::boolean(false, ['0', '1']));
$this->assertFalse(Validation::boolean(true, ['0', '1']));
$this->assertFalse(Validation::boolean('false', ['0', '1']));
$this->assertFalse(Validation::boolean('true', ['0', '1']));
$this->assertTrue(Validation::boolean(0, [0, 1]));
$this->assertTrue(Validation::boolean(1, [0, 1]));
}

/**
* testTruthy method
*
* @return void
*/
public function testTruthy()
{
$this->assertTrue(Validation::truthy(1));
$this->assertTrue(Validation::truthy(true));
$this->assertTrue(Validation::truthy('1'));

$this->assertFalse(Validation::truthy('true'));
$this->assertFalse(Validation::truthy('on'));
$this->assertFalse(Validation::truthy('yes'));

$this->assertFalse(Validation::truthy(0));
$this->assertFalse(Validation::truthy(false));
$this->assertFalse(Validation::truthy('0'));
$this->assertFalse(Validation::truthy('false'));

$this->assertTrue(Validation::truthy(1));
$this->assertTrue(Validation::truthy(true));
$this->assertTrue(Validation::truthy('1'));

$this->assertFalse(Validation::truthy('true'));
$this->assertFalse(Validation::truthy('on'));
$this->assertFalse(Validation::truthy('yes'));

$this->assertFalse(Validation::truthy(0));
$this->assertFalse(Validation::truthy(false));
$this->assertFalse(Validation::truthy('0'));
$this->assertFalse(Validation::truthy('false'));

$this->assertFalse(Validation::truthy('on'));
$this->assertFalse(Validation::truthy('yes'));

$this->assertTrue(Validation::truthy('on', ['on', 'yes', 'true']));
$this->assertTrue(Validation::truthy('yes', ['on', 'yes', 'true']));
$this->assertTrue(Validation::truthy('true', ['on', 'yes', 'true']));

$this->assertFalse(Validation::truthy(1, ['on', 'yes', 'true']));
$this->assertFalse(Validation::truthy(true, ['on', 'yes', 'true']));
$this->assertFalse(Validation::truthy('1', ['on', 'yes', 'true']));

$this->assertTrue(Validation::truthy('true', ['on', 'yes', 'true']));
}

/**
* testTruthy method
*
* @return void
*/
public function testFalsey()
{
$this->assertTrue(Validation::falsey(0));
$this->assertTrue(Validation::falsey(false));
$this->assertTrue(Validation::falsey('0'));

$this->assertFalse(Validation::falsey('false'));
$this->assertFalse(Validation::falsey('off'));
$this->assertFalse(Validation::falsey('no'));

$this->assertFalse(Validation::falsey(1));
$this->assertFalse(Validation::falsey(true));
$this->assertFalse(Validation::falsey('1'));
$this->assertFalse(Validation::falsey('true'));

$this->assertTrue(Validation::falsey(0));
$this->assertTrue(Validation::falsey(false));
$this->assertTrue(Validation::falsey('0'));

$this->assertFalse(Validation::falsey('false'));
$this->assertFalse(Validation::falsey('off'));
$this->assertFalse(Validation::falsey('no'));

$this->assertFalse(Validation::falsey(1));
$this->assertFalse(Validation::falsey(true));
$this->assertFalse(Validation::falsey('1'));
$this->assertFalse(Validation::falsey('true'));

$this->assertFalse(Validation::falsey('off'));
$this->assertFalse(Validation::falsey('no'));

$this->assertTrue(Validation::falsey('off', ['off', 'no', 'false']));
$this->assertTrue(Validation::falsey('no', ['off', 'no', 'false']));
$this->assertTrue(Validation::falsey('false', ['off', 'no', 'false']));

$this->assertFalse(Validation::falsey(0, ['off', 'no', 'false']));
$this->assertFalse(Validation::falsey(false, ['off', 'no', 'false']));
$this->assertFalse(Validation::falsey('0', ['off', 'yes', 'false']));

$this->assertTrue(Validation::falsey('false', ['off', 'no', 'false']));
}

/** /**
* testDateCustomRegx method * testDateCustomRegx method
* *
Expand Down

0 comments on commit ea96290

Please sign in to comment.