Skip to content

Commit

Permalink
Ensure that a redis connection is available before interacting with r…
Browse files Browse the repository at this point in the history
…edis (#15930)

See #15929
  • Loading branch information
colindecarlo authored and taylorotwell committed Oct 15, 2016
1 parent 4683c79 commit 5ab5179
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 64 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
"Illuminate\\": "src/Illuminate/"
}
},
"autoload-dev": {
"files": [
"tests/Redis/InteractsWithRedis.php"
]
},
"extra": {
"branch-alias": {
"dev-master": "5.4-dev"
Expand Down
24 changes: 3 additions & 21 deletions tests/Cache/RedisCacheIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,23 @@

use Illuminate\Cache\RedisStore;
use Illuminate\Cache\Repository;
use Illuminate\Redis\PredisDatabase;
use Mockery as m;

class RedisCacheTest extends PHPUnit_Framework_TestCase
{
/**
* @var PredisDatabase
*/
private $redis;
use InteractsWithRedis;

public function setUp()
{
parent::setUp();

$host = getenv('REDIS_HOST') ?: '127.0.0.1';
$port = getenv('REDIS_PORT') ?: 6379;

$this->redis = new PredisDatabase([
'cluster' => false,
'default' => [
'host' => $host,
'port' => $port,
'database' => 5,
'timeout' => 0.5,
],
]);

$this->redis->connection()->flushdb();
$this->setUpRedis();
}

public function tearDown()
{
parent::tearDown();
m::close();
$this->redis->connection()->flushdb();
$this->tearDownRedis();
}

public function testRedisCacheAddTwice()
Expand Down
46 changes: 3 additions & 43 deletions tests/Queue/RedisQueueIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@
use Illuminate\Queue\RedisQueue;
use Illuminate\Container\Container;
use Illuminate\Queue\Jobs\RedisJob;
use Illuminate\Redis\PredisDatabase;

class RedisQueueIntegrationTest extends PHPUnit_Framework_TestCase
{
/**
* @var bool
*/
private static $connectionFailedOnceWithDefaultsSkip = false;

/**
* @var Database
*/
private $redis;
use InteractsWithRedis;

/**
* @var RedisQueue
Expand All @@ -26,36 +17,7 @@ class RedisQueueIntegrationTest extends PHPUnit_Framework_TestCase
public function setUp()
{
parent::setUp();

$host = getenv('REDIS_HOST') ?: '127.0.0.1';
$port = getenv('REDIS_PORT') ?: 6379;

if (static::$connectionFailedOnceWithDefaultsSkip) {
$this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__);

return;
}

$this->redis = new PredisDatabase([
'cluster' => false,
'default' => [
'host' => $host,
'port' => $port,
'database' => 5,
'timeout' => 0.5,
],
]);

try {
$this->redis->connection()->flushdb();
} catch (\Exception $e) {
if ($host === '127.0.0.1' && $port === 6379 && getenv('REDIS_HOST') === false) {
$this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__);
static::$connectionFailedOnceWithDefaultsSkip = true;

return;
}
}
$this->setUpRedis();

$this->queue = new RedisQueue($this->redis);
$this->queue->setContainer(m::mock(Container::class));
Expand All @@ -64,10 +26,8 @@ public function setUp()
public function tearDown()
{
parent::tearDown();
$this->tearDownRedis();
m::close();
if ($this->redis) {
$this->redis->connection()->flushdb();
}
}

public function testExpiredJobsArePopped()
Expand Down
57 changes: 57 additions & 0 deletions tests/Redis/InteractsWithRedis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php


use Illuminate\Redis\PredisDatabase;

trait InteractsWithRedis
{
/**
* @var bool
*/
private static $connectionFailedOnceWithDefaultsSkip = false;

/**
* @var PredisDatabase
*/
private $redis;

public function setUpRedis()
{
$host = getenv('REDIS_HOST') ?: '127.0.0.1';
$port = getenv('REDIS_PORT') ?: 6379;

if (static::$connectionFailedOnceWithDefaultsSkip) {
$this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__);

return;
}

$this->redis = new PredisDatabase([
'cluster' => false,
'default' => [
'host' => $host,
'port' => $port,
'database' => 5,
'timeout' => 0.5,
],
]);

try {
$this->redis->connection()->flushdb();
} catch (\Exception $e) {
if ($host === '127.0.0.1' && $port === 6379 && getenv('REDIS_HOST') === false) {
$this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__);
static::$connectionFailedOnceWithDefaultsSkip = true;

return;
}
}
}

public function tearDownRedis()
{
if ($this->redis) {
$this->redis->connection()->flushdb();
}
}
}

0 comments on commit 5ab5179

Please sign in to comment.