8000 [PhpUnitBridge] Mock DNS functions by nicolas-grekas · Pull Request #18181 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PhpUnitBridge] Mock DNS functions #18181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/PhpUnit/ClockMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function register($class)
$self = get_called_class();

$mockedNs = array(substr($class, 0, strrpos($class, '\\')));
if (strpos($class, '\\Tests\\')) {
if (0 < strpos($class, '\\Tests\\')) {
$ns = str_replace('\\Tests\\', '\\', $class);
$mockedNs[] = substr($ns, 0, strrpos($ns, '\\'));
}
Expand Down
224 changes: 224 additions & 0 deletions src/Symfony/Bridge/PhpUnit/DnsMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class DnsMock
{
private static $hosts = array();
private static $dnsTypes = array(
'A' => DNS_A,
'MX' => DNS_MX,
'NS' => DNS_NS,
'SOA' => DNS_SOA,
'PTR' => DNS_PTR,
'CNAME' => DNS_CNAME,
'AAAA' => DNS_AAAA,
'A6' => DNS_A6,
'SRV' => DNS_SRV,
'NAPTR' => DNS_NAPTR,
'TXT' => DNS_TXT,
'HINFO' => DNS_HINFO,
);

/**
* Configures the mock values for DNS queries.
*
* @param array $hosts Mocked hosts as keys, arrays of DNS records as returned by dns_get_record() as values.
*/
public static function withMockedHosts(array $hosts)
{
self::$hosts = $hosts;
}

public static function checkdnsrr($hostname, $type = 'MX')
{
if (!self::$hosts) {
return \checkdnsrr($hostname, $type);
}
if (isset(self::$hosts[$hostname])) {
$type = strtoupper($type);

foreach (self::$hosts[$hostname] as $record) {
if ($record['type'] === $type) {
return true;
}
if ('ANY' === $type && isset(self::$dnsTypes[$record['type']]) && 'HINFO' !== $record['type']) {
return true;
}
}
}

return false;
}

public static function getmxrr($hostname, &$mxhosts, &$weight = null)
{
if (!self::$hosts) {
return \getmxrr($hostname, $mxhosts, $weight);
}
$mxhosts = $weight = array();

if (isset(self::$hosts[$hostname])) {
foreach (self::$hosts[$hostname] as $record) {
if ('MX' === $record['type']) {
$mxhosts[] = $record['host'];
$weight[] = $record['pri'];
}
}
}

return (bool) $mxhosts;
}

public static function gethostbyaddr($ipAddress)
{
if (!self::$hosts) {
return \gethostbyaddr($ipAddress);
}
foreach (self::$hosts as $hostname => $records) {
foreach ($records as $record) {
if ('A' === $record['type'] && $ipAddress === $record['ip']) {
return $hostname;
}
if ('AAAA' === $record['type'] && $ipAddress === $record['ipv6']) {
return $hostname;
}
}
}

return $ipAddress;
}

public static function gethostbyname($hostname)
{
if (!self::$hosts) {
return \gethostbyname($hostname);
}
if (isset(self::$hosts[$hostname])) {
foreach (self::$hosts[$hostname] as $record) {
if ('A' === $record['type']) {
return $record['ip'];
}
}
}

return $hostname;
}

public static function gethostbynamel($hostname)
{
if (!self::$hosts) {
return \gethostbynamel($hostname);
}
$ips = false;

if (isset(self::$hosts[$hostname])) {
$ips = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could now return an empty array. I guess that is not possible using PHP's built-in function, right? Should we still return false at the end if $ips is empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is, as reported in the comments on php docs... It means some records have been found, but none for the A type (which is strange but possible, thus mockable)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was the use case I had in mind. But if the built-in function also returns an empty array, that's fine.


foreach (self::$hosts[$hostname] as $record) {
if ('A' === $record['type']) {
$ips[] = $record['ip'];
}
}
}

return $ips;
}

public static function dns_get_record($hostname, $type = DNS_ANY, &$authns = null, &$addtl = null, $raw = false)
{
if (!self::$hosts) {
return \dns_get_record($hostname, $type, $authns, $addtl, $raw);
}

$records = false;

if (isset(self::$hosts[$hostname])) {
if (DNS_ANY === $type) {
$type = DNS_ALL;
}
$records = array();

foreach (self::$hosts[$hostname] as $record) {
if (isset(self::$dnsTypes[$record['type']]) && (self::$dnsTypes[$record['type']] & $type)) {
$records[] = array_merge(array('host' => $hostname, 'class' => 'IN', 'ttl' => 1, 'type' => $record['type']), $record);
}
}
}

return $records;
}

public static function register($class)
{
$self = get_called_class();

$mockedNs = array(substr($class, 0, strrpos($class, '\\')));
if (0 < strpos($class, '\\Tests\\')) {
$ns = str_replace('\\Tests\\', '\\', $class);
$mockedNs[] = substr($ns, 0, strrpos($ns, '\\'));
}
foreach ($mockedNs as $ns) {
if (function_exists($ns.'\checkdnsrr')) {
continue;
}
eval(<<<EOPHP
namespace $ns;

function checkdnsrr(\$host, \$type = 'MX')
{
return \\$self::checkdnsrr(\$host, \$type);
}

function dns_check_record(\$host, \$type = 'MX')
{
return \\$self::checkdnsrr(\$host, \$type);
}

function getmxrr(\$hostname, &\$mxhosts, &\$weight = null)
{
return \\$self::getmxrr(\$hostname, \$mxhosts, \$weight);
}

function dns_get_mx(\$hostname, &\$mxhosts, &\$weight = null)
{
return \\$self::getmxrr(\$hostname, \$mxhosts, \$weight);
}

function gethostbyaddr(\$ipAddress)
{
return \\$self::gethostbyaddr(\$ipAddress);
}

function gethostbyname(\$hostname)
{
return \\$self::gethostbyname(\$hostname);
}

function gethostbynamel(\$hostname)
{
return \\$self::gethostbynamel(\$hostname);
}

function dns_get_record(\$hostname, \$type = DNS_ANY, &\$authns = null, &\$addtl = null, \$raw = false)
{
return \\$self::dns_get_record(\$hostname, \$type, \$authns, \$addtl, \$raw);
}

EOPHP
);
}
}
}
48 changes: 41 additions & 7 deletions src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,39 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
private $wasSkipped = array();
private $isSkipped = array();

public function __construct(array $extraClockMockedNamespaces = array())
/**
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive).
*/
public function __construct(array $mockedNamespaces = array())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should now document which keys (groups) are supported by the listener.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documented, ok for you ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

{
if ($extraClockMockedNamespaces) {
foreach ($extraClockMockedNamespaces as $ns) {
ClockMock::register($ns.'\DummyClass');
$warn = false;
foreach ($mockedNamespaces as $type => $namespaces) {
if (!is_array($namespaces)) {
$namespaces = array($namespaces);
}
if (is_int($type)) {
// @deprecated BC with v2.8 to v3.0
$type = 'time-sensitive';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can trigger a deprecation to get rid of this in 4.0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

triggering a deprecation is not possible, but I just added a simple "echo", ok for you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, looks good. Why is that only necessary if $globallyEnabled is false? Is it because otherwise the warning was already shown before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my mistake, fixed

$warn = true;
}
if ('time-sensitive' === $type) {
foreach ($namespaces as $ns) {
ClockMock::register($ns.'\DummyClass');
}
}
if ('dns-sensitive' === $type) {
foreach ($namespaces as $ns) {
DnsMock::register($ns.'\DummyClass');
}
}
}
if (self::$globallyEnabled) {
$this->state = -2;
} else {
self::$globallyEnabled = true;
if ($warn) {
echo "Clock-mocked namespaces for SymfonyTestsListener need to be nested in a \"time-sensitive\" key. This will be enforced in Symfony 4.0.\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writing this to stderr would be better than in stdout

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but did not work when I tried so I resorted to stdout, which is where phpunit and this listener echo in other cases...

}
}
}

Expand Down Expand Up @@ -75,10 +97,16 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
for ($i = 0; isset($testSuites[$i]); ++$i) {
foreach ($testSuites[$i]->tests() as $test) {
if ($test instanceof \PHPUnit_Framework_TestSuite) {
if (class_exists($test->getName(), false) && in_array('time-sensitive', \PHPUnit_Util_Test::getGroups($test->getName()), true)) {
ClockMock::register($test->getName());
} else {
if (!class_exists($test->getName(), false)) {
$testSuites[] = $test;
continue;
}
$groups = \PHPUnit_Util_Test::getGroups($test->getName());
if (in_array('time-sensitive', $groups, true)) {
ClockMock::register($test->getName());
}
if (in_array('dns-sensitive', $groups, true)) {
DnsMock::register($test->getName());
}
}
}
Expand Down Expand Up @@ -120,6 +148,9 @@ public function startTest(\PHPUnit_Framework_Test $test)
ClockMock::register(get_class($test));
ClockMock::withClockMock(true);
}
if (in_array('dns-sensitive', $groups, true)) {
DnsMock::register(get_class($test));
}
}
}

Expand All @@ -131,6 +162,9 @@ public function endTest(\PHPUnit_Framework_Test $test, $time)
if (in_array('time-sensitive', $groups, true)) {
ClockMock::withClockMock(false);
}
if (in_array('dns-sensitive', $groups, true)) {
DnsMock::withMockedHosts(array());
}
}
}
}
Loading
0