Skip to content

Commit 8113e62

Browse files
committed
use a mock to test correct redis exceptions are surfaced
1 parent 599dc4c commit 8113e62

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

lib/Resque/Redis.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,17 @@ public static function prefix($namespace)
108108
* @param string|array $server A DSN or array
109109
* @param int $database A database number to select. However, if we find a valid database number in the DSN the
110110
* DSN-supplied value will be used instead and this parameter is ignored.
111+
* @param object $client Optional Credis_Cluster or Credis_Client instance instantiated by you
111112
*/
112-
public function __construct($server, $database = null)
113+
public function __construct($server, $database = null, $client = null)
113114
{
114115
try {
115116
if (is_array($server)) {
116117
$this->driver = new Credis_Cluster($server);
117118
}
119+
else if (is_object($client)) {
120+
$this->driver = $client;
121+
}
118122
else {
119123
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
120124
// $user is not used, only $password

test/Resque/Tests/JobTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ public function testJobCanBeQueued()
3131
*/
3232
public function testRedisErrorThrowsExceptionOnJobCreation()
3333
{
34-
Resque::setBackend('redis://255.255.255.255:1234');
34+
$mockCredis = $this->getMockBuilder('Credis_Client')
35+
->setMethods(['connect', '__call'])
36+
->getMock();
37+
$mockCredis->expects($this->any())->method('__call')
38+
->will($this->throwException(new CredisException('failure')));
39+
40+
Resque::setBackend(function($database) use ($mockCredis) {
41+
return new Resque_Redis('localhost:6379', $database, $mockCredis);
42+
});
3543
Resque::enqueue('jobs', 'This is a test');
3644
}
3745

test/Resque/Tests/RedisTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ class Resque_Tests_RedisTest extends Resque_Tests_TestCase
1313
*/
1414
public function testRedisExceptionsAreSurfaced()
1515
{
16-
$redis = new Resque_Redis('redis://255.255.255.255:1234');
17-
$redis->ping();
16+
$mockCredis = $this->getMockBuilder('Credis_Client')
17+
->setMethods(['connect', '__call'])
18+
->getMock();
19+
$mockCredis->expects($this->any())->method('__call')
20+
->will($this->throwException(new CredisException('failure')));
21+
22+
Resque::setBackend(function($database) use ($mockCredis) {
23+
return new Resque_Redis('localhost:6379', $database, $mockCredis);
24+
});
25+
Resque::redis()->ping();
1826
}
1927

2028
/**

0 commit comments

Comments
 (0)