8000 [PhpUnitBridge] Mock checkdnsrr() · symfony/symfony@5abd11d · GitHub
[go: up one dir, main page]

Skip to content

Commit 5abd11d

Browse files
[PhpUnitBridge] Mock checkdnsrr()
1 parent f29d46f commit 5abd11d

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Bridge\PhpUnit;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
class DnsMock
18+
{
19+
private static $resolutions = array();
20+
21+
public static function withMockedHosts(array $mockedHosts, $type = 'MX')
22+
{
23+
if (!$mockedHosts && false === $type) {
24+
self::$resolutions = array();
25+
} elseif ($mockedHosts) {
26+
self::$resolutions[$type] = $mockedHosts;
27+
} else {
28+
unset(self::$resolutions[$type]);
29+
}
30+
}
31+
32+
public static function checkdnsrr($host, $type = 'MX')
33+
{
34+
return isset(self::$resolutions[$type][$host])
35+
? self::$resolutions[$type][$host]
36+
: \checkdnsrr($host, $type);
37+
}
38+
39+
public static function register($class)
40+
{
41+
$self = get_called_class();
42+
43+
$mockedNs = array(substr($class, 0, strrpos($class, '\\')));
44+
if (strpos($class, '\\Tests\\')) {
45+
$ns = str_replace('\\Tests\\', '\\', $class);
46+
$mockedNs[] = substr($ns, 0, strrpos($ns, '\\'));
47+
}
48+
foreach ($mockedNs as $ns) {
49+
if (function_exists($ns.'\time')) {
50+
continue;
51+
}
52+
eval(<<<EOPHP
53+
namespace $ns;
54+
55+
function checkdnsrr(\$host, \$type = 'MX')
56+
{
57+
return \\$self::checkdnsrr(\$host, \$type);
58+
}
59+
60+
EOPHP
61+
);
62+
}
63+
}
64+
}

src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
2626
private $wasSkipped = array();
2727
private $isSkipped = array();
2828

29-
public function __construct(array $extraClockMockedNamespaces = array())
29+
public function __construct(array $extraClockMockedNamespaces = array(), array $extraDnsMockedNamespaces = array())
3030
{
3131
if ($extraClockMockedNamespaces) {
3232
foreach ($extraClockMockedNamespaces as $ns) {
3333
ClockMock::register($ns.'\DummyClass');
3434
}
3535
}
36+
if ($extraDnsMockedNamespaces) {
37+
foreach ($extraDnsMockedNamespaces as $ns) {
38+
DnsMock::register($ns.'\DummyClass');
39+
}
40+
}
3641
if (self::$globallyEnabled) {
3742
$this->state = -2;
3843
} else {
@@ -75,10 +80,16 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
7580
for ($i = 0; isset($testSuites[$i]); ++$i) {
7681
foreach ($testSuites[$i]->tests() as $test) {
7782
if ($test instanceof \PHPUnit_Framework_TestSuite) {
78-
if (class_exists($test->getName(), false) && in_array('time-sensitive', \PHPUnit_Util_Test::getGroups($test->getName()), true)) {
79-
ClockMock::register($test->getName());
80-
} else {
83+
if (!class_exists($test->getName(), false)) {
8184
$testSuites[] = $test;
85+
continue;
86+
}
87+
$groups = \PHPUnit_Util_Test::getGroups($test->getName());
88+
if (in_array('time-sensitive', $groups, true)) {
89+
ClockMock::register($test->getName());
90+
}
91+
if (in_array('dns-sensitive', $groups, true)) {
92+
DnsMock::register($test->getName());
8293
}
8394
}
8495
}
@@ -120,6 +131,9 @@ public function startTest(\PHPUnit_Framework_Test $test)
120131
ClockMock::register(get_class($test));
121132
ClockMock::withClockMock(true);
122133
}
134+
if (in_array('dns-sensitive', $groups, true)) {
135+
DnsMock::register(get_class($test));
136+
}
123137
}
124138
}
125139

@@ -131,6 +145,9 @@ public function endTest(\PHPUnit_Framework_Test $test, $time)
131145
if (in_array('time-sensitive', $groups, true)) {
132146
ClockMock::withClockMock(false);
133147
}
148+
if (in_array('dns-sensitive', $groups, true)) {
149+
DnsMock::withMockedHosts(array(), false);
150+
}
134151
}
135152
}
136153
}

src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Bridge\PhpUnit\DnsMock;
1415
use Symfony\Component\Validator\Constraints\Email;
1516
use Symfony\Component\Validator\Constraints\EmailValidator;
1617

18+
/**
19+
* @group dns-sensitive
20+
*/
1721
class EmailValidatorTest extends AbstractConstraintValidatorTest
1822
{
1923
protected function createValidator()
@@ -97,4 +101,46 @@ public function testStrict()
97101

98102
$this->assertNoViolation();
99103
}
104+
105+
/**
106+
* @dataProvider getDnsChecks
107+
*/
108+
public function testDnsChecks($type, $violation)
109+
{
110+
if (!class_exists('Symfony\Bridge\PhpUnit\DnsMock')) {
111+
$this->markTestSkipped('Symfony PhpUnit bridge 3.1 required.');
112+
}
113+
114+
foreach (array('MX', 'A', 'AAAA') as $t) {
115+
DnsMock::withMockedHosts(array('example.com' => !$violation && $t === $type), $t);
116+
}
117+
118+
$constraint = new Email(array(
119+
'message' => 'myMessage',
120+
'MX' === $type ? 'checkMX' : 'checkHost' => true,
121+
));
122+
123+
$this->validator->validate('foo@example.com', $constraint);
124+
125+
if (!$violation) {
126+
$this->assertNoViolation();
127+
} else {
128+
$this->buildViolation('myMessage')
129+
->setParameter('{{ value }}', '"foo@example.com"')
130+
->setCode($violation)
131+
->assertRaised();
132+
}
133+
}
134+
135+
public function getDnsChecks()
136+
{
137+
return array(
138+
array('MX', false),
139+
array('MX', Email::MX_CHECK_FAILED_ERROR),
140+
array('A', false),
141+
array('A', Email::HOST_CHECK_FAILED_ERROR),
142+
array('AAAA', false),
143+
array('AAAA', Email::HOST_CHECK_FAILED_ERROR),
144+
);
145+
}
100146
}

0 commit comments

Comments
 (0)
0