File: ValidationExceptionTest.php

package info (click to toggle)
php-laravel-framework 11.44.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 22,184 kB
  • sloc: php: 265,914; sh: 167; javascript: 51; makefile: 46
file content (189 lines) | stat: -rwxr-xr-x 5,947 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

namespace Illuminate\Tests\Validation;

use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\TestCase;

class ValidationExceptionTest extends TestCase
{
    public function testExceptionSummarizesZeroErrors()
    {
        $exception = $this->getException([], []);

        $this->assertSame('The given data was invalid.', $exception->getMessage());
    }

    public function testExceptionSummarizesOneError()
    {
        $exception = $this->getException([], ['foo' => 'required']);

        $this->assertSame('validation.required', $exception->getMessage());
    }

    public function testExceptionSummarizesTwoErrors()
    {
        $exception = $this->getException([], ['foo' => 'required', 'bar' => 'required']);

        $this->assertSame('validation.required (and 1 more error)', $exception->getMessage());
    }

    public function testExceptionSummarizesThreeOrMoreErrors()
    {
        $exception = $this->getException([], [
            'foo' => 'required',
            'bar' => 'required',
            'baz' => 'required',
        ]);

        $this->assertSame('validation.required (and 2 more errors)', $exception->getMessage());
    }

    public function testExceptionTranslatedSummarizesTwoErrors()
    {
        $translator = $this->getTranslator('uk', [
            '*' => [
                '*' => [
                    'uk' => [
                        '(and :count more error)' => '(та ще :count помилка)',
                        '(and :count more errors)' => '(та ще :count помилка)|(та ще :count помилки)|(та ще :count помилок)',
                    ],
                ],
            ],
        ]);

        $exception = $this->getException([], [
            'foo' => 'required',
            'bar' => 'required',
        ], $translator);

        $this->assertSame('validation.required (та ще 1 помилка)', $exception->getMessage());
    }

    public function testExceptionTranslatedSummarizesThreeOrMoreErrors()
    {
        $translator = $this->getTranslator('uk', [
            '*' => [
                '*' => [
                    'uk' => [
                        '(and :count more error)' => '(та ще :count помилка)',
                        '(and :count more errors)' => '(та ще :count помилка)|(та ще :count помилки)|(та ще :count помилок)',
                    ],
                ],
            ],
        ]);

        $exception = $this->getException([], [
            'foo' => 'required',
            'bar' => 'required',
            'baz' => 'required',
        ], $translator);

        $this->assertSame('validation.required (та ще 2 помилки)', $exception->getMessage());
    }

    public function testExceptionTranslatedSummarizesFiveOrMoreErrors()
    {
        $translator = $this->getTranslator('uk', [
            '*' => [
                '*' => [
                    'uk' => [
                        '(and :count more error)' => '(та ще :count помилка)',
                        '(and :count more errors)' => '(та ще :count помилка)|(та ще :count помилки)|(та ще :count помилок)',
                    ],
                ],
            ],
        ]);

        $exception = $this->getException([], [
            'foo' => 'required',
            'bar' => 'required',
            'baz' => 'required',
            'baq' => 'required',
            'baw' => 'required',
            'bae' => 'required',
        ], $translator);

        $this->assertSame('validation.required (та ще 5 помилок)', $exception->getMessage());
    }

    public function testExceptionErrorZeroErrors()
    {
        $exception = $this->getException([], []);

        $this->assertSame([], $exception->errors());
    }

    public function testExceptionErrorOneError()
    {
        $exception = $this->getException([], ['foo' => 'required']);

        $this->assertSame(['foo' => ['validation.required']], $exception->errors());
    }

    public function testExceptionStatusOneError()
    {
        $exception = $this->getException([], ['foo' => 'required']);
        $exception->status(500);

        $this->assertEquals(500, $exception->status);
    }

    public function testExceptionErrorBagOneError()
    {
        $exception = $this->getException([], ['foo' => 'required']);
        $exception->errorBag('milwad');

        $this->assertEquals('milwad', $exception->errorBag);
    }

    public function testExceptionRedirectToOneError()
    {
        $exception = $this->getException([], ['foo' => 'required']);
        $exception->redirectTo('https://google.com');

        $this->assertEquals('https://google.com', $exception->redirectTo);
    }

    public function testExceptionGetResponseOneError()
    {
        $exception = $this->getException([], ['foo' => 'required']);

        $this->assertNull($exception->getResponse());
    }

    public function testGetExceptionClassFromValidator()
    {
        $validator = $this->getValidator();

        $exception = $validator->getException();

        $this->assertEquals(ValidationException::class, $exception);
    }

    protected function getException($data = [], $rules = [], $translator = null)
    {
        $validator = $this->getValidator($data, $rules, $translator);

        return new ValidationException($validator);
    }

    protected function getValidator($data = [], $rules = [], $translator = null)
    {
        $translator ??= $this->getTranslator();

        return new Validator($translator, $data, $rules);
    }

    protected function getTranslator($locale = 'en', $loaded = [])
    {
        $translator ??= new Translator(new ArrayLoader, $locale);

        $translator->setLoaded($loaded);

        return $translator;
    }
}