|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
use AdvancedLearning\InputValidator\Exceptions\InputValidationException;
|
|
4
|
|
|
use AdvancedLearning\InputValidator\InputValidator;
|
|
5
|
|
|
use AdvancedLearning\InputValidator\Interfaces\MappableModel;
|
|
6
|
|
|
use Respect\Validation\Validator as v;
|
|
7
|
|
|
|
|
8
|
|
|
/**
|
|
9
|
|
|
* Test validator
|
|
10
|
|
|
*/
|
|
11
|
|
|
class ValidatorTest extends PHPUnit_Framework_TestCase
|
|
12
|
|
|
{
|
|
13
|
|
|
public function setUp()
|
|
14
|
|
|
{
|
|
15
|
|
|
parent::setUp();
|
|
16
|
|
|
}
|
|
17
|
|
|
|
|
18
|
|
|
public function testErrors()
|
|
19
|
|
|
{
|
|
20
|
|
|
$data = [
|
|
21
|
|
|
'FirstName' => '',
|
|
22
|
|
|
'LastName' => 'T',
|
|
23
|
|
|
'DateOfBirth' => '2017-09-20'
|
|
24
|
|
|
];
|
|
25
|
|
|
|
|
26
|
|
|
$validator = new TestInputValidator();
|
|
27
|
|
|
$validator->setMessages([
|
|
28
|
|
|
'notBlank' => 'Please enter a value'
|
|
29
|
|
|
]);
|
|
30
|
|
|
|
|
31
|
|
|
try {
|
|
32
|
|
|
$validator->valid($data);
|
|
33
|
|
|
} catch (InputValidationException $e) {
|
|
34
|
|
|
$errors = $e->getMessages();
|
|
35
|
|
|
$fieldMessages = $e->getFieldMessages();
|
|
36
|
|
|
}
|
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals(4, count($errors), 'There should be four errors');
|
|
39
|
|
|
$this->assertNotEmpty($errors['FirstName'], 'FirstName should be in validation errors');
|
|
40
|
|
|
$this->assertEquals(2, count($errors['LastName']), 'LastName should have 2 errors');
|
|
41
|
|
|
$this->assertTrue(is_string($fieldMessages['LastName']), 'Last Name should have a single message');
|
|
42
|
|
|
$this->assertEquals(
|
|
43
|
|
|
$errors['FirstName'][0],
|
|
44
|
|
|
'Please enter a value',
|
|
45
|
|
|
'Not blank message should have been updated'
|
|
46
|
|
|
);
|
|
47
|
|
|
}
|
|
48
|
|
|
|
|
49
|
|
|
public function testMappableModel()
|
|
50
|
|
|
{
|
|
51
|
|
|
$model = new MappableObject();
|
|
52
|
|
|
$validator = new TestInputValidator();
|
|
53
|
|
|
|
|
54
|
|
|
try {
|
|
55
|
|
|
$validator->validateModel($model);
|
|
56
|
|
|
} catch (InputValidationException $e) {
|
|
57
|
|
|
$errors = $e->getMessages();
|
|
58
|
|
|
}
|
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals(4, count($errors), 'There should be four errors');
|
|
61
|
|
|
}
|
|
62
|
|
|
}
|
|
63
|
|
|
|
|
64
|
|
|
class TestInputValidator extends InputValidator
|
|
65
|
|
|
{
|
|
66
|
|
|
/**
|
|
67
|
|
|
* @inheritdoc
|
|
68
|
|
|
*/
|
|
69
|
|
|
public function getRules()
|
|
70
|
|
|
{
|
|
71
|
|
|
return [
|
|
72
|
|
|
'FirstName' => v::notBlank()->setName('First Name'),
|
|
73
|
|
|
'LastName' => v::notBlank()
|
|
74
|
|
|
->lowercase()
|
|
75
|
|
|
->length(2)
|
|
76
|
|
|
->setName('Last Name'),
|
|
77
|
|
|
'NHI' => v::notBlank()->setName('NHI'),
|
|
78
|
|
|
'DateOfBirth' => v::date('Y-m-d')
|
|
79
|
|
|
->notEmpty()
|
|
80
|
|
|
->between('1900-01-01', (new \DateTime('2017-08-01'))->format('Y-m-d'))
|
|
81
|
|
|
->setName('Date of Birth')
|
|
82
|
|
|
];
|
|
83
|
|
|
}
|
|
84
|
|
|
}
|
|
85
|
|
|
|
|
86
|
|
|
class MappableObject implements MappableModel
|
|
87
|
|
|
{
|
|
88
|
|
|
public function toMap()
|
|
89
|
|
|
{
|
|
90
|
|
|
return [
|
|
91
|
|
|
'FirstName' => '',
|
|
92
|
|
|
'LastName' => 'T',
|
|
93
|
|
|
'DateOfBirth' => '2017-09-20'
|
|
94
|
|
|
];
|
|
95
|
|
|
}
|
|
96
|
|
|
} |