Skip to content

Commit fcc088d

Browse files
committed
Throw exceptions when "connect" fails
1 parent f8d7241 commit fcc088d

File tree

2 files changed

+116
-33
lines changed

2 files changed

+116
-33
lines changed

Model/Datasource/RedisSource.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,33 @@ public function enabled() {
123123
/**
124124
* Connects to the database using options in the given configuration array.
125125
*
126-
* "Connects mean:
126+
* "Connects" means:
127127
* - connect
128128
* - authenticate
129129
* - select
130130
* - setPrefix
131131
*
132-
* @return bool
132+
* @return bool True if all of the above steps succeed, else false
133+
* @throws RedisSourceException
133134
*/
134135
public function connect() {
135-
$this->connected = $this->_connect();
136-
$this->connected = $this->connected && $this->_authenticate();
137-
$this->connected = $this->connected && $this->_select();
138-
$this->connected = $this->connected && $this->_setPrefix();
136+
if (!$this->_connect()) {
137+
throw new RedisSourceException(__d('redis', 'Could not connect.'));
138+
}
139+
140+
if (!$this->_authenticate()) {
141+
throw new RedisSourceException(__d('redis', 'Could not authenticate.'));
142+
}
143+
144+
if (!$this->_select()) {
145+
throw new RedisSourceException(__d('redis', 'Could not select.'));
146+
}
147+
148+
if (!$this->_setPrefix()) {
149+
throw new RedisSourceException(__d('redis', 'Could not set prefix.'));
150+
}
151+
152+
$this->connected = true;
139153

140154
return $this->connected;
141155
}

Test/Case/Model/Datasource/RedisSourceTest.php

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function tearDown() {
9292
* Tests that `connect` will never be called when redis extension is not loaded.
9393
*
9494
* @expectedException RedisSourceException
95+
* @expectedExceptionMessage Extension is not loaded.
9596
* @return void
9697
*/
9798
public function testConstructExtensionNotLoaded() {
@@ -134,6 +135,100 @@ public function testConstructExtensionLoaded() {
134135
$this->assertInstanceOf($expected, $result);
135136
}
136137

138+
/**
139+
* testConnectFailedConnect method
140+
*
141+
* Tests that an exception in thrown when `_connect` fails.
142+
*
143+
* @expectedException RedisSourceException
144+
* @expectedExceptionMessage Could not connect.
145+
* @return void
146+
*/
147+
public function testConnectFailedConnect() {
148+
// Get mock, without the constructor being called
149+
$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
150+
151+
// Set expectations for connect calls
152+
$Source->expects($this->once())->method('_connect')->will($this->returnValue(false));
153+
154+
// Now call connect
155+
$reflectedClass = new ReflectionClass('TestRedisSource');
156+
$connect = $reflectedClass->getMethod('connect');
157+
$connect->invoke($Source);
158+
}
159+
160+
/**
161+
* testConnectFailedAuthenticate method
162+
*
163+
* Tests that an exception in thrown when `_authenticate` fails.
164+
*
165+
* @expectedException RedisSourceException
166+
* @expectedExceptionMessage Could not authenticate.
167+
* @return void
168+
*/
169+
public function testConnectFailedAuthenticate() {
170+
// Get mock, without the constructor being called
171+
$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
172+
173+
// Set expectations for connect calls
174+
$Source->expects($this->once())->method('_connect')->will($this->returnValue(true));
175+
$Source->expects($this->once())->method('_authenticate')->will($this->returnValue(false));
176+
177+
// Now call connect
178+
$reflectedClass = new ReflectionClass('TestRedisSource');
179+
$connect = $reflectedClass->getMethod('connect');
180+
$connect->invoke($Source);
181+
}
182+
183+
/**
184+
* testConnectFailedSelect method
185+
*
186+
* Tests that an exception in thrown when `_select` fails.
187+
*
188+
* @expectedException RedisSourceException
189+
* @expectedExceptionMessage Could not select.
190+
* @return void
191+
*/
192+
public function testConnectFailedSelect() {
193+
// Get mock, without the constructor being called
194+
$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
195+
196+
// Set expectations for connect calls
197+
$Source->expects($this->once())->method('_connect')->will($this->returnValue(true));
198+
$Source->expects($this->once())->method('_authenticate')->will($this->returnValue(true));
199+
$Source->expects($this->once())->method('_select')->will($this->returnValue(false));
200+
201+
// Now call connect
202+
$reflectedClass = new ReflectionClass('TestRedisSource');
203+
$connect = $reflectedClass->getMethod('connect');
204+
$connect->invoke($Source);
205+
}
206+
207+
/**
208+
* testConnectFailedSetPrefix method
209+
*
210+
* Tests that an exception in thrown when `_setPrefix` fails.
211+
*
212+
* @expectedException RedisSourceException
213+
* @expectedExceptionMessage Could not set prefix.
214+
* @return void
215+
*/
216+
public function testConnectFailedSetPrefix() {
217+
// Get mock, without the constructor being called
218+
$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
219+
220+
// Set expectations for connect calls
221+
$Source->expects($this->once())->method('_connect')->will($this->returnValue(true));
222+
$Source->expects($this->once())->method('_authenticate')->will($this->returnValue(true));
223+
$Source->expects($this->once())->method('_select')->will($this->returnValue(true));
224+
$Source->expects($this->once())->method('_setPrefix')->will($this->returnValue(false));
225+
226+
// Now call connect
227+
$reflectedClass = new ReflectionClass('TestRedisSource');
228+
$connect = $reflectedClass->getMethod('connect');
229+
$connect->invoke($Source);
230+
}
231+
137232
/**
138233
* testConnected method
139234
*
@@ -276,40 +371,14 @@ public function testCallExisting() {
276371
*
277372
* @return void
278373
* @expectedException RedisSourceException
374+
* @expectedExceptionMessage Method (pang) does not exist.
279375
*/
280376
public function testCallNonExisting() {
281377
$Source = new TestRedisSource();
282378

283379
$Source->pang();
284380
}
285381

286-
/**
287-
* testCallExistingFailure method
288-
*
289-
* Tests calling of an existing (Redis) method on a disconnected / misconfigured instance.
290-
*
291-
* @return void
292-
* @expectedException RedisSourceException
293-
*/
294-
public function testCallExistingFailure() {
295-
$unixSocket = '';
296-
$persistent = false;
297-
$host = '127.0.0.1';
298-
$port = 63790;
299-
$timeout = 0;
300-
301-
$config = array(
302-
'unix_socket' => $unixSocket,
303-
'persistent' => $persistent,
304-
'host' => $host,
305-
'port' => $port,
306-
'timeout' => $timeout,
307-
);
308-
309-
$Source = new TestRedisSource($config);
310-
$Source->ping();
311-
}
312-
313382
/**
314383
* testNoAuthenticate method
315384
*

0 commit comments

Comments
 (0)