Skip to content

chore: Use real Redis connection for tests #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 29 additions & 45 deletions tests/Impl/Integrations/RedisBigSegmentsStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace LaunchDarkly\Impl\Integrations\Tests\Impl\Integrations;

use Exception;
use LaunchDarkly\Impl\Integrations\RedisBigSegmentsStore;
use PHPUnit\Framework;
use Predis\ClientInterface;
use Predis\Client;
use Psr\Log;

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

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

$connection->expects($this->once())
->method('__call')
->with('get', ['launchdarkly:big_segments_synchronized_on'])
->willReturn("$now");

$metadata = $store->getMetadata();
$this->assertNull($metadata->getLastUpToDate());
$this->assertTrue($metadata->isStale(10));

$connection->set('launchdarkly:big_segments_synchronized_on', $now);
$metadata = $store->getMetadata();
$this->assertEquals($now, $metadata->getLastUpToDate());
$this->assertFalse($metadata->isStale(10));
}

public function testGetMetadataWithException(): void
public function testGetMetadataWithInvalidConfiguration(): void
{
$logger = new Log\NullLogger();

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

$connection->expects($this->once())
->method('__call')
->with('get', ['launchdarkly:big_segments_synchronized_on'])
->willThrowException(new \Exception('sorry'));

$metadata = $store->getMetadata();

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

$connection = $this->createMock(ClientInterface::class);
$store = new RedisBigSegmentsStore($connection, $logger, []);

$connection->expects($this->exactly(2))
->method('__call')
->willReturnCallback(function ($method, $args) {
if ($method !== 'smembers') {
return;
}
$connection = new Client();
$connection->flushAll();
$connection->sAdd('launchdarkly:big_segment_include:ctx', 'key1', 'key2');
$connection->sAdd('launchdarkly:big_segment_exclude:ctx', 'key1', 'key3');

return match ($args[0]) {
'launchdarkly:big_segment_include:ctx' => ['key1', 'key2'],
'launchdarkly:big_segment_exclude:ctx' => ['key1', 'key3'],
default => [],
};
});
$store = new RedisBigSegmentsStore($connection, $logger, []);

$membership = $store->getMembership('ctx');
$membership = $store->getMembership('ctx') ?? [];

$this->assertCount(3, $membership);
$this->assertTrue($membership['key1']);
$this->assertTrue($membership['key2']);
$this->assertFalse($membership['key3']);
}

public function testCanDetectInclusionWithException(): void
public function testCanDetectInclusionWithEmptyData(): void
{
$logger = new Log\NullLogger();

$connection = $this->createMock(ClientInterface::class);
$connection = new Client();
$connection->flushAll();

$store = new RedisBigSegmentsStore($connection, $logger, []);

$connection->expects($this->exactly(2))
->method('__call')
->willReturnCallback(function ($method, $args) {
if ($method !== 'smembers') {
return;
}
$membership = $store->getMembership('ctx');

$this->assertNotNull($membership);
$this->assertCount(0, $membership);
}

return match ($args[0]) {
'launchdarkly:big_segment_include:ctx' => ['key1', 'key2'],
'launchdarkly:big_segment_exclude:ctx' => throw new Exception('sorry'),
default => [],
};
});
public function testCanDetectInclusionWithInvalidConfiguration(): void
{
$logger = new Log\NullLogger();

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

$this->assertNull($membership);
Expand Down
Loading