RedisSourceTest::testConnectTcp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 30
rs 9.6
1
<?php
2
App::uses('RedisSource', 'Redis.Model/Datasource');
3
4
/**
5
 * Test Redis Source class
6
 *
7
 */
8
class TestRedisSource extends RedisSource {
9
10
/**
11
 * Test double of `parent::_connection`.
12
 *
13
 * @var Redis
14
 */
15
	// @codingStandardsIgnoreStart
16
	public $_connection = null;
17
	// @codingStandardsIgnoreEnd
18
19
/**
20
 * Test double of `parent::_connect`.
21
 *
22
 * @return bool
23
 */
24
	// @codingStandardsIgnoreStart
25
	public function _connect() {
26
	// @codingStandardsIgnoreEnd
27
		return parent::_connect();
28
	}
29
30
/**
31
 * Test double of `parent::_authenticate`.
32
 *
33
 * @return bool
34
 */
35
	// @codingStandardsIgnoreStart
36
	public function _authenticate() {
37
	// @codingStandardsIgnoreEnd
38
		return parent::_authenticate();
39
	}
40
41
/**
42
 * Test double of `parent::_select`.
43
 *
44
 * @return bool
45
 */
46
	// @codingStandardsIgnoreStart
47
	public function _select() {
48
	// @codingStandardsIgnoreEnd
49
		return parent::_select();
50
	}
51
52
/**
53
 * Test double of `parent::_setPrefix`.
54
 *
55
 * @return bool
56
 */
57
	// @codingStandardsIgnoreStart
58
	public function _setPrefix() {
59
	// @codingStandardsIgnoreEnd
60
		return parent::_setPrefix();
61
	}
62
63
}
64
65
/**
66
 * Redis Source Test class
67
 *
68
 */
69
class RedisSourceTest extends CakeTestCase {
0 ignored issues
show
Bug introduced by
The type CakeTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
71
/**
72
 * setUp method
73
 *
74
 * @return void
75
 */
76
	public function setUp() {
77
		parent::setUp();
78
	}
79
80
/**
81
 * tearDown method
82
 *
83
 * @return void
84
 */
85
	public function tearDown() {
86
		parent::tearDown();
87
	}
88
89
/**
90
 * testConstructExtensionNotLoaded method
91
 *
92
 *  Tests that `connect` will never be called when redis extension is not loaded.
93
 *
94
 * @expectedException RedisSourceException
95
 * @expectedExceptionMessage Extension is not loaded.
96
 * @return void
97
 */
98
	public function testConstructExtensionNotLoaded() {
99
		// Get mock, without the constructor being called
100
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
101
102
		// Set expectations for constructor calls
103
		$Source->expects($this->once())->method('enabled')->will($this->returnValue(false));
104
		$Source->expects($this->never())->method('connect');
105
106
		// Now call the constructor
107
		$reflectedClass = new ReflectionClass('TestRedisSource');
108
		$constructor = $reflectedClass->getConstructor();
109
		$constructor->invoke($Source);
110
	}
111
112
/**
113
 * testConstructExtensionLoaded method
114
 *
115
 *  Tests that `connect` will be called when redis extension is loaded.
116
 *
117
 * @return void
118
 */
119
	public function testConstructExtensionLoaded() {
120
		// Get mock, without the constructor being called
121
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
122
123
		// Set expectations for constructor calls
124
		$Source->expects($this->once())->method('enabled')->will($this->returnValue(true));
125
		$Source->expects($this->once())->method('connect');
126
127
		// Now call the constructor
128
		$reflectedClass = new ReflectionClass('TestRedisSource');
129
		$constructor = $reflectedClass->getConstructor();
130
		$constructor->invoke($Source);
131
132
		$expected = 'Redis';
133
		$result = $Source->_connection;
134
135
		$this->assertInstanceOf($expected, $result);
136
	}
137
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
232
/**
233
 * testConnected method
234
 *
235
 * @return void
236
 */
237
	public function testIsConnected() {
238
		$Source = new TestRedisSource();
239
240
		$Source->connected = true;
241
		$result = $Source->isConnected();
242
243
		$this->assertTrue($result);
244
245
		$Source->connected = false;
246
		$result = $Source->isConnected();
247
248
		$this->assertFalse($result);
249
	}
250
251
/**
252
 * testConnectUnixSocket method
253
 *
254
 * @return void
255
 */
256
	public function testConnectUnixSocket() {
257
		// Get mock, without the constructor being called
258
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
259
260
		$unixSocket = '/foo/bar';
261
		$persistent = false;
262
		$host = 'foo';
263
		$port = 'bar';
264
		$timeout = 0;
265
266
		$Source->config = [
267
			'unix_socket' => $unixSocket,
268
			'persistent' => $persistent,
269
			'host' => $host,
270
			'port' => $port,
271
			'timeout' => $timeout,
272
		];
273
		$Source->_connection = $this->getMock('Redis', ['connect']);
274
275
		// Set expectations for connect calls
276
		$Source->_connection->expects($this->once())->method('connect')
277
			->with($this->equalTo($unixSocket))->will($this->returnValue(true));
278
279
		// Now call _connect
280
		$reflectedClass = new ReflectionClass('TestRedisSource');
281
		$connect = $reflectedClass->getMethod('_connect');
282
		$result = $connect->invoke($Source);
283
284
		$this->assertTrue($result);
285
	}
286
287
/**
288
 * testConnectTcp method
289
 *
290
 * @return void
291
 */
292
	public function testConnectTcp() {
293
		// Get mock, without the constructor being called
294
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
295
296
		$unixSocket = '';
297
		$persistent = false;
298
		$host = 'foo';
299
		$port = 'bar';
300
		$timeout = 0;
301
302
		$Source->config = [
303
			'unix_socket' => $unixSocket,
304
			'persistent' => $persistent,
305
			'host' => $host,
306
			'port' => $port,
307
			'timeout' => $timeout,
308
		];
309
		$Source->_connection = $this->getMock('Redis', ['connect']);
310
311
		// Set expectations for connect calls
312
		$Source->_connection->expects($this->once())->method('connect')
313
			->with($this->equalTo($host), $this->equalTo($port), $this->equalTo($timeout))
314
			->will($this->returnValue(true));
315
316
		// Now call _connect
317
		$reflectedClass = new ReflectionClass('TestRedisSource');
318
		$connect = $reflectedClass->getMethod('_connect');
319
		$result = $connect->invoke($Source);
320
321
		$this->assertTrue($result);
322
	}
323
324
/**
325
 * testConnectTcpPersistent method
326
 *
327
 * @return void
328
 */
329
	public function testConnectTcpPersistent() {
330
		// Get mock, without the constructor being called
331
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
332
333
		$unixSocket = '';
334
		$persistent = true;
335
		$host = 'foo';
336
		$port = 'bar';
337
		$timeout = 0;
338
339
		$Source->config = [
340
			'unix_socket' => $unixSocket,
341
			'persistent' => $persistent,
342
			'host' => $host,
343
			'port' => $port,
344
			'timeout' => $timeout,
345
		];
346
		$Source->_connection = $this->getMock('Redis', ['pconnect']);
347
348
		// Set expectations for pconnect calls
349
		$Source->_connection->expects($this->once())->method('pconnect')
350
			->with($this->equalTo($host), $this->equalTo($port), $this->equalTo($timeout), $this->anything())
351
			->will($this->returnValue(true));
352
353
		// Now call _connect
354
		$reflectedClass = new ReflectionClass('TestRedisSource');
355
		$connect = $reflectedClass->getMethod('_connect');
356
		$result = $connect->invoke($Source);
357
358
		$this->assertTrue($result);
359
	}
360
361
/**
362
 * testCallExisting method
363
 *
364
 *  Tests calling of an existing (Redis) method on a connected / configured instance.
365
 *
366
 * @return void
367
 */
368
	public function testCallExisting() {
369
		$Source = new TestRedisSource();
370
371
		$result = $Source->ping();
372
		$expected = '+PONG';
373
374
		$this->assertIdentical($result, $expected);
375
	}
376
377
/**
378
 * testCallNonExisting method
379
 *
380
 *  Tests calling of an non-existing (Redis) method on a connected / configured instance.
381
 *
382
 * @return void
383
 * @expectedException RedisSourceException
384
 * @expectedExceptionMessage Method (pang) does not exist.
385
 */
386
	public function testCallNonExisting() {
387
		$Source = new TestRedisSource();
388
389
		$Source->pang();
390
	}
391
392
/**
393
 * testCallExistingFailure method
394
 *
395
 *  Tests calling of an existing (Redis) method on a disconnected / misconfigured instance.
396
 *
397
 * @return void
398
 * @expectedException RedisSourceException
399
 * @expectedExceptionMessage Method (ping) failed: Redis server went away.
400
 */
401
	public function testCallExistingFailure() {
402
		// Get mock, without the constructor being called
403
		$Source = new TestRedisSource();
404
		$Source->_connection = new Redis();
405
		$Source->connected = false;
406
407
		// Now call ping
408
		$Source->ping();
409
	}
410
411
/**
412
 * testNoAuthenticate method
413
 *
414
 * @return void
415
 */
416
	public function testNoAuthenticate() {
417
		// Get mock, without the constructor being called
418
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
419
420
		$password = '';
421
422
		$Source->config = ['password' => $password];
423
		$Source->_connection = $this->getMock('Redis', ['auth']);
424
425
		// Set expectations for constructor calls
426
		$Source->_connection->expects($this->never())->method('auth');
427
428
		// Now call _authenticate
429
		$reflectedClass = new ReflectionClass('TestRedisSource');
430
		$connect = $reflectedClass->getMethod('_authenticate');
431
		$result = $connect->invoke($Source);
432
433
		$this->assertTrue($result);
434
	}
435
436
/**
437
 * testSuccessfulAuthenticate method
438
 *
439
 * @return void
440
 */
441
	public function testSuccessfulAuthenticate() {
442
		// Get mock, without the constructor being called
443
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
444
445
		$password = 'foo';
446
447
		$Source->config = ['password' => $password];
448
		$Source->_connection = $this->getMock('Redis', ['auth']);
449
450
		// Set expectations for auth calls
451
		$Source->_connection->expects($this->once())->method('auth')
452
			->with($this->equalTo($password))->will($this->returnValue(true));
453
454
		// Now call _authenticate
455
		$reflectedClass = new ReflectionClass('TestRedisSource');
456
		$connect = $reflectedClass->getMethod('_authenticate');
457
		$result = $connect->invoke($Source);
458
459
		$this->assertTrue($result);
460
	}
461
462
/**
463
 * testFailingAuthenticate method
464
 *
465
 * @return void
466
 */
467
	public function testFailingAuthenticate() {
468
		// Get mock, without the constructor being called
469
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
470
471
		$password = 'foo';
472
473
		$Source->config = ['password' => $password];
474
		$Source->_connection = $this->getMock('Redis', ['auth']);
475
476
		// Set expectations for auth calls
477
		$Source->_connection->expects($this->once())->method('auth')
478
			->with($this->equalTo($password))->will($this->returnValue(false));
479
480
		// Now call _authenticate
481
		$reflectedClass = new ReflectionClass('TestRedisSource');
482
		$connect = $reflectedClass->getMethod('_authenticate');
483
		$result = $connect->invoke($Source);
484
485
		$this->assertFalse($result);
486
	}
487
488
/**
489
 * testNoSelect method
490
 *
491
 * @return void
492
 */
493
	public function testNoSelect() {
494
		// Get mock, without the constructor being called
495
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
496
497
		$database = '';
498
499
		$Source->config = ['database' => $database];
500
		$Source->_connection = $this->getMock('Redis', ['select']);
501
502
		// Set expectations for select calls
503
		$Source->_connection->expects($this->never())->method('select');
504
505
		// Now call _Select
506
		$reflectedClass = new ReflectionClass('TestRedisSource');
507
		$connect = $reflectedClass->getMethod('_select');
508
		$result = $connect->invoke($Source);
509
510
		$this->assertTrue($result);
511
	}
512
513
/**
514
 * testSuccessfulSelect method
515
 *
516
 * @return void
517
 */
518
	public function testSuccessfulSelect() {
519
		// Get mock, without the constructor being called
520
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
521
522
		$database = 'foo';
523
524
		$Source->config = ['database' => $database];
525
		$Source->_connection = $this->getMock('Redis', ['select']);
526
527
		// Set expectations for select calls
528
		$Source->_connection->expects($this->once())->method('select')
529
			->with($this->equalTo($database))->will($this->returnValue(true));
530
531
		// Now call _Select
532
		$reflectedClass = new ReflectionClass('TestRedisSource');
533
		$connect = $reflectedClass->getMethod('_select');
534
		$result = $connect->invoke($Source);
535
536
		$this->assertTrue($result);
537
	}
538
539
/**
540
 * testFailingSelect method
541
 *
542
 * @return void
543
 */
544
	public function testFailingSelect() {
545
		// Get mock, without the constructor being called
546
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
547
548
		$database = 'foo';
549
550
		$Source->config = ['database' => $database];
551
		$Source->_connection = $this->getMock('Redis', ['select']);
552
553
		// Set expectations for select calls
554
		$Source->_connection->expects($this->once())->method('select')
555
			->with($this->equalTo($database))->will($this->returnValue(false));
556
557
		// Now call _Select
558
		$reflectedClass = new ReflectionClass('TestRedisSource');
559
		$connect = $reflectedClass->getMethod('_select');
560
		$result = $connect->invoke($Source);
561
562
		$this->assertFalse($result);
563
	}
564
565
/**
566
 * testNoSelect method
567
 *
568
 * @return void
569
 */
570
	public function testNoSetPrefix() {
571
		// Get mock, without the constructor being called
572
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
573
574
		$prefix = '';
575
576
		$Source->config = ['prefix' => $prefix];
577
		$Source->_connection = $this->getMock('Redis', ['setOption']);
578
579
		// Set expectations for setOption calls
580
		$Source->_connection->expects($this->never())->method('setOption');
581
582
		// Now call _Select
583
		$reflectedClass = new ReflectionClass('TestRedisSource');
584
		$connect = $reflectedClass->getMethod('_setPrefix');
585
		$result = $connect->invoke($Source);
586
587
		$this->assertTrue($result);
588
	}
589
590
/**
591
 * testSuccessfulSelect method
592
 *
593
 * @return void
594
 */
595
	public function testSuccessfulSetPrefix() {
596
		// Get mock, without the constructor being called
597
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
598
599
		$prefix = 'foo';
600
601
		$Source->config = ['prefix' => $prefix];
602
		$Source->_connection = $this->getMock('Redis', ['setOption']);
603
604
		// Set expectations for setOption calls
605
		$Source->_connection->expects($this->once())->method('setOption')
606
			->with($this->equalTo(Redis::OPT_PREFIX), $this->equalTo($prefix))->will($this->returnValue(true));
607
608
		// Now call _Select
609
		$reflectedClass = new ReflectionClass('TestRedisSource');
610
		$connect = $reflectedClass->getMethod('_setPrefix');
611
		$result = $connect->invoke($Source);
612
613
		$this->assertTrue($result);
614
	}
615
616
/**
617
 * testFailingSelect method
618
 *
619
 * @return void
620
 */
621
	public function testFailingSetPrefix() {
622
		// Get mock, without the constructor being called
623
		$Source = $this->getMockBuilder('TestRedisSource')->disableOriginalConstructor()->getMock();
624
625
		$prefix = 'foo';
626
627
		$Source->config = ['prefix' => $prefix];
628
		$Source->_connection = $this->getMock('Redis', ['setOption']);
629
630
		// Set expectations for setOption calls
631
		$Source->_connection->expects($this->once())->method('setOption')
632
			->with($this->equalTo(Redis::OPT_PREFIX), $this->equalTo($prefix))->will($this->returnValue(false));
633
634
		// Now call _Select
635
		$reflectedClass = new ReflectionClass('TestRedisSource');
636
		$connect = $reflectedClass->getMethod('_setPrefix');
637
		$result = $connect->invoke($Source);
638
639
		$this->assertFalse($result);
640
	}
641
642
	public function testCloseNotConnected() {
643
		$Source = new TestRedisSource();
644
645
		$Source->connected = false;
646
		$Source->_connection = $this->getMock('Redis', ['close']);
647
648
		// Set expectations for close calls
649
		$Source->_connection->expects($this->never())->method('close');
650
651
		$result = $Source->close();
652
653
		$this->assertFalse($Source->connected);
654
		$this->assertNull($Source->_connection);
655
		$this->assertTrue($result);
656
	}
657
658
	public function testCloseConnected() {
659
		$Source = new TestRedisSource();
660
661
		$Source->connected = true;
662
		$Source->_connection = $this->getMock('Redis', ['close']);
663
664
		// Set expectations for close calls
665
		$Source->_connection->expects($this->once())->method('close');
666
667
		$result = $Source->close();
668
669
		$this->assertFalse($Source->connected);
670
		$this->assertNull($Source->_connection);
671
		$this->assertTrue($result);
672
	}
673
674
/**
675
 * testListSources method
676
 *
677
 * @return void
678
 */
679
	public function testListSources() {
680
		$Source = new TestRedisSource();
681
682
		$result = $Source->listSources();
683
684
		$this->assertNull($result);
685
	}
686
687
/**
688
 * testDescribe method
689
 *
690
 * @return void
691
 */
692
	public function testDescribe() {
693
		$Source = new TestRedisSource();
694
		$Model = $this->getMockForModel('Model');
695
696
		$result = $Source->describe($Model);
697
698
		$this->assertNull($result);
699
	}
700
701
}
702