FieldRuleSetTest::providesInvalidValues()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 15
rs 10
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));
0 ignored issues
show
Bug Best Practice introduced by
The expression return __($key, array_me... $attribute)), $extra)) could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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
    /**
48
     * @test
49
     */
50
    public function it_can_use_a_rule_set_except_certain_rules()
51
    {
52
        $attribute = 'email_address';
53
54
        $rule = (new NewEmailRuleSet)->except('required', 'email');
55
56
        $this->assertTrue($rule->passes($attribute, null));
57
    }
58
59
    public function providesInvalidValues()
60
    {
61
        return [
62
            [
63
                null,
64
                'validation.required',
65
            ],
66
            [
67
                'not.an.email',
68
                'validation.email',
69
            ],
70
            [
71
                '[email protected]',
72
                'validation.max.string',
73
                ['max' => 25],
74
            ],
75
        ];
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function it_plays_nice_with_confirmed_and_request_data()
82
    {
83
        $requestData = [
84
            'email_address' => '[email protected]',
85
            'email_address_confirmation' => '[email protected]',
86
        ];
87
88
        $this->assertTrue((new NewConfirmedEmailRuleSet($requestData))->passes('email_address', '[email protected]'));
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function it_plays_nice_with_separate_confirmed_and_no_request_data()
95
    {
96
        $requestData = [
97
            'email_address' => '[email protected]',
98
            'email_address_confirmation' => '[email protected]',
99
        ];
100
101
        $validator = Validator::make($requestData, [
102
            'email_address' => [
103
                new NewEmailRuleSet(),
104
                'confirmed',
105
            ],
106
        ]);
107
108
        $this->assertTrue($validator->passes());
109
    }
110
111
    /**
112
     * @test
113
     */
114
    public function it_returns_the_expected_validation_message_when_cannot_confirm_with_confirmed_and_request_data()
115
    {
116
        $attribute = 'email_address';
117
118
        $requestData = [
119
            $attribute => '[email protected]',
120
            // NO CONFIRMATION...!
121
        ];
122
123
        $rule = new NewConfirmedEmailRuleSet($requestData);
124
125
        $this->assertFalse($rule->passes($attribute, '[email protected]'));
126
        $this->assertEquals($this->getError('validation.confirmed', $attribute), $rule->message());
127
    }
128
129
    /**
130
     * @test
131
     */
132
    public function it_returns_the_expected_validation_message_when_cannot_confirm_with_separate_confirmed_and_no_request_data()
133
    {
134
        $attribute = 'email_address';
135
136
        $validator = Validator::make(
137
            [
138
                $attribute => '[email protected]',
139
                // NO CONFIRMATION...!
140
            ], [
141
                $attribute => [
142
                    new NewEmailRuleSet(),
143
                    'confirmed',
144
                ],
145
            ]
146
        );
147
148
        $this->assertFalse($validator->passes());
149
        $this->assertEquals($this->getError('validation.confirmed', $attribute), $validator->errors()->first());
150
    }
151
}
152