-
Notifications
You must be signed in to change notification settings - Fork 1
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
180 additions
and
0 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
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Elie\Validator\Rule; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class AbstractRuleTest extends TestCase | ||
{ | ||
|
||
protected function tearDown(): void | ||
{ | ||
\Mockery::close(); | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* @dataProvider getValueProvider | ||
*/ | ||
public function testValidateEmptyCases($value, $params, $expectedError, $expectedResult): void | ||
{ | ||
/*@var $rule RuleInterface */ | ||
$rule = \Mockery::mock(AbstractRule::class, ['key', $value, $params]) | ||
->makePartial(); | ||
|
||
$res = $rule->validate(); | ||
assertThat($res, identicalTo($expectedResult)); | ||
|
||
$error = $rule->getError(); | ||
assertThat($error, identicalTo($expectedError)); | ||
|
||
assertThat('key', identicalTo($rule->getKey())); | ||
} | ||
|
||
public function testValidatorValue(): void | ||
{ | ||
/*@var $rule RuleInterface */ | ||
$rule = \Mockery::mock(AbstractRule::class, ['key', ' ']) | ||
->makePartial(); | ||
|
||
$res = $rule->validate(); | ||
assertThat($res, identicalTo(RuleInterface::VALID)); | ||
|
||
assertThat('', equalTo($rule->getValue())); | ||
} | ||
|
||
public function getValueProvider(): \Generator | ||
{ | ||
yield 'Trim is true but required is false by default' => [ | ||
' ', [], '', RuleInterface::VALID | ||
]; | ||
|
||
yield 'Value could be empty if not required' => [ | ||
' ', ['trim' => true, 'required' => false], '', RuleInterface::VALID | ||
]; | ||
|
||
yield 'Required valued could be one character space' => [ | ||
' ', ['trim' => false, 'required' => true], '', RuleInterface::CHECK | ||
]; | ||
|
||
yield 'Required valued could be false' => [ | ||
false, ['required' => true], '', RuleInterface::CHECK | ||
]; | ||
|
||
yield 'Required value should not be empty string' => [ | ||
'', ['required' => true], 'key is required and should not be empty', RuleInterface::ERROR | ||
]; | ||
|
||
yield 'Required value should not be null' => [ | ||
null, ['required' => true], 'key is required and should not be empty', RuleInterface::ERROR | ||
]; | ||
|
||
yield 'Required value should not be empty array' => [ | ||
[], ['required' => true], 'key is required and should not be empty', RuleInterface::ERROR | ||
]; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Elie\Validator\Rule; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class NumericRuleTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider getNumericValueProvider | ||
*/ | ||
public function testValidate($value, $params, $expectedResult, $expectedError): void | ||
{ | ||
$rule = new NumericRule('age', $value, $params); | ||
|
||
$res = $rule->validate(); | ||
|
||
assertThat($res, identicalTo($expectedResult)); | ||
|
||
assertThat($rule->getError(), identicalTo($expectedError)); | ||
} | ||
|
||
public function getNumericValueProvider(): \Generator | ||
{ | ||
yield 'Given value could be empty' => [ | ||
'', [], RuleInterface::VALID, '' | ||
]; | ||
|
||
yield 'Given value between 24 and 29' => [ | ||
'25', ['min' => 24, 'max' => 29], RuleInterface::VALID, '' | ||
]; | ||
|
||
yield 'Given value should be numeric' => [ | ||
'nothing', ['min' => 24, 'max' => 29], RuleInterface::ERROR, 'age: nothing is not numeric' | ||
]; | ||
|
||
yield 'Given value is less than 24' => [ | ||
'21', ['min' => 24, 'max' => 29], RuleInterface::ERROR, 'age: 21 is less than 24' | ||
]; | ||
|
||
yield 'Given value is greated than 29' => [ | ||
'30', ['min' => 24, 'max' => 29], RuleInterface::ERROR, 'age: 30 is greater 29' | ||
]; | ||
|
||
yield 'Given value could not be empty' => [ | ||
'', ['required' => true], RuleInterface::ERROR, 'age is required and should not be empty' | ||
]; | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Elie\Validator\Rule; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class StringRuleTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider getStringValueProvider | ||
*/ | ||
public function testValidate($value, $params, $expectedResult, $expectedError): void | ||
{ | ||
$rule = new StringRule('name', $value, $params); | ||
|
||
$res = $rule->validate(); | ||
|
||
assertThat($res, identicalTo($expectedResult)); | ||
|
||
assertThat($rule->getError(), identicalTo($expectedError)); | ||
} | ||
|
||
public function getStringValueProvider(): \Generator | ||
{ | ||
yield 'Given value could be empty' => [ | ||
'', [], RuleInterface::VALID, '' | ||
]; | ||
|
||
yield 'Given value between 4 and 8 characters' => [ | ||
'Peter ', ['min' => 4, 'max' => 8], RuleInterface::VALID, '' | ||
]; | ||
|
||
yield 'Given value should be more than 3 characters' => [ | ||
'Simon ', ['min' => 3], RuleInterface::VALID, '' | ||
]; | ||
|
||
yield 'Given value should be a string' => [ | ||
25, [], RuleInterface::ERROR, 'name does not have a string value' | ||
]; | ||
|
||
yield 'Given value is not between 4 and 8 characters' => [ | ||
'Ben', ['min' => 4, 'max' => 8], RuleInterface::ERROR, 'name: The length of Ben is not between 4 and 8' | ||
]; | ||
} | ||
} |