Skip to content

Commit

Permalink
Make redis host configurable in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakzal authored and fabpot committed Sep 19, 2016
1 parent 476dadc commit c87de00
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Expand Up @@ -15,6 +15,7 @@
<env name="DUMP_STRING_LENGTH" value="" />
<env name="LDAP_HOST" value="127.0.0.1" />
<env name="LDAP_PORT" value="3389" />
<env name="REDIS_HOST" value="localhost" />
</php>

<testsuites>
Expand Down
Expand Up @@ -17,6 +17,16 @@

class CachePoolsTest extends WebTestCase
{
protected function setUp()
{
$_SERVER['SYMFONY__REDIS_HOST'] = getenv('REDIS_HOST');
}

protected function tearDown()
{
unset($_SERVER['SYMFONY__REDIS_HOST']);
}

public function testCachePools()
{
$this->doTestCachePools(array(), FilesystemAdapter::class);
Expand All @@ -30,7 +40,7 @@ public function testRedisCachePools()
try {
$this->doTestCachePools(array('root_config' => 'redis_config.yml', 'environment' => 'redis_cache'), RedisAdapter::class);
} catch (\PHPUnit_Framework_Error_Warning $e) {
if (0 !== strpos($e->getMessage(), 'unable to connect to 127.0.0.1')) {
if (0 !== strpos($e->getMessage(), 'unable to connect to')) {
throw $e;
}
$this->markTestSkipped($e->getMessage());
Expand All @@ -50,7 +60,7 @@ public function testRedisCustomCachePools()
try {
$this->doTestCachePools(array('root_config' => 'redis_custom_config.yml', 'environment' => 'custom_redis_cache'), RedisAdapter::class);
} catch (\PHPUnit_Framework_Error_Warning $e) {
if (0 !== strpos($e->getMessage(), 'unable to connect to 127.0.0.1')) {
if (0 !== strpos($e->getMessage(), 'unable to connect to')) {
throw $e;
}
$this->markTestSkipped($e->getMessage());
Expand Down
Expand Up @@ -4,6 +4,7 @@ imports:
framework:
cache:
app: cache.adapter.redis
default_redis_provider: "redis://%redis_host%"
pools:
cache.test:
public: true
Expand Up @@ -6,7 +6,7 @@ services:
public: false
class: Redis
calls:
- [connect, [127.0.0.1]]
- [connect, ['%redis_host%']]

cache.app:
parent: cache.adapter.redis
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/phpunit.xml.dist
Expand Up @@ -8,6 +8,7 @@
>
<php>
<ini name="error_reporting" value="-1" />
<env name="REDIS_HOST" value="localhost" />
</php>

<testsuites>
Expand Down
Expand Up @@ -33,7 +33,7 @@ public static function setupBeforeClass()
if (!extension_loaded('redis')) {
self::markTestSkipped('Extension redis required.');
}
if (!@((new \Redis())->connect('127.0.0.1'))) {
if (!@((new \Redis())->connect(getenv('REDIS_HOST')))) {
$e = error_get_last();
self::markTestSkipped($e['message']);
}
Expand Down
Expand Up @@ -19,20 +19,22 @@ class PredisAdapterTest extends AbstractRedisAdapterTest
public static function setupBeforeClass()
{
parent::setupBeforeClass();
self::$redis = new \Predis\Client();
self::$redis = new \Predis\Client(array('host' => getenv('REDIS_HOST')));
}

public function testCreateConnection()
{
$redis = RedisAdapter::createConnection('redis://localhost/1', array('class' => \Predis\Client::class, 'timeout' => 3));
$redisHost = getenv('REDIS_HOST');

$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/1', array('class' => \Predis\Client::class, 'timeout' => 3));
$this->assertInstanceOf(\Predis\Client::class, $redis);

$connection = $redis->getConnection();
$this->assertInstanceOf(StreamConnection::class, $connection);

$params = array(
'scheme' => 'tcp',
'host' => 'localhost',
'host' => $redisHost,
'path' => '',
'dbindex' => '1',
'port' => 6379,
Expand Down
14 changes: 8 additions & 6 deletions src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
Expand Up @@ -19,26 +19,28 @@ public static function setupBeforeClass()
{
parent::setupBeforeClass();
self::$redis = new \Redis();
self::$redis->connect('127.0.0.1');
self::$redis->connect(getenv('REDIS_HOST'));
}

public function testCreateConnection()
{
$redis = RedisAdapter::createConnection('redis://localhost');
$redisHost = getenv('REDIS_HOST');

$redis = RedisAdapter::createConnection('redis://'.$redisHost);
$this->assertInstanceOf(\Redis::class, $redis);
$this->assertTrue($redis->isConnected());
$this->assertSame(0, $redis->getDbNum());

$redis = RedisAdapter::createConnection('redis://localhost/2');
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2');
$this->assertSame(2, $redis->getDbNum());

$redis = RedisAdapter::createConnection('redis://localhost', array('timeout' => 3));
$redis = RedisAdapter::createConnection('redis://'.$redisHost, array('timeout' => 3));
$this->assertEquals(3, $redis->getTimeout());

$redis = RedisAdapter::createConnection('redis://localhost?timeout=4');
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?timeout=4');
$this->assertEquals(4, $redis->getTimeout());

$redis = RedisAdapter::createConnection('redis://localhost', array('read_timeout' => 5));
$redis = RedisAdapter::createConnection('redis://'.$redisHost, array('read_timeout' => 5));
$this->assertEquals(5, $redis->getReadTimeout());
}

Expand Down
Expand Up @@ -19,6 +19,6 @@ public static function setupBeforeClass()
if (!class_exists('RedisArray')) {
self::markTestSkipped('The RedisArray class is required.');
}
self::$redis = new \RedisArray(array('localhost'), array('lazy_connect' => true));
self::$redis = new \RedisArray(array(getenv('REDIS_HOST')), array('lazy_connect' => true));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/phpunit.xml.dist
Expand Up @@ -8,6 +8,7 @@
>
<php>
<ini name="error_reporting" value="-1" />
<env name="REDIS_HOST" value="localhost" />
</php>

<testsuites>
Expand Down

0 comments on commit c87de00

Please sign in to comment.