Skip to content

Commit 9623a51

Browse files
authored
chore: Use real Redis connection for tests (#30)
1 parent 4155441 commit 9623a51

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

tests/Impl/Integrations/RedisBigSegmentsStoreTest.php

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace LaunchDarkly\Impl\Integrations\Tests\Impl\Integrations;
66

7-
use Exception;
87
use LaunchDarkly\Impl\Integrations\RedisBigSegmentsStore;
98
use PHPUnit\Framework;
10-
use Predis\ClientInterface;
9+
use Predis\Client;
1110
use Psr\Log;
1211

1312
class RedisBigSegmentsStoreTest extends Framework\TestCase
@@ -17,32 +16,27 @@ public function testGetMetadata(): void
1716
$now = time();
1817
$logger = new Log\NullLogger();
1918

20-
$connection = $this->createMock(ClientInterface::class);
19+
$connection = new Client();
20+
$connection->flushAll();
2121
$store = new RedisBigSegmentsStore($connection, $logger, []);
2222

23-
$connection->expects($this->once())
24-
->method('__call')
25-
->with('get', ['launchdarkly:big_segments_synchronized_on'])
26-
->willReturn("$now");
27-
2823
$metadata = $store->getMetadata();
24+
$this->assertNull($metadata->getLastUpToDate());
25+
$this->assertTrue($metadata->isStale(10));
2926

27+
$connection->set('launchdarkly:big_segments_synchronized_on', $now);
28+
$metadata = $store->getMetadata();
3029
$this->assertEquals($now, $metadata->getLastUpToDate());
3130
$this->assertFalse($metadata->isStale(10));
3231
}
3332

34-
public function testGetMetadataWithException(): void
33+
public function testGetMetadataWithInvalidConfiguration(): void
3534
{
3635
$logger = new Log\NullLogger();
3736

38-
$connection = $this->createMock(ClientInterface::class);
37+
$connection = new Client(['port' => 33_333]);
3938
$store = new RedisBigSegmentsStore($connection, $logger, []);
4039

41-
$connection->expects($this->once())
42-
->method('__call')
43-
->with('get', ['launchdarkly:big_segments_synchronized_on'])
44-
->willThrowException(new \Exception('sorry'));
45-
4640
$metadata = $store->getMetadata();
4741

4842
$this->assertNull($metadata->getLastUpToDate());
@@ -53,52 +47,42 @@ public function testCanDetectInclusion(): void
5347
{
5448
$logger = new Log\NullLogger();
5549

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');
6554

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, []);
7256

73-
$membership = $store->getMembership('ctx');
57+
$membership = $store->getMembership('ctx') ?? [];
7458

7559
$this->assertCount(3, $membership);
7660
$this->assertTrue($membership['key1']);
7761
$this->assertTrue($membership['key2']);
7862
$this->assertFalse($membership['key3']);
7963
}
8064

81-
public function testCanDetectInclusionWithException(): void
65+
public function testCanDetectInclusionWithEmptyData(): void
8266
{
8367
$logger = new Log\NullLogger();
8468

85-
$connection = $this->createMock(ClientInterface::class);
69+
$connection = new Client();
70+
$connection->flushAll();
71+
8672
$store = new RedisBigSegmentsStore($connection, $logger, []);
8773

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+
}
9479

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();
10183

84+
$connection = new Client(['port' => 33_333]);
85+
$store = new RedisBigSegmentsStore($connection, $logger, []);
10286
$membership = $store->getMembership('ctx');
10387

10488
$this->assertNull($membership);

0 commit comments

Comments
 (0)