Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 391d2c8

Browse filesBrowse files
[PhpUnitBridge] Mock DNS functions
1 parent f29d46f commit 391d2c8
Copy full SHA for 391d2c8

File tree

5 files changed

+377
-9
lines changed
Filter options

5 files changed

+377
-9
lines changed

‎composer.json

Copy file name to clipboardExpand all lines: composer.json
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@
9393
},
9494
"autoload": {
9595
"psr-4": {
96-
"Symfony\\Bridge\\Doctrine\\": "src/Symfony/Bridge/Doctrine/",
97-
"Symfony\\Bridge\\Monolog\\": "src/Symfony/Bridge/Monolog/",
98-
"Symfony\\Bridge\\ProxyManager\\": "src/Symfony/Bridge/ProxyManager/",
99-
"Symfony\\Bridge\\Twig\\": "src/Symfony/Bridge/Twig/",
96+
"Symfony\\Bridge\\": "src/Symfony/Bridge/",
10097
"Symfony\\Bundle\\": "src/Symfony/Bundle/",
10198
"Symfony\\Component\\": "src/Symfony/Component/"
10299
},

‎src/Symfony/Bridge/PhpUnit/ClockMock.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/ClockMock.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function register($class)
7474
$self = get_called_class();
7575

7676
$mockedNs = array(substr($class, 0, strrpos($class, '\\')));
77-
if (strpos($class, '\\Tests\\')) {
77+
if (0 < strpos($class, '\\Tests\\')) {
7878
$ns = str_replace('\\Tests\\', '\\', $class);
7979
$mockedNs[] = substr($ns, 0, strrpos($ns, '\\'));
8080
}
+224Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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 $hosts = array();
20+
private static $dnsTypes = array(
21+
'A' => DNS_A,
22+
'MX' => DNS_MX,
23+
'NS' => DNS_NS,
24+
'SOA' => DNS_SOA,
25+
'PTR' => DNS_PTR,
26+
'CNAME' => DNS_CNAME,
27+
'AAAA' => DNS_AAAA,
28+
'A6' => DNS_A6,
29+
'SRV' => DNS_SRV,
30+
'NAPTR' => DNS_NAPTR,
31+
'TXT' => DNS_TXT,
32+
'HINFO' => DNS_HINFO,
33+
);
34+
35+
/**
36+
* Configures the mock values for DNS queries.
37+
*
38+
* @param array $hosts Mocked hosts as keys, arrays of DNS records as returned by dns_get_record() as values.
39+
*/
40+
public static function withMockedHosts(array $hosts)
41+
{
42+
self::$hosts = $hosts;
43+
}
44+
45+
public static function checkdnsrr($hostname, $type = 'MX')
46+
{
47+
if (!self::$hosts) {
48+
return \checkdnsrr($hostname, $type);
49+
}
50+
if (isset(self::$hosts[$hostname])) {
51+
$type = strtoupper($type);
52+
53+
foreach (self::$hosts[$hostname] as $record) {
54+
if ($record['type'] === $type) {
55+
return true;
56+
}
57+
if ('ANY' === $type && isset(self::$dnsTypes[$record['type']]) && 'HINFO' !== $record['type']) {
58+
return true;
59+
}
60+
}
61+
}
62+
63+
return false;
64+
}
65+
66+
public static function getmxrr($hostname, &$mxhosts, &$weight = null)
67+
{
68+
if (!self::$hosts) {
69+
return \getmxrr($hostname, $mxhosts, $weight);
70+
}
71+
$mxhosts = $weight = array();
72+
73+
if (isset(self::$hosts[$hostname])) {
74+
foreach (self::$hosts[$hostname] as $record) {
75+
if ('MX' === $record['type']) {
76+
$mxhosts[] = $record['host'];
77+
$weight[] = $record['pri'];
78+
}
79+
}
80+
}
81+
82+
return (bool) $mxhosts;
83+
}
84+
85+
public static function gethostbyaddr($ipAddress)
86+
{
87+
if (!self::$hosts) {
88+
return \gethostbyaddr($ipAddress);
89+
}
90+
foreach (self::$hosts as $hostname => $records) {
91+
foreach ($records as $record) {
92+
if ('A' === $record['type'] && $ipAddress === $record['ip']) {
93+
return $hostname;
94+
}
95+
if ('AAAA' === $record['type'] && $ipAddress === $record['ipv6']) {
96+
return $hostname;
97+
}
98+
}
99+
}
100+
101+
return $ipAddress;
102+
}
103+
104+
public static function gethostbyname($hostname)
105+
{
106+
if (!self::$hosts) {
107+
return \gethostbyname($hostname);
108+
}
109+
if (isset(self::$hosts[$hostname])) {
110+
foreach (self::$hosts[$hostname] as $record) {
111+
if ('A' === $record['type']) {
112+
return $record['ip'];
113+
}
114+
}
115+
}
116+
117+
return $hostname;
118+
}
119+
120+
public static function gethostbynamel($hostname)
121+
{
122+
if (!self::$hosts) {
123+
return \gethostbynamel($hostname);
124+
}
125+
$ips = false;
126+
127+
if (isset(self::$hosts[$hostname])) {
128+
$ips = array();
129+
130+
foreach (self::$hosts[$hostname] as $record) {
131+
if ('A' === $record['type']) {
132+
$ips[] = $record['ip'];
133+
}
134+
}
135+
}
136+
137+
return $ips;
138+
}
139+
140+
public static function dns_get_record($hostname, $type = DNS_ANY, &$authns = null, &$addtl = null, $raw = false)
141+
{
142+
if (!self::$hosts) {
143+
return \dns_get_record($hostname, $type, $authns, $addtl, $raw);
144+
}
145+
146+
$records = false;
147+
148+
if (isset(self::$hosts[$hostname])) {
149+
if (DNS_ANY === $type) {
150+
return self::$hosts[$hostname];
151+
}
152+
$records = array();
153+
154+
foreach (self::$hosts[$hostname] as $record) {
155+
if (isset(self::$dnsTypes[$record['type']]) && (self::$dnsTypes[$record['type']] & $type)) {
156+
$records[] = $record;
157+
}
158+
}
159+
}
160+
161+
return $records;
162+
}
163+
164+
public static function register($class)
165+
{
166+
$self = get_called_class();
167+
168+
$mockedNs = array(substr($class, 0, strrpos($class, '\\')));
169+
if (0 < strpos($class, '\\Tests\\')) {
170+
$ns = str_replace('\\Tests\\', '\\', $class);
171+
$mockedNs[] = substr($ns, 0, strrpos($ns, '\\'));
172+
}
173+
foreach ($mockedNs as $ns) {
174+
if (function_exists($ns.'\checkdnsrr')) {
175+
continue;
176+
}
177+
eval(<<<EOPHP
178+
namespace $ns;
179+
180+
function checkdnsrr(\$host, \$type = 'MX')
181+
{
182+
return \\$self::checkdnsrr(\$host, \$type);
183+
}
184+
185+
function dns_check_record(\$host, \$type = 'MX')
186+
{
187+
return \\$self::checkdnsrr(\$host, \$type);
188+
}
189+
190+
function getmxrr(\$hostname, &\$mxhosts, &\$weight = null)
191+
{
192+
return \\$self::getmxrr(\$hostname, \$mxhosts, \$weight);
193+
}
194+
195+
function dns_get_mx(\$hostname, &\$mxhosts, &\$weight = null)
196+
{
197+
return \\$self::getmxrr(\$hostname, \$mxhosts, \$weight);
198+
}
199+
200+
function gethostbyaddr(\$ipAddress)
201+
{
202+
return \\$self::gethostbyaddr(\$ipAddress);
203+
}
204+
205+
function gethostbyname(\$hostname)
206+
{
207+
return \\$self::gethostbyname(\$hostname);
208+
}
209+
210+
function gethostbynamel(\$hostname)
211+
{
212+
return \\$self::gethostbynamel(\$hostname);
213+
}
214+
215+
function dns_get_record(\$hostname, \$type = DNS_ANY, &\$authns = null, &\$addtl = null, \$raw = false)
216+
{
217+
return \\$self::dns_get_record(\$hostname, \$type, \$authns, \$addtl, \$raw);
218+
}
219+
220+
EOPHP
221+
);
222+
}
223+
}
224+
}

‎src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php
+21-4Lines 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());
150+
}
134151
}
135152
}
136153
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.