From a4b42d1fbddaa12d57ae99a8c95dbef05647aad9 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 16 Mar 2016 15:58:21 +0100 Subject: [PATCH] [Validator] Test DNS Url constraints using checkdnsrr() mock --- .../Tests/Constraints/UrlValidatorTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php index ab22876a1feb1..fab7cf18675b5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php @@ -11,10 +11,14 @@ namespace Symfony\Component\Validator\Tests\Constraints; +use Symfony\Bridge\PhpUnit\DnsMock; use Symfony\Component\Validator\Constraints\Url; use Symfony\Component\Validator\Constraints\UrlValidator; use Symfony\Component\Validator\Validation; +/** + * @group dns-sensitive + */ class UrlValidatorTest extends AbstractConstraintValidatorTest { protected function getApiVersion() @@ -187,6 +191,34 @@ public function getValidCustomUrls() array('git://[::1]/'), ); } + + /** + * @dataProvider getCheckDns + */ + public function testCheckDns($violation) + { + DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? '' : 'A')))); + + $constraint = new Url(array( + 'checkDNS' => true, + 'dnsMessage' => 'myMessage', + )); + + $this->validator->validate('http://example.com', $constraint); + + if (!$violation) { + $this->assertNoViolation(); + } else { + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"example.com"') + ->assertRaised(); + } + } + + public function getCheckDns() + { + return array(array(true), array(false)); + } } class EmailProvider