Skip to content

Commit

Permalink
✅ TEST: Add validator rule test
Browse files Browse the repository at this point in the history
  • Loading branch information
elie29 committed Feb 20, 2019
1 parent 22cb356 commit e427b45
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

parameters:
level: max
ignoreErrors:
- '#^Call to an undefined method Mockery\\Mock.*#'

includes:
- vendor/phpstan/phpstan-mockery/extension.neon
78 changes: 78 additions & 0 deletions tests/Rule/AbstractRuleTest.php
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
];
}
}
52 changes: 52 additions & 0 deletions tests/Rule/NumericRuleTest.php
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'
];
}
}
48 changes: 48 additions & 0 deletions tests/Rule/StringRuleTest.php
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'
];
}
}

0 comments on commit e427b45

Please sign in to comment.