18
18
use Hyperf \Redis \RedisFactory ;
19
19
use Hyperf \Redis \RedisProxy ;
20
20
use Mockery ;
21
+ use PHPUnit \Framework \Attributes \CoversNothing ;
21
22
use PHPUnit \Framework \TestCase ;
22
23
use Psr \Container \ContainerInterface ;
23
24
use ReflectionProperty ;
26
27
* @internal
27
28
* @coversNothing
28
29
*/
30
+ #[CoversNothing]
29
31
class RedisStorageFactoryTest extends TestCase
30
32
{
31
33
protected string $ prePrefix ;
@@ -37,10 +39,7 @@ protected function setUp(): void
37
39
parent ::setUp ();
38
40
39
41
$ prefixProperty = new ReflectionProperty (Redis::class, 'prefix ' );
40
- $ prefixProperty ->setAccessible (true );
41
-
42
42
$ metricGatherKeySuffix = new ReflectionProperty (Redis::class, 'metricGatherKeySuffix ' );
43
- $ metricGatherKeySuffix ->setAccessible (true );
44
43
45
44
$ this ->prePrefix = $ prefixProperty ->getDefaultValue ();
46
45
$ this ->preMetricGatherKeySuffix = $ metricGatherKeySuffix ->getDefaultValue ();
@@ -69,14 +68,12 @@ public function testEmptyMetricRedisConfig()
69
68
$ redis = $ factory ($ container );
70
69
71
70
$ prefixProperty = new ReflectionProperty (Redis::class, 'prefix ' );
72
- $ prefixProperty ->setAccessible (true );
73
71
74
72
$ metricGatherKeySuffixProperty = new ReflectionProperty (Redis::class, 'metricGatherKeySuffix ' );
75
- $ metricGatherKeySuffixProperty ->setAccessible (true );
76
73
77
74
self ::assertInstanceOf (Redis::class, $ redis );
78
75
self ::assertEquals ('skeleton ' , $ prefixProperty ->getValue ($ redis ));
79
- self ::assertEquals ('_METRIC_KEYS ' , $ metricGatherKeySuffixProperty ->getValue ($ redis ));
76
+ self ::assertEquals (':metric_keys ' , $ metricGatherKeySuffixProperty ->getValue ($ redis ));
80
77
}
81
78
82
79
public function testNewConfig ()
@@ -102,13 +99,42 @@ public function testNewConfig()
102
99
$ redis = $ factory ($ container );
103
100
104
101
$ prefixProperty = new ReflectionProperty (Redis::class, 'prefix ' );
105
- $ prefixProperty ->setAccessible (true );
106
102
107
103
$ metricGatherKeySuffixProperty = new ReflectionProperty (Redis::class, 'metricGatherKeySuffix ' );
108
- $ metricGatherKeySuffixProperty ->setAccessible (true );
109
104
110
105
self ::assertInstanceOf (Redis::class, $ redis );
111
106
self ::assertEquals ('prometheus: ' , $ prefixProperty ->getValue ($ redis ));
112
107
self ::assertEquals (':metric_keys ' , $ metricGatherKeySuffixProperty ->getValue ($ redis ));
113
108
}
109
+
110
+ public function testCustomConfig ()
111
+ {
112
+ $ redisFactory = Mockery::mock (RedisFactory::class);
113
+ $ redisFactory ->shouldReceive ('get ' )->with ('custom ' )->andReturn (Mockery::mock (RedisProxy::class));
114
+
115
+ $ container = Mockery::mock (ContainerInterface::class);
116
+ $ container ->shouldReceive ('get ' )->with (ConfigInterface::class)->andReturn (new Config ([
117
+ 'metric ' => [
118
+ 'metric ' => [
119
+ 'prometheus ' => [
120
+ 'redis_config ' => 'custom ' ,
121
+ 'redis_prefix ' => 'custom: ' ,
122
+ 'redis_gather_key_suffix ' => ':custom ' ,
123
+ ],
124
+ ],
125
+ ],
126
+ ]));
127
+ $ container ->shouldReceive ('get ' )->with (RedisFactory::class)->andReturn ($ redisFactory );
128
+
129
+ $ factory = new RedisStorageFactory ();
130
+ $ redis = $ factory ($ container );
131
+
132
+ $ prefixProperty = new ReflectionProperty (Redis::class, 'prefix ' );
133
+
134
+ $ metricGatherKeySuffixProperty = new ReflectionProperty (Redis::class, 'metricGatherKeySuffix ' );
135
+
136
+ self ::assertInstanceOf (Redis::class, $ redis );
137
+ self ::assertEquals ('custom: ' , $ prefixProperty ->getValue ($ redis ));
138
+ self ::assertEquals (':custom ' , $ metricGatherKeySuffixProperty ->getValue ($ redis ));
139
+ }
114
140
}
0 commit comments