4
4
5
5
namespace LaunchDarkly \Impl \Integrations \Tests \Impl \Integrations ;
6
6
7
- use Exception ;
8
7
use LaunchDarkly \Impl \Integrations \RedisBigSegmentsStore ;
9
8
use PHPUnit \Framework ;
10
- use Predis \ClientInterface ;
9
+ use Predis \Client ;
11
10
use Psr \Log ;
12
11
13
12
class RedisBigSegmentsStoreTest extends Framework \TestCase
@@ -17,32 +16,27 @@ public function testGetMetadata(): void
17
16
$ now = time ();
18
17
$ logger = new Log \NullLogger ();
19
18
20
- $ connection = $ this ->createMock (ClientInterface::class);
19
+ $ connection = new Client ();
20
+ $ connection ->flushAll ();
21
21
$ store = new RedisBigSegmentsStore ($ connection , $ logger , []);
22
22
23
- $ connection ->expects ($ this ->once ())
24
- ->method ('__call ' )
25
- ->with ('get ' , ['launchdarkly:big_segments_synchronized_on ' ])
26
- ->willReturn ("$ now " );
27
-
28
23
$ metadata = $ store ->getMetadata ();
24
+ $ this ->assertNull ($ metadata ->getLastUpToDate ());
25
+ $ this ->assertTrue ($ metadata ->isStale (10 ));
29
26
27
+ $ connection ->set ('launchdarkly:big_segments_synchronized_on ' , $ now );
28
+ $ metadata = $ store ->getMetadata ();
30
29
$ this ->assertEquals ($ now , $ metadata ->getLastUpToDate ());
31
30
$ this ->assertFalse ($ metadata ->isStale (10 ));
32
31
}
33
32
34
- public function testGetMetadataWithException (): void
33
+ public function testGetMetadataWithInvalidConfiguration (): void
35
34
{
36
35
$ logger = new Log \NullLogger ();
37
36
38
- $ connection = $ this -> createMock (ClientInterface::class );
37
+ $ connection = new Client ([ ' port ' => 33_333 ] );
39
38
$ store = new RedisBigSegmentsStore ($ connection , $ logger , []);
40
39
41
- $ connection ->expects ($ this ->once ())
42
- ->method ('__call ' )
43
- ->with ('get ' , ['launchdarkly:big_segments_synchronized_on ' ])
44
- ->willThrowException (new \Exception ('sorry ' ));
45
-
46
40
$ metadata = $ store ->getMetadata ();
47
41
48
42
$ this ->assertNull ($ metadata ->getLastUpToDate ());
@@ -53,52 +47,42 @@ public function testCanDetectInclusion(): void
53
47
{
54
48
$ logger = new Log \NullLogger ();
55
49
56
- $ connection = $ this ->createMock (ClientInterface::class);
57
- $ store = new RedisBigSegmentsStore ($ connection , $ logger , []);
58
-
59
- $ connection ->expects ($ this ->exactly (2 ))
60
- ->method ('__call ' )
61
- ->willReturnCallback (function ($ method , $ args ) {
62
- if ($ method !== 'smembers ' ) {
63
- return ;
64
- }
50
+ $ connection = new Client ();
51
+ $ connection ->flushAll ();
52
+ $ connection ->sAdd ('launchdarkly:big_segment_include:ctx ' , 'key1 ' , 'key2 ' );
53
+ $ connection ->sAdd ('launchdarkly:big_segment_exclude:ctx ' , 'key1 ' , 'key3 ' );
65
54
66
- return match ($ args [0 ]) {
67
- 'launchdarkly:big_segment_include:ctx ' => ['key1 ' , 'key2 ' ],
68
- 'launchdarkly:big_segment_exclude:ctx ' => ['key1 ' , 'key3 ' ],
69
- default => [],
70
- };
71
- });
55
+ $ store = new RedisBigSegmentsStore ($ connection , $ logger , []);
72
56
73
- $ membership = $ store ->getMembership ('ctx ' );
57
+ $ membership = $ store ->getMembership ('ctx ' ) ?? [] ;
74
58
75
59
$ this ->assertCount (3 , $ membership );
76
60
$ this ->assertTrue ($ membership ['key1 ' ]);
77
61
$ this ->assertTrue ($ membership ['key2 ' ]);
78
62
$ this ->assertFalse ($ membership ['key3 ' ]);
79
63
}
80
64
81
- public function testCanDetectInclusionWithException (): void
65
+ public function testCanDetectInclusionWithEmptyData (): void
82
66
{
83
67
$ logger = new Log \NullLogger ();
84
68
85
- $ connection = $ this ->createMock (ClientInterface::class);
69
+ $ connection = new Client ();
70
+ $ connection ->flushAll ();
71
+
86
72
$ store = new RedisBigSegmentsStore ($ connection , $ logger , []);
87
73
88
- $ connection ->expects ($ this ->exactly (2 ))
89
- ->method ('__call ' )
90
- ->willReturnCallback (function ($ method , $ args ) {
91
- if ($ method !== 'smembers ' ) {
92
- return ;
93
- }
74
+ $ membership = $ store ->getMembership ('ctx ' );
75
+
76
+ $ this ->assertNotNull ($ membership );
77
+ $ this ->assertCount (0 , $ membership );
78
+ }
94
79
95
- return match ($ args [0 ]) {
96
- 'launchdarkly:big_segment_include:ctx ' => ['key1 ' , 'key2 ' ],
97
- 'launchdarkly:big_segment_exclude:ctx ' => throw new Exception ('sorry ' ),
98
- default => [],
99
- };
100
- });
80
+ public function testCanDetectInclusionWithInvalidConfiguration (): void
81
+ {
82
+ $ logger = new Log \NullLogger ();
101
83
84
+ $ connection = new Client (['port ' => 33_333 ]);
85
+ $ store = new RedisBigSegmentsStore ($ connection , $ logger , []);
102
86
$ membership = $ store ->getMembership ('ctx ' );
103
87
104
88
$ this ->assertNull ($ membership );
0 commit comments