2014-07-19 15:37:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AddrTest;
|
|
|
|
|
|
|
|
use Addr\ResolverFactory,
|
|
|
|
Alert\ReactorFactory;
|
|
|
|
|
2014-07-21 15:12:51 +02:00
|
|
|
use Addr\Cache;
|
|
|
|
use Addr\Cache\APCCache;
|
|
|
|
use Addr\Cache\MemoryCache;
|
|
|
|
use Addr\Cache\RedisCache;
|
|
|
|
use Predis\Client as RedisClient;
|
|
|
|
use Predis\Connection\ConnectionException as RedisConnectionException;
|
|
|
|
|
|
|
|
|
2014-07-19 15:37:48 +02:00
|
|
|
class AddrTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
|
|
|
|
static private $redisEnabled = true;
|
|
|
|
|
|
|
|
static private $redisParameters = array(
|
|
|
|
'connection_timeout' => 2,
|
|
|
|
'read_write_timeout' => 2,
|
|
|
|
);
|
|
|
|
|
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
try {
|
2014-07-21 15:12:51 +02:00
|
|
|
$predisClient = new RedisClient(self::$redisParameters, []);
|
2014-07-19 15:37:48 +02:00
|
|
|
$predisClient->ping();
|
|
|
|
//It's connected
|
|
|
|
}
|
2014-07-21 15:12:51 +02:00
|
|
|
catch (RedisConnectionException $rce) {
|
2014-07-19 15:37:48 +02:00
|
|
|
self::$redisEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testWithNullCache() {
|
|
|
|
$this->basicRun(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testWithMemoryCache() {
|
2014-07-21 15:12:51 +02:00
|
|
|
$memoryCache = new MemoryCache();
|
2014-07-19 15:37:48 +02:00
|
|
|
$this->basicRun($memoryCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @requires extension APC
|
|
|
|
*/
|
|
|
|
function testWithApcCache() {
|
|
|
|
$prefix = time().uniqid('CacheTest');
|
2014-07-21 15:12:51 +02:00
|
|
|
$apcCache = new APCCache($prefix);
|
2014-07-19 15:37:48 +02:00
|
|
|
$this->basicRun($apcCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testWithRedisCache() {
|
|
|
|
if (self::$redisEnabled != true) {
|
|
|
|
$this->markTestSkipped("Could not connect to Redis, skipping test.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$prefix = time().'_'.uniqid('CacheTest');
|
|
|
|
try {
|
2014-07-21 15:12:51 +02:00
|
|
|
$redisClient = new RedisClient(self::$redisParameters, []);
|
2014-07-19 15:37:48 +02:00
|
|
|
}
|
2014-07-21 15:12:51 +02:00
|
|
|
catch (RedisConnectionException $rce) {
|
2014-07-19 15:37:48 +02:00
|
|
|
$this->markTestIncomplete("Could not connect to Redis server, cannot test redis cache.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:12:51 +02:00
|
|
|
$redisCache = new RedisCache($redisClient, $prefix);
|
2014-07-19 15:37:48 +02:00
|
|
|
$this->basicRun($redisCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group internet
|
|
|
|
*/
|
2014-07-21 15:12:51 +02:00
|
|
|
function basicRun(\Addr\Cache $cache = null) {
|
2014-07-19 15:37:48 +02:00
|
|
|
$names = [
|
|
|
|
'google.com',
|
|
|
|
'github.com',
|
|
|
|
'stackoverflow.com',
|
|
|
|
'localhost',
|
|
|
|
'192.168.0.1',
|
|
|
|
'::1',
|
|
|
|
];
|
|
|
|
|
|
|
|
$reactor = (new ReactorFactory)->select();
|
|
|
|
$resolver = (new ResolverFactory)->createResolver(
|
|
|
|
$reactor,
|
|
|
|
null, // $serverAddr = null,
|
|
|
|
null, //$serverPort = null,
|
|
|
|
null, //$requestTimeout = null,
|
|
|
|
$cache,
|
|
|
|
null //$hostsFilePath = null
|
|
|
|
);
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
|
|
|
|
foreach ($names as $name) {
|
|
|
|
$resolver->resolve($name, function($addr) use($name, $resolver, &$results) {
|
|
|
|
$results[$name] = $addr;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$reactor->run();
|
|
|
|
|
|
|
|
foreach ($results as $name => $addr) {
|
|
|
|
$validIP = filter_var($addr, FILTER_VALIDATE_IP);
|
|
|
|
$this->assertNotFalse(
|
|
|
|
$validIP,
|
|
|
|
"Server name $name did not resolve to a valid IP address"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertCount(
|
|
|
|
count($names),
|
|
|
|
$results,
|
|
|
|
"At least one of the name lookups did not resolve."
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2014-07-21 15:12:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|