-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that a redis connection is available before interacting with r…
- Loading branch information
1 parent
4683c79
commit 5ab5179
Showing
4 changed files
with
68 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |