|
| 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\Bridge\PhpUnit; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Nicolas Grekas <[email protected]> |
| 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 | +} |
0 commit comments