-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Boolean Validator | ||
* | ||
* Array Validator class | ||
* | ||
* @package Mooti | ||
* @subpackage Validator | ||
* @author Ken Lalobo <ken@mooti.io> | ||
*/ | ||
|
||
namespace Mooti\Validator\TypeValidator; | ||
|
||
use Mooti\Factory\Factory; | ||
use Mooti\Validator\Exception\DataValidationException; | ||
|
||
class BooleanValidator extends AbstractTypeValidator | ||
{ | ||
use Factory; | ||
|
||
/** | ||
* Validate some data and throw an exception if the data invalid | ||
* | ||
* @param array $constraints The rules | ||
* @param mixed $data The data to validate | ||
* @param mixed $prettyName Human readable name for the data being validated | ||
* | ||
* @throws DataValidationException | ||
*/ | ||
public function validate(array $constraints, $data, $prettyName = 'This value') | ||
{ | ||
if (!is_bool($data)) { | ||
$message = $constraints['message'] ?? '%s must be boolean'; | ||
throw new DataValidationException(sprintf($message, $prettyName)); | ||
} | ||
|
||
parent::validate($constraints, $data, $prettyName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
tests/phpunit/src/Unit/TypeValidator/BooleanValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Mooti\Test\PHPUnit\Validator\Unit\TypeValidator; | ||
|
||
use Mooti\Validator\Exception\DataValidationException; | ||
use Mooti\Validator\TypeValidator\BooleanValidator; | ||
|
||
class BooleanValidatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* @test | ||
* @expectedException \Mooti\Validator\Exception\DataValidationException | ||
* @expectedExceptionMessage This value must be boolean | ||
* @dataProvider badBooleans | ||
*/ | ||
public function validateDataThrowsDataValidationException($data) | ||
{ | ||
$constraints = []; | ||
|
||
$typeValidator = new BooleanValidator(); | ||
|
||
$typeValidator->validate($constraints, $data); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider goodBooleans | ||
*/ | ||
public function validateDataSuceeds($data) | ||
{ | ||
$constraints = []; | ||
|
||
$typeValidator = new BooleanValidator(); | ||
|
||
$typeValidator->validate($constraints, $data); | ||
} | ||
|
||
public function badBooleans() | ||
{ | ||
return [ | ||
['foobar'], | ||
[1], | ||
[''], | ||
[null] | ||
]; | ||
} | ||
|
||
public function goodBooleans() | ||
{ | ||
return [ | ||
[true], | ||
[false] | ||
]; | ||
} | ||
} |