Skip to content

Commit 5b4ff72

Browse files
committed
feat(php-validation): more tests
1 parent 0586715 commit 5b4ff72

File tree

5 files changed

+84
-11
lines changed

5 files changed

+84
-11
lines changed

src/Rule/EmptyRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class EmptyRule extends AbstractRule
88

99
public function check($value)
1010
{
11-
return !empty($value);
11+
return empty($value);
1212
}
1313

1414
public function getMessage()

src/Rule/RequiredRule.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Atakde\PhpValidation\Rule;
4+
5+
class RequiredRule extends AbstractRule
6+
{
7+
private $ruleName = 'required';
8+
9+
public function check($value)
10+
{
11+
return isset($value);
12+
}
13+
14+
public function getMessage()
15+
{
16+
return 'The value is required';
17+
}
18+
19+
public function getRuleName()
20+
{
21+
return $this->ruleName;
22+
}
23+
}

src/Validator.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@ public function __construct()
1616
'numeric' => new Rule\NumericRule(),
1717
'string' => new Rule\StringRule(),
1818
'email' => new Rule\EmailRule(),
19-
'password' => new Rule\PasswordRule()
19+
'password' => new Rule\PasswordRule(),
20+
'required' => new Rule\RequiredRule(),
2021
];
2122
}
2223

2324
public function validate($rules, $params)
2425
{
2526
foreach ($rules as $ruleField => $ruleString) {
26-
27-
if (!isset($params[$ruleField])) {
28-
$this->errors[$ruleField][] = 'The field ' . $ruleField . ' is not exists.';
29-
continue;
30-
}
31-
3227
$ruleArray = explode('|', $ruleString);
33-
3428
foreach ($ruleArray as $rule) {
35-
3629
if (isset($this->validators[$rule])) {
37-
if (!$this->validators[$rule]->check($params[$ruleField])) {
30+
$checkValue = isset($params[$ruleField]) ? $params[$ruleField] : null; // test fail if not doing this for now.
31+
if (!$this->validators[$rule]->check($checkValue)) {
3832
$this->errors[$ruleField][] = $this->validators[$rule]->getMessage();
3933
}
4034
} else {

tests/Rules/RequiredRuleTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
use Atakde\PhpValidation\Validator;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class RequiredRuleTest extends TestCase
9+
{
10+
protected $validator;
11+
12+
protected function setUp()
13+
{
14+
$this->validator = new Validator;
15+
}
16+
17+
/**
18+
* Check required rule is working or not
19+
*/
20+
public function testEmptyRule()
21+
{
22+
$this->validator->validate(['myInput' => 'required'], ['myInput' => '']); // giving an the input value in array
23+
$this->assertTrue($this->validator->passes());
24+
25+
$this->validator->validate(['myInput' => 'required'], []); // sending an empty array
26+
$this->assertTrue($this->validator->fails());
27+
}
28+
}

tests/Rules/StringRuleTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
use Atakde\PhpValidation\Validator;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class StringRuleTest extends TestCase
9+
{
10+
protected $validator;
11+
12+
protected function setUp()
13+
{
14+
$this->validator = new Validator;
15+
}
16+
17+
/**
18+
* Check string rule is working or not
19+
*/
20+
public function testEmptyRule()
21+
{
22+
$this->validator->validate(['myInput' => 'string'], ['myInput' => 'my string']); // giving a string value
23+
$this->assertTrue($this->validator->passes());
24+
25+
$this->validator->validate(['myInput' => 'string'], ['myInput' => 123]); // giving a number value
26+
$this->assertTrue($this->validator->fails());
27+
}
28+
}

0 commit comments

Comments
 (0)