|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraints\Hostname; |
| 15 | +use Symfony\Component\Validator\Constraints\HostnameValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Dmitrii Poddubnyi <[email protected]> |
| 20 | + */ |
| 21 | +class HostnameValidatorTest extends ConstraintValidatorTestCase |
| 22 | +{ |
| 23 | + public function testNullIsValid() |
| 24 | + { |
| 25 | + $this->validator->validate(null, new Hostname()); |
| 26 | + |
| 27 | + $this->assertNoViolation(); |
| 28 | + } |
| 29 | + |
| 30 | + public function testEmptyStringIsValid() |
| 31 | + { |
| 32 | + $this->validator->validate('', new Hostname()); |
| 33 | + |
| 34 | + $this->assertNoViolation(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testExpectsStringCompatibleType() |
| 38 | + { |
| 39 | + $this->expectException(\Symfony\Component\Validator\Exception\UnexpectedValueException::class); |
| 40 | + |
| 41 | + $this->validator->validate(new \stdClass(), new Hostname()); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @dataProvider getValidMultilevelDomains |
| 46 | + */ |
| 47 | + public function testValidTldDomainsPassValidationIfTldRequired($domain) |
| 48 | + { |
| 49 | + $this->validator->validate($domain, new Hostname()); |
| 50 | + |
| 51 | + $this->assertNoViolation(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dataProvider getValidMultilevelDomains |
| 56 | + */ |
| 57 | + public function testValidTldDomainsPassValidationIfTldNotRequired($domain) |
| 58 | + { |
| 59 | + $this->validator->validate($domain, new Hostname(['requireTld' => false])); |
| 60 | + |
| 61 | + $this->assertNoViolation(); |
| 62 | + } |
| 63 | + |
| 64 | + public function getValidMultilevelDomains() |
| 65 | + { |
| 66 | + return [ |
| 67 | + ['symfony.com'], |
| 68 | + ['example.co.uk'], |
| 69 | + ['example.fr'], |
| 70 | + ['example.com'], |
| 71 | + ['xn--diseolatinoamericano-66b.com'], |
| 72 | + ['xn--ggle-0nda.com'], |
| 73 | + ['www.xn--simulateur-prt-2kb.fr'], |
| 74 | + [sprintf('%s.com', str_repeat('a', 20))], |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @dataProvider getInvalidDomains |
| 80 | + */ |
| 81 | + public function testInvalidDomainsRaiseViolationIfTldRequired($domain) |
| 82 | + { |
| 83 | + $this->validator->validate($domain, new Hostname([ |
| 84 | + 'message' => 'myMessage', |
| 85 | + ])); |
| 86 | + |
| 87 | + $this->buildViolation('myMessage') |
| 88 | + ->setParameter('{{ value }}', '"'.$domain.'"') |
| 89 | + ->setCode(Hostname::INVALID_HOSTNAME_ERROR) |
| 90 | + ->assertRaised(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @dataProvider getInvalidDomains |
| 95 | + */ |
| 96 | + public function testInvalidDomainsRaiseViolationIfTldNotRequired($domain) |
| 97 | + { |
| 98 | + $this->validator->validate($domain, new Hostname([ |
| 99 | + 'message' => 'myMessage', |
| 100 | + 'requireTld' => false, |
| 101 | + ])); |
| 102 | + |
| 103 | + $this->buildViolation('myMessage') |
| 104 | + ->setParameter('{{ value }}', '"'.$domain.'"') |
| 105 | + ->setCode(Hostname::INVALID_HOSTNAME_ERROR) |
| 106 | + ->assertRaised(); |
| 107 | + } |
| 108 | + |
| 109 | + public function getInvalidDomains() |
| 110 | + { |
| 111 | + return [ |
| 112 | + ['acme..com'], |
| 113 | + ['qq--.com'], |
| 114 | + ['-example.com'], |
| 115 | + ['example-.com'], |
| 116 | + [sprintf('%s.com', str_repeat('a', 300))], |
| 117 | + ]; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @dataProvider getReservedDomains |
| 122 | + */ |
| 123 | + public function testReservedDomainsPassValidationIfTldNotRequired($domain) |
| 124 | + { |
| 125 | + $this->validator->validate($domain, new Hostname(['requireTld' => false])); |
| 126 | + |
| 127 | + $this->assertNoViolation(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @dataProvider getReservedDomains |
| 132 | + */ |
| 133 | + public function testReservedDomainsRaiseViolationIfTldRequired($domain) |
| 134 | + { |
| 135 | + $this->validator->validate($domain, new Hostname([ |
| 136 | + 'message' => 'myMessage', |
| 137 | + 'requireTld' => true, |
| 138 | + ])); |
| 139 | + |
| 140 | + $this->buildViolation('myMessage') |
| 141 | + ->setParameter('{{ value }}', '"'.$domain.'"') |
| 142 | + ->setCode(Hostname::INVALID_HOSTNAME_ERROR) |
| 143 | + ->assertRaised(); |
| 144 | + } |
| 145 | + |
| 146 | + public function getReservedDomains() |
| 147 | + { |
| 148 | + return [ |
| 149 | + ['example'], |
| 150 | + ['foo.example'], |
| 151 | + ['invalid'], |
| 152 | + ['bar.invalid'], |
| 153 | + ['localhost'], |
| 154 | + ['lol.localhost'], |
| 155 | + ['test'], |
| 156 | + ['abc.test'], |
| 157 | + ]; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @dataProvider getTopLevelDomains |
| 162 | + */ |
| 163 | + public function testTopLevelDomainsPassValidationIfTldNotRequired($domain) |
| 164 | + { |
| 165 | + $this->validator->validate($domain, new Hostname(['requireTld' => false])); |
| 166 | + |
| 167 | + $this->assertNoViolation(); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @dataProvider getTopLevelDomains |
| 172 | + */ |
| 173 | + public function testTopLevelDomainsRaiseViolationIfTldRequired($domain) |
| 174 | + { |
| 175 | + $this->validator->validate($domain, new Hostname([ |
| 176 | + 'message' => 'myMessage', |
| 177 | + 'requireTld' => true, |
| 178 | + ])); |
| 179 | + |
| 180 | + $this->buildViolation('myMessage') |
| 181 | + ->setParameter('{{ value }}', '"'.$domain.'"') |
| 182 | + ->setCode(Hostname::INVALID_HOSTNAME_ERROR) |
| 183 | + ->assertRaised(); |
| 184 | + } |
| 185 | + |
| 186 | + public function getTopLevelDomains() |
| 187 | + { |
| 188 | + return [ |
| 189 | + ['com'], |
| 190 | + ['net'], |
| 191 | + ['org'], |
| 192 | + ['etc'], |
| 193 | + ]; |
| 194 | + } |
| 195 | + |
| 196 | + protected function createValidator() |
| 197 | + { |
| 198 | + return new HostnameValidator(); |
| 199 | + } |
| 200 | +} |
0 commit comments