Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/phpunit-bridge/DnsMock.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 $type = DNS_ALL; | |
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[] = array_merge(array('host' => $hostname, 'class' => 'IN', 'ttl' => 1, 'type' => $record['type']), $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 } elseif (0 === strpos($class, 'Tests\\')) { | |
173 $mockedNs[] = substr($class, 6, strrpos($class, '\\') - 6); | |
174 } | |
175 foreach ($mockedNs as $ns) { | |
176 if (function_exists($ns.'\checkdnsrr')) { | |
177 continue; | |
178 } | |
179 eval(<<<EOPHP | |
180 namespace $ns; | |
181 | |
182 function checkdnsrr(\$host, \$type = 'MX') | |
183 { | |
184 return \\$self::checkdnsrr(\$host, \$type); | |
185 } | |
186 | |
187 function dns_check_record(\$host, \$type = 'MX') | |
188 { | |
189 return \\$self::checkdnsrr(\$host, \$type); | |
190 } | |
191 | |
192 function getmxrr(\$hostname, &\$mxhosts, &\$weight = null) | |
193 { | |
194 return \\$self::getmxrr(\$hostname, \$mxhosts, \$weight); | |
195 } | |
196 | |
197 function dns_get_mx(\$hostname, &\$mxhosts, &\$weight = null) | |
198 { | |
199 return \\$self::getmxrr(\$hostname, \$mxhosts, \$weight); | |
200 } | |
201 | |
202 function gethostbyaddr(\$ipAddress) | |
203 { | |
204 return \\$self::gethostbyaddr(\$ipAddress); | |
205 } | |
206 | |
207 function gethostbyname(\$hostname) | |
208 { | |
209 return \\$self::gethostbyname(\$hostname); | |
210 } | |
211 | |
212 function gethostbynamel(\$hostname) | |
213 { | |
214 return \\$self::gethostbynamel(\$hostname); | |
215 } | |
216 | |
217 function dns_get_record(\$hostname, \$type = DNS_ANY, &\$authns = null, &\$addtl = null, \$raw = false) | |
218 { | |
219 return \\$self::dns_get_record(\$hostname, \$type, \$authns, \$addtl, \$raw); | |
220 } | |
221 | |
222 EOPHP | |
223 ); | |
224 } | |
225 } | |
226 } |