Completed
Push — issue#736 ( 78f04a )
by Guilherme
19:20
created

UriValidatorTest::testValidUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\ValidationBundle\Tests\Validator\Constraints;
12
13
use LoginCidadao\ValidationBundle\Validator\Constraints\Uri;
14
use LoginCidadao\ValidationBundle\Validator\Constraints\UriValidator;
15
use Symfony\Bridge\PhpUnit\DnsMock;
16
use Symfony\Component\Validator\Constraints\Url;
17
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
18
use Symfony\Component\Validator\Validation;
19
20
/**
21
 * @group dns-sensitive
22
 */
23
class UriValidatorTest extends AbstractConstraintValidatorTest
24
{
25
    protected function getApiVersion()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
26
    {
27
        return Validation::API_VERSION_2_5;
28
    }
29
30
    protected function createValidator()
31
    {
32
        return new UriValidator();
33
    }
34
35
    public function testNullIsValid()
36
    {
37
        $this->validator->validate(null, new Uri());
38
39
        $this->assertNoViolation();
40
    }
41
42
    public function testEmptyStringIsValid()
43
    {
44
        $this->validator->validate('', new Uri());
45
46
        $this->assertNoViolation();
47
    }
48
49
    public function testEmptyStringFromObjectIsValid()
50
    {
51
        $this->validator->validate(new EmailProvider(), new Uri());
52
53
        $this->assertNoViolation();
54
    }
55
56
    /**
57
     * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
58
     */
59
    public function testExpectsStringCompatibleType()
60
    {
61
        $this->validator->validate(new \stdClass(), new Uri());
62
    }
63
64
    /**
65
     * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
66
     */
67
    public function testExpectsUriConstraint()
68
    {
69
        $this->validator->validate('', new Url());
70
    }
71
72
    /**
73
     * @dataProvider getValidUrls
74
     */
75
    public function testValidUrls($url)
76
    {
77
        $this->validator->validate($url, new Uri());
78
79
        $this->assertNoViolation();
80
    }
81
82
    public function getValidUrls()
83
    {
84
        return [
85
            ['http://a.pl'],
86
            ['http://www.google.com'],
87
            ['http://www.google.com.'],
88
            ['http://www.google.museum'],
89
            ['https://google.com/'],
90
            ['https://google.com:80/'],
91
            ['http://www.example.coop/'],
92
            ['http://www.test-example.com/'],
93
            ['http://www.symfony.com/'],
94
            ['http://symfony.fake/blog/'],
95
            ['http://symfony.com/?'],
96
            ['http://symfony.com/search?type=&q=url+validator'],
97
            ['http://symfony.com/#'],
98
            ['http://symfony.com/#?'],
99
            ['http://www.symfony.com/doc/current/book/validation.html#supported-constraints'],
100
            ['http://very.long.domain.name.com/'],
101
            ['http://localhost/'],
102
            ['http://myhost123/'],
103
            ['http://127.0.0.1/'],
104
            ['http://127.0.0.1:80/'],
105
            ['http://[::1]/'],
106
            ['http://[::1]:80/'],
107
            ['http://[1:2:3::4:5:6:7]/'],
108
            ['http://xn--sopaulo-xwa.com/'],
109
            ['http://xn--sopaulo-xwa.com.br/'],
110
            ['http://xn--e1afmkfd.xn--80akhbyknj4f/'],
111
            ['http://xn--mgbh0fb.xn--kgbechtv/'],
112
            ['http://xn--fsqu00a.xn--0zwm56d/'],
113
            ['http://xn--fsqu00a.xn--g6w251d/'],
114
            ['http://xn--r8jz45g.xn--zckzah/'],
115
            ['http://xn--mgbh0fb.xn--hgbk6aj7f53bba/'],
116
            ['http://xn--9n2bp8q.xn--9t4b11yi5a/'],
117
            ['http://xn--ogb.idn.icann.org/'],
118
            ['http://xn--e1afmkfd.xn--80akhbyknj4f.xn--e1afmkfd/'],
119
            ['http://xn--espaa-rta.xn--ca-ol-fsay5a/'],
120
            ['http://xn--d1abbgf6aiiy.xn--p1ai/'],
121
            ['http://username:[email protected]'],
122
            ['http://[email protected]'],
123
            ['http://symfony.com?'],
124
            ['http://symfony.com?query=1'],
125
            ['http://symfony.com/?query=1'],
126
            ['http://symfony.com#'],
127
            ['http://symfony.com#fragment'],
128
            ['http://symfony.com/#fragment'],
129
            ['http://symfony.com/#one_more%20test'],
130
            ['custom-scheme://symfony.com'],
131
        ];
132
    }
133
134
    /**
135
     * @dataProvider getInvalidUrls
136
     */
137
    public function testInvalidUrls($url)
138
    {
139
        $constraint = new Uri(['message' => 'myMessage']);
140
141
        $this->validator->validate($url, $constraint);
142
143
        $this->buildViolation('myMessage')
144
            ->setParameter('{{ value }}', '"'.$url.'"')
145
            ->setCode(Uri::INVALID_URL_ERROR)
146
            ->assertRaised();
147
    }
148
149
    public function getInvalidUrls()
150
    {
151
        return [
152
            ['google.com'],
153
            ['://google.com'],
154
            ['http ://google.com'],
155
            ['http:/google.com'],
156
            ['http://google.com::aa'],
157
            ['http://google.com:aa'],
158
            ['http://127.0.0.1:aa/'],
159
            ['http://[::1'],
160
            ['http://hello.☎/'],
161
            ['http://:password@@symfony.com'],
162
            ['http://username:passwordsymfony.com'],
163
            ['http://usern@me:[email protected]'],
164
            ['http://example.com/exploit.html?<script>alert(1);</script>'],
165
            ['http://example.com/exploit.html?hel lo'],
166
            ['http://example.com/exploit.html?not_a%hex'],
167
            ['invalid scheme://example.com/'],
168
            ['http://'],
169
        ];
170
    }
171
172
    /**
173
     * @dataProvider getValidCustomUrls
174
     */
175
    public function testCustomProtocolIsValid($url)
176
    {
177
        $constraint = new Uri();
178
179
        $this->validator->validate($url, $constraint);
180
181
        $this->assertNoViolation();
182
    }
183
184
    public function getValidCustomUrls()
185
    {
186
        return [
187
            ['ftp://google.com'],
188
            ['file://127.0.0.1'],
189
            ['git://[::1]/'],
190
        ];
191
    }
192
193
    /**
194
     * @dataProvider getCheckDns
195
     * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
196
     */
197
    public function testCheckDns($violation)
198
    {
199
        DnsMock::withMockedHosts(['example.com' => [['type' => $violation ? '' : 'A']]]);
200
201
        $constraint = new Uri([
202
            'checkDNS' => true,
203
            'dnsMessage' => 'myMessage',
204
        ]);
205
206
        $this->validator->validate('http://example.com', $constraint);
207
208
        if (!$violation) {
209
            $this->assertNoViolation();
210
        } else {
211
            $this->buildViolation('myMessage')
212
                ->setParameter('{{ value }}', '"example.com"')
213
                ->setCode(Uri::INVALID_URL_ERROR)
214
                ->assertRaised();
215
        }
216
    }
217
218
    public function getCheckDns()
219
    {
220
        return [[true], [false]];
221
    }
222
}
223
224
class EmailProvider
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
225
{
226
    public function __toString()
227
    {
228
        return '';
229
    }
230
}
231