-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathValidationResultTest.php
More file actions
116 lines (105 loc) · 3.13 KB
/
ValidationResultTest.php
File metadata and controls
116 lines (105 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Selective\Validation\Test;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Selective\Validation\ValidationError;
use Selective\Validation\ValidationResult;
/**
* ValidationResult tests.
*/
#[CoversClass(ValidationResult::class)]
class ValidationResultTest extends TestCase
{
/**
* Test instance.
*/
public function testInstance(): void
{
$actual = new ValidationResult();
$this->assertInstanceOf(ValidationResult::class, $actual);
}
/**
* Tests addError and success functions.
* Tests addError function with two strings.
*/
public function testErrors(): void
{
$val = new ValidationResult();
$val->addError('error1', 'failed');
$result = $val->fails();
$this->assertTrue($result);
}
/**
* Tests addError and success functions.
* Tests addError function with an empty string for the first parameter.
*/
public function testErrorsEmptyFieldOne(): void
{
$val = new ValidationResult();
$val->addError('', 'invalid');
$result = $val->fails();
$this->assertTrue($result);
}
/**
* Tests addError and success functions.
* Tests addError function with an empty string for the second parameter.
*/
public function testErrorsWithField(): void
{
$val = new ValidationResult();
$val->addError('field', 'message');
$result = $val->fails();
$this->assertTrue($result);
}
/**
* Tests addError and success functions.
* Tests addError function with null for the second parameter.
*/
public function testErrorsWithMessage(): void
{
$val = new ValidationResult();
$val->addError('email', 'required', '5000');
$result = $val->fails();
$this->assertTrue($result);
$firstError = $val->getFirstError();
$this->assertInstanceOf(ValidationError::class, $firstError);
$this->assertSame('email', $firstError->getField());
$this->assertSame('required', $firstError->getMessage());
$this->assertSame('5000', $firstError->getCode());
}
/**
* Tests success function.
* Tests for no errors.
*/
public function testNoErrors(): void
{
$val = new ValidationResult();
$result = $val->fails();
$this->assertFalse($result);
}
/**
* Tests clear function.
*/
public function testClear(): void
{
$val = new ValidationResult();
$val->addError('email', 'required');
$val->clear();
$result = $val->fails();
$this->assertFalse($result);
}
/**
* Tests getErrors function.
*/
public function testGetErrors(): void
{
$val = new ValidationResult();
$errorFieldName = 'ERROR';
$errorMessage = 'This is an error!';
$val->addError($errorFieldName, $errorMessage);
$result = $val->getErrors()[0];
$this->assertSame($errorFieldName, $result->getField());
$this->assertSame($errorMessage, $result->getMessage());
$this->assertNull($result->getCode());
}
}