1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace Codeception \Module ;
4
6
5
7
use Codeception \Lib \Interfaces \RequiresPackage ;
6
- use Codeception \Module as CodeceptionModule ;
8
+ use Codeception \Module ;
7
9
use Codeception \Exception \ModuleException ;
8
10
use Codeception \TestInterface ;
11
+ use Exception ;
9
12
use Predis \Client as RedisDriver ;
10
13
11
14
/**
47
50
*
48
51
* @author Marc Verney <[email protected] >
49
52
*/
50
- class Redis extends CodeceptionModule implements RequiresPackage
53
+ class Redis extends Module implements RequiresPackage
51
54
{
52
55
/**
53
56
* {@inheritdoc}
54
57
*
55
58
* No default value is set for the database, using this parameter.
59
+ *
60
+ * @var array
56
61
*/
57
62
protected $ config = [
58
63
'host ' => '127.0.0.1 ' ,
@@ -62,6 +67,8 @@ class Redis extends CodeceptionModule implements RequiresPackage
62
67
63
68
/**
64
69
* {@inheritdoc}
70
+ *
71
+ * @var string[]
65
72
*/
66
73
protected $ requiredFields = [
67
74
'database '
@@ -76,7 +83,7 @@ class Redis extends CodeceptionModule implements RequiresPackage
76
83
77
84
public function _requires ()
78
85
{
79
- return [' Predis\Client ' => '"predis/predis": "^1.0" ' ];
86
+ return [\ Predis \Client::class => '"predis/predis": "^1.0" ' ];
80
87
}
81
88
82
89
/**
@@ -88,7 +95,7 @@ public function _initialize()
88
95
{
89
96
try {
90
97
$ this ->driver = new RedisDriver ($ this ->config );
91
- } catch (\ Exception $ e ) {
98
+ } catch (Exception $ e ) {
92
99
throw new ModuleException (
93
100
__CLASS__ ,
94
101
$ e ->getMessage ()
@@ -110,8 +117,6 @@ public function _beforeSuite($settings = [])
110
117
111
118
/**
112
119
* Code to run before each test
113
- *
114
- * @param TestInterface $test
115
120
*/
116
121
public function _before (TestInterface $ test )
117
122
{
@@ -125,11 +130,11 @@ public function _before(TestInterface $test)
125
130
*
126
131
* @throws ModuleException
127
132
*/
128
- public function cleanup ()
133
+ public function cleanup (): void
129
134
{
130
135
try {
131
136
$ this ->driver ->flushdb ();
132
- } catch (\ Exception $ e ) {
137
+ } catch (Exception $ e ) {
133
138
throw new ModuleException (
134
139
__CLASS__ ,
135
140
$ e ->getMessage ()
@@ -174,21 +179,20 @@ public function cleanup()
174
179
*
175
180
* @param string $key The key name
176
181
*
177
- * @return mixed
182
+ * @return array|string|null
178
183
*
179
184
* @throws ModuleException if the key does not exist
180
185
*/
181
- public function grabFromRedis ($ key )
186
+ public function grabFromRedis (string $ key )
182
187
{
183
188
$ args = func_get_args ();
184
189
185
190
switch ($ this ->driver ->type ($ key )) {
186
191
case 'none ' :
187
192
throw new ModuleException (
188
193
$ this ,
189
- " Cannot grab key \" $ key \ " as it does not exist "
194
+ sprintf ( ' Cannot grab key "%s " as it does not exist ' , $ key )
190
195
);
191
- break ;
192
196
193
197
case 'string ' :
194
198
$ reply = $ this ->driver ->get ($ key );
@@ -214,8 +218,7 @@ public function grabFromRedis($key)
214
218
if (count ($ args ) === 2 ) {
215
219
throw new ModuleException (
216
220
$ this ,
217
- "The method grabFromRedis(), when used with sorted "
218
- . "sets, expects either one argument or three "
221
+ 'The method grabFromRedis(), when used with sorted sets, expects either one argument or three '
219
222
);
220
223
}
221
224
$ reply = $ this ->driver ->zrange (
@@ -273,15 +276,14 @@ public function grabFromRedis($key)
273
276
*
274
277
* @throws ModuleException
275
278
*/
276
- public function haveInRedis ($ type , $ key , $ value )
279
+ public function haveInRedis (string $ type , string $ key , $ value ): void
277
280
{
278
281
switch (strtolower ($ type )) {
279
282
case 'string ' :
280
283
if (!is_scalar ($ value )) {
281
284
throw new ModuleException (
282
285
$ this ,
283
- 'If second argument of haveInRedis() method is "string", '
284
- . 'third argument must be a scalar '
286
+ 'If second argument of haveInRedis() method is "string", third argument must be a scalar '
285
287
);
286
288
}
287
289
$ this ->driver ->set ($ key , $ value );
@@ -299,8 +301,7 @@ public function haveInRedis($type, $key, $value)
299
301
if (!is_array ($ value )) {
300
302
throw new ModuleException (
301
303
$ this ,
302
- 'If second argument of haveInRedis() method is "zset", '
303
- . 'third argument must be an (associative) array '
304
+ 'If second argument of haveInRedis() method is "zset", third argument must be an (associative) array '
304
305
);
305
306
}
306
307
$ this ->driver ->zadd ($ key , $ value );
@@ -310,8 +311,7 @@ public function haveInRedis($type, $key, $value)
310
311
if (!is_array ($ value )) {
311
312
throw new ModuleException (
312
313
$ this ,
313
- 'If second argument of haveInRedis() method is "hash", '
314
- . 'third argument must be an array '
314
+ 'If second argument of haveInRedis() method is "hash", third argument must be an array '
315
315
);
316
316
}
317
317
$ this ->driver ->hmset ($ key , $ value );
@@ -320,7 +320,7 @@ public function haveInRedis($type, $key, $value)
320
320
default :
321
321
throw new ModuleException (
322
322
$ this ,
323
- " Unknown type \" $ type \ " for key \" $ key \ ". Allowed types are "
323
+ sprintf ( ' Unknown type "%s " for key "%s ". Allowed types are ' , $ type , $ key )
324
324
. '"string", "list", "set", "zset", "hash" '
325
325
);
326
326
}
@@ -357,11 +357,11 @@ public function haveInRedis($type, $key, $value)
357
357
* @param mixed $value Optional. If specified, also checks the key has this
358
358
* value. Booleans will be converted to 1 and 0 (even inside arrays)
359
359
*/
360
- public function dontSeeInRedis ($ key , $ value = null )
360
+ public function dontSeeInRedis (string $ key , $ value = null ): void
361
361
{
362
362
$ this ->assertFalse (
363
- ( bool ) $ this ->checkKeyExists ($ key , $ value ),
364
- " The key \" $ key \ " exists " . ($ value ? ' and its value matches the one provided ' : '' )
363
+ $ this ->checkKeyExists ($ key , $ value ),
364
+ sprintf ( ' The key "%s " exists ' , $ key ) . ($ value ? ' and its value matches the one provided ' : '' )
365
365
);
366
366
}
367
367
@@ -398,17 +398,15 @@ public function dontSeeInRedis($key, $value = null)
398
398
* @param mixed $item The item
399
399
* @param null $itemValue Optional and only used for zsets and hashes. If
400
400
* specified, the method will also check that the $item has this value/score
401
- *
402
- * @return bool
403
401
*/
404
- public function dontSeeRedisKeyContains ($ key , $ item , $ itemValue = null )
402
+ public function dontSeeRedisKeyContains (string $ key , $ item , $ itemValue = null ): void
405
403
{
406
404
$ this ->assertFalse (
407
- ( bool ) $ this ->checkKeyContains ($ key , $ item , $ itemValue ),
408
- " The key \" $ key \ " contains " . (
405
+ $ this ->checkKeyContains ($ key , $ item , $ itemValue ),
406
+ sprintf ( ' The key "%s " contains ' , $ key ) . (
409
407
is_null ($ itemValue )
410
- ? "\" $ item\""
411
- : " [ \" $ item \ " => \" $ itemValue \" ] "
408
+ ? sprintf ( ' "%s" ' , $ item)
409
+ : sprintf ( ' ["%s " => "%s"] ' , $ item , $ itemValue )
412
410
)
413
411
);
414
412
}
@@ -443,11 +441,11 @@ public function dontSeeRedisKeyContains($key, $item, $itemValue = null)
443
441
* @param mixed $value Optional. If specified, also checks the key has this
444
442
* value. Booleans will be converted to 1 and 0 (even inside arrays)
445
443
*/
446
- public function seeInRedis ($ key , $ value = null )
444
+ public function seeInRedis (string $ key , $ value = null ): void
447
445
{
448
446
$ this ->assertTrue (
449
- ( bool ) $ this ->checkKeyExists ($ key , $ value ),
450
- " Cannot find key \" $ key\"" . ($ value ? ' with the provided value ' : '' )
447
+ $ this ->checkKeyExists ($ key , $ value ),
448
+ sprintf ( ' Cannot find key "%s" ' , $ key) . ($ value ? ' with the provided value ' : '' )
451
449
);
452
450
}
453
451
@@ -471,7 +469,7 @@ public function seeInRedis($key, $value = null)
471
469
*
472
470
* @return mixed
473
471
*/
474
- public function sendCommandToRedis ($ command )
472
+ public function sendCommandToRedis (string $ command )
475
473
{
476
474
return call_user_func_array (
477
475
[$ this ->driver , $ command ],
@@ -512,17 +510,15 @@ public function sendCommandToRedis($command)
512
510
* @param mixed $item The item
513
511
* @param null $itemValue Optional and only used for zsets and hashes. If
514
512
* specified, the method will also check that the $item has this value/score
515
- *
516
- * @return bool
517
513
*/
518
- public function seeRedisKeyContains ($ key , $ item , $ itemValue = null )
514
+ public function seeRedisKeyContains (string $ key , $ item , $ itemValue = null ): void
519
515
{
520
516
$ this ->assertTrue (
521
- ( bool ) $ this ->checkKeyContains ($ key , $ item , $ itemValue ),
522
- " The key \" $ key \ " does not contain " . (
517
+ $ this ->checkKeyContains ($ key , $ item , $ itemValue ),
518
+ sprintf ( ' The key "%s " does not contain ' , $ key ) . (
523
519
is_null ($ itemValue )
524
- ? "\" $ item\""
525
- : " [ \" $ item \ " => \" $ itemValue \" ] "
520
+ ? sprintf ( ' "%s" ' , $ item)
521
+ : sprintf ( ' ["%s " => "%s"] ' , $ item , $ itemValue )
526
522
)
527
523
);
528
524
}
@@ -556,10 +552,9 @@ private function boolToString($var)
556
552
* specified, the method will also check that the $item has this value/score
557
553
*
558
554
* @return bool
559
- *
560
555
* @throws ModuleException
561
556
*/
562
- private function checkKeyContains ($ key , $ item , $ itemValue = null )
557
+ private function checkKeyContains (string $ key , $ item , $ itemValue = null ): bool
563
558
{
564
559
$ result = null ;
565
560
@@ -608,12 +603,11 @@ private function checkKeyContains($key, $item, $itemValue = null)
608
603
case 'none ' :
609
604
throw new ModuleException (
610
605
$ this ,
611
- " Key \" $ key \ " does not exist "
606
+ sprintf ( ' Key "%s " does not exist ' , $ key )
612
607
);
613
- break ;
614
608
}
615
609
616
- return $ result ;
610
+ return ( bool ) $ result ;
617
611
}
618
612
619
613
/**
@@ -622,10 +616,8 @@ private function checkKeyContains($key, $item, $itemValue = null)
622
616
* @param string $key The key name
623
617
* @param mixed $value Optional. If specified, also checks the key has this
624
618
* value. Booleans will be converted to 1 and 0 (even inside arrays)
625
- *
626
- * @return bool
627
619
*/
628
- private function checkKeyExists ($ key , $ value = null )
620
+ private function checkKeyExists (string $ key , $ value = null ): bool
629
621
{
630
622
$ type = $ this ->driver ->type ($ key );
631
623
@@ -684,7 +676,7 @@ private function checkKeyExists($key, $value = null)
684
676
*
685
677
* @return array
686
678
*/
687
- private function scoresToFloat (array $ arr )
679
+ private function scoresToFloat (array $ arr ): array
688
680
{
689
681
foreach ($ arr as $ member => $ score ) {
690
682
$ arr [$ member ] = (float ) $ score ;
0 commit comments