Skip to content

Commit

Permalink
Fix Bool value parsing and Int value parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jun 14, 2017
1 parent 1b3fcf8 commit b8ad4dd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
13 changes: 13 additions & 0 deletions src/ValueType/BoolType.php
Expand Up @@ -2,10 +2,20 @@

namespace ActionKit\ValueType;

/*
* Returns TRUE for "1", "true", "on" and "yes". Returns FALSE otherwise.
*
* If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for "0", "false",
* "off", "no", and "", and NULL is returned for all non-boolean values.
*/
class BoolType extends BaseType
{
public function test($value)
{
if ($value === "") {
return true;
}

$var = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if (is_bool($var)) {
return true;
Expand All @@ -16,6 +26,9 @@ public function test($value)

public function parse($value)
{
if ($value === "") {
return null;
}
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}

Expand Down
36 changes: 29 additions & 7 deletions src/ValueType/BoolTypeTest.php
Expand Up @@ -4,15 +4,37 @@

class BoolTypeTest extends \PHPUnit\Framework\TestCase
{
public function testBoolTypeValidator()

public function boolDataProvider()
{
return [
/* input, expected, test pass */
["true", true, true],
["True", true, true],
["on", true, true],
["false", false, true],
["False", false, true],
["off", false, true],
["0", false, true],
["1", true, true],
["", null, true],

// for parse failed value, it should always return NULL
["foo", null, false],
["123", null, false],
];
}



/**
* @dataProvider boolDataProvider
*/
public function testBoolTypeValidator($input, $expected, $success)
{
$bool = new BoolType;
$this->assertTrue($bool->test('true'));
$this->assertTrue($bool->test('false'));
$this->assertTrue($bool->test('0'));
$this->assertTrue($bool->test('1'));
$this->assertFalse($bool->test('foo'));
$this->assertFalse($bool->test('123'));
$this->assertSame($success, $bool->test($input));
$this->assertSame($expected, $bool->parse($input));
}

public function falseValueProvider()
Expand Down
11 changes: 10 additions & 1 deletion src/ValueType/IntType.php
Expand Up @@ -2,15 +2,24 @@

namespace ActionKit\ValueType;

/**
* To support nullable, empty string will be treated as NULL
*/
class IntType extends BaseType
{
public function test($value)
{
return is_int($value) || filter_var($value, FILTER_VALIDATE_INT) || is_numeric($value);
return $value === ""
|| is_int($value)
|| filter_var($value, FILTER_VALIDATE_INT)
|| is_numeric($value);
}

public function parse($value)
{
if ($value === "") {
return null;
}
return filter_var($value, FILTER_VALIDATE_INT);
}

Expand Down
6 changes: 3 additions & 3 deletions src/ValueType/IntTypeTest.php
Expand Up @@ -12,10 +12,10 @@ public function intDataProvider()
[100 , 100 , true] ,
[-100 , -100 , true] ,

["", false , false],
["", null, true],

['123', 123, true],
['10', 10, true],
['10', 10, true],
['-10', -10, true],
['foo', false, false],
];
Expand All @@ -28,7 +28,7 @@ public function intDataProvider()
public function testIntTypeTest($input, $expect, $success)
{
$bool = new IntType;
$this->assertEquals($success, $bool->test($input));
$this->assertSame($success, $bool->test($input));
$this->assertSame($expect, $bool->parse($input));
}
}
Expand Down

0 comments on commit b8ad4dd

Please sign in to comment.