1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telkins\Validation\Tests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
6
|
|
|
use Telkins\Validation\Tests\TestRuleSets\NewEmailRuleSet; |
7
|
|
|
use Telkins\Validation\Tests\TestRuleSets\NewConfirmedEmailRuleSet; |
8
|
|
|
|
9
|
|
|
class FieldRuleSetTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
protected function getError($key, $attribute, $extra = []) : string |
12
|
|
|
{ |
13
|
|
|
return __($key, array_merge(['attribute' => str_replace('_', ' ', $attribute)], $extra)); |
|
|
|
|
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @test |
18
|
|
|
*/ |
19
|
|
|
public function it_will_return_true_for_valid_values() |
20
|
|
|
{ |
21
|
|
|
$this->assertTrue((new NewEmailRuleSet())->passes('attribute', '[email protected]')); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @test |
26
|
|
|
*/ |
27
|
|
|
public function it_will_return_false_for_invalid_values() |
28
|
|
|
{ |
29
|
|
|
$this->assertFalse((new NewEmailRuleSet())->passes('attribute', 'not.an.email')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @test |
34
|
|
|
* @dataProvider providesInvalidValues |
35
|
|
|
*/ |
36
|
|
|
public function it_returns_the_expected_validation_message($value, $errorKey, $extra = []) |
37
|
|
|
{ |
38
|
|
|
$attribute = 'email_address'; |
39
|
|
|
|
40
|
|
|
$rule = new NewEmailRuleSet(); |
41
|
|
|
|
42
|
|
|
$rule->passes($attribute, $value); |
43
|
|
|
|
44
|
|
|
$this->assertEquals($this->getError($errorKey, $attribute, $extra), $rule->message()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function providesInvalidValues() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
[ |
51
|
|
|
null, |
52
|
|
|
'validation.required', |
53
|
|
|
], |
54
|
|
|
[ |
55
|
|
|
'not.an.email', |
56
|
|
|
'validation.email', |
57
|
|
|
], |
58
|
|
|
[ |
59
|
|
|
'[email protected]', |
60
|
|
|
'validation.max.string', |
61
|
|
|
['max' => 25], |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function it_plays_nice_with_confirmed_and_request_data() |
70
|
|
|
{ |
71
|
|
|
$requestData = [ |
72
|
|
|
'email_address' => '[email protected]', |
73
|
|
|
'email_address_confirmation' => '[email protected]', |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
$this->assertTrue((new NewConfirmedEmailRuleSet($requestData))->passes('email_address', '[email protected]')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
|
public function it_plays_nice_with_separate_confirmed_and_no_request_data() |
83
|
|
|
{ |
84
|
|
|
$requestData = [ |
85
|
|
|
'email_address' => '[email protected]', |
86
|
|
|
'email_address_confirmation' => '[email protected]', |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$validator = Validator::make($requestData, [ |
90
|
|
|
'email_address' => [ |
91
|
|
|
new NewEmailRuleSet(), |
92
|
|
|
'confirmed', |
93
|
|
|
], |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$this->assertTrue($validator->passes()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
*/ |
102
|
|
|
public function it_returns_the_expected_validation_message_when_cannot_confirm_with_confirmed_and_request_data() |
103
|
|
|
{ |
104
|
|
|
$attribute = 'email_address'; |
105
|
|
|
|
106
|
|
|
$requestData = [ |
107
|
|
|
$attribute => '[email protected]', |
108
|
|
|
// NO CONFIRMATION...! |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$rule = new NewConfirmedEmailRuleSet($requestData); |
112
|
|
|
|
113
|
|
|
$this->assertFalse($rule->passes($attribute, '[email protected]')); |
114
|
|
|
$this->assertEquals($this->getError('validation.confirmed', $attribute), $rule->message()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @test |
119
|
|
|
*/ |
120
|
|
|
public function it_returns_the_expected_validation_message_when_cannot_confirm_with_separate_confirmed_and_no_request_data() |
121
|
|
|
{ |
122
|
|
|
$attribute = 'email_address'; |
123
|
|
|
|
124
|
|
|
$validator = Validator::make( |
125
|
|
|
[ |
126
|
|
|
$attribute => '[email protected]', |
127
|
|
|
// NO CONFIRMATION...! |
128
|
|
|
], [ |
129
|
|
|
$attribute => [ |
130
|
|
|
new NewEmailRuleSet(), |
131
|
|
|
'confirmed', |
132
|
|
|
], |
133
|
|
|
] |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$this->assertFalse($validator->passes()); |
137
|
|
|
$this->assertEquals($this->getError('validation.confirmed', $attribute), $validator->errors()->first()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|