Skip to content

Commit f0aa01b

Browse files
innocenzibrendt
authored andcommitted
ci: install redis in isolated kv-store tests (#1490)
1 parent 6615751 commit f0aa01b

File tree

11 files changed

+63
-52
lines changed

11 files changed

+63
-52
lines changed

.github/workflows/isolated-tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ jobs:
6161
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo, intl
6262
coverage: pcov
6363

64+
- name: Setup Redis
65+
if: ${{ matrix.os != 'windows-latest' && matrix.package.name == 'kv-store' }}
66+
uses: supercharge/[email protected]
67+
6468
- name: Install PHPUnit
6569
run: composer global require phpunit/phpunit:^12.2.3
6670

packages/cryptography/tests/Encryption/EncryptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function test_time_protection(): void
6262
$this->assertSame('important data', $encrypter->decrypt($encrypted));
6363
$elapsed = microtime(true) - $start;
6464

65-
$this->assertEqualsToMoreOrLess(0.3, $elapsed, margin: 0.015);
65+
$this->assertEqualsToMoreOrLess(0.3, $elapsed, margin: 0.020, windowsMargin: 0.025);
6666
}
6767

6868
public function test_wrong_key(): void

packages/cryptography/tests/HasMoreIntegerAssertions.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
trait HasMoreIntegerAssertions
88
{
9-
private function assertEqualsToMoreOrLess(int|float $expected, int|float $actual, int|float $margin): void
9+
private function assertEqualsToMoreOrLess(int|float $expected, int|float $actual, int|float $margin, null|int|float $windowsMargin = null): void
1010
{
11+
if ($windowsMargin && PHP_OS_FAMILY === 'Windows') {
12+
$margin = $windowsMargin;
13+
}
14+
1115
try {
1216
$this->assertGreaterThanOrEqual($expected - $margin, $actual);
1317
$this->assertLessThanOrEqual($expected + $margin, $actual);

packages/cryptography/tests/Signing/SignerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function test_time_protection(): void
166166
$this->assertTrue($signer->verify($data, $signature));
167167
$elapsed = microtime(true) - $start;
168168

169-
$this->assertEqualsToMoreOrLess(0.3, $elapsed, margin: 0.015);
169+
$this->assertEqualsToMoreOrLess(0.3, $elapsed, margin: 0.015, windowsMargin: 0.025);
170170
}
171171

172172
public function test_time_protection_with_mock_clock(): void

packages/cryptography/tests/TimelockTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function test_locks_for_duration(): void
3131

3232
$elapsed = microtime(true) - $start;
3333

34-
$this->assertEqualsToMoreOrLess(0.1, $elapsed, margin: 0.015);
34+
$this->assertEqualsToMoreOrLess(0.1, $elapsed, margin: 0.015, windowsMargin: 0.025);
3535
}
3636

3737
public function test_return_early(): void
@@ -63,7 +63,7 @@ public function test_throws_exception_after_delay(): void
6363
);
6464
} catch (\RuntimeException) {
6565
$elapsed = microtime(true) - $start;
66-
$this->assertEqualsToMoreOrLess(0.1, $elapsed, margin: 0.015);
66+
$this->assertEqualsToMoreOrLess(0.1, $elapsed, margin: 0.015, windowsMargin: 0.025);
6767
}
6868
}
6969

packages/kv-store/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"require": {
77
"php": "^8.4",
88
"tempest/support": "dev-main",
9+
"tempest/datetime": "dev-main",
910
"tempest/event-bus": "dev-main"
1011
},
1112
"require-dev": {

packages/kv-store/tests/PhpRedisClientTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
namespace Tempest\KeyValue\Tests;
44

5+
use PHPUnit\Framework\Attributes\PostCondition;
6+
use PHPUnit\Framework\Attributes\PreCondition;
7+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
58
use PHPUnit\Framework\TestCase;
69
use Tempest\KeyValue\Redis\Config\RedisConfig;
710
use Tempest\KeyValue\Redis\PhpRedisClient;
11+
use Throwable;
812

13+
#[RequiresPhpExtension('redis')]
914
final class PhpRedisClientTest extends TestCase
1015
{
1116
private PhpRedisClient $redis;
1217

13-
protected function setUp(): void
18+
#[PreCondition]
19+
protected function configure(): void
1420
{
15-
parent::setUp();
16-
17-
if (! extension_loaded('redis') || ! class_exists(\Redis::class)) {
18-
$this->markTestSkipped('The `redis` extension is not loaded.');
19-
}
20-
2121
$this->redis = new PhpRedisClient(
2222
client: new \Redis(),
2323
config: new RedisConfig(
@@ -29,17 +29,17 @@ protected function setUp(): void
2929

3030
try {
3131
$this->redis->connect();
32-
} catch (\Throwable) {
32+
} catch (Throwable) {
3333
$this->markTestSkipped('Could not connect to Redis.');
3434
}
3535
}
3636

37-
protected function tearDown(): void
37+
#[PostCondition]
38+
protected function cleanup(): void
3839
{
3940
try {
4041
$this->redis->flush();
41-
} finally {
42-
parent::tearDown();
42+
} catch (Throwable) {
4343
}
4444
}
4545

packages/kv-store/tests/PredisClientTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace Tempest\KeyValue\Tests;
44

5+
use PHPUnit\Framework\Attributes\PostCondition;
6+
use PHPUnit\Framework\Attributes\PreCondition;
57
use PHPUnit\Framework\TestCase;
68
use Predis;
7-
use Tempest\KeyValue\Redis\PhpRedisClient;
89
use Tempest\KeyValue\Redis\PredisClient;
10+
use Throwable;
911

1012
final class PredisClientTest extends TestCase
1113
{
1214
private PredisClient $redis;
1315

14-
protected function setUp(): void
16+
#[PreCondition]
17+
protected function configure(): void
1518
{
16-
parent::setUp();
17-
1819
if (! class_exists(Predis\Client::class)) {
1920
$this->markTestSkipped('The `predis/predis` package is not installed.');
2021
}
@@ -34,17 +35,17 @@ protected function setUp(): void
3435

3536
try {
3637
$this->redis->connect();
37-
} catch (\Throwable) {
38+
} catch (Throwable) {
3839
$this->markTestSkipped('Could not connect to Redis.');
3940
}
4041
}
4142

42-
protected function tearDown(): void
43+
#[PostCondition]
44+
protected function cleanup(): void
4345
{
4446
try {
4547
$this->redis->flush();
46-
} finally {
47-
parent::tearDown();
48+
} catch (Throwable) {
4849
}
4950
}
5051

packages/support/tests/Filesystem/UnixFunctionsTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tempest\Support\Tests\Filesystem;
44

5+
use PHPUnit\Framework\Attributes\PostCondition;
6+
use PHPUnit\Framework\Attributes\PreCondition;
57
use PHPUnit\Framework\Attributes\TestWith;
68
use PHPUnit\Framework\TestCase;
79
use Tempest\Support\Filesystem;
@@ -17,22 +19,24 @@ final class UnixFunctionsTest extends TestCase
1719
{
1820
private string $fixtures = __DIR__ . '/Fixtures';
1921

20-
protected function setUp(): void
22+
#[PreCondition]
23+
protected function configure(): void
2124
{
22-
parent::setUp();
23-
2425
if (PHP_OS_FAMILY === 'Windows') {
25-
$this->markTestSkipped('Irrelevant on Windows.');
26+
$this->markTestSkipped('This test is only for Unix-like systems.');
2627
}
2728

2829
Filesystem\ensure_directory_empty($this->fixtures);
2930

3031
$this->assertTrue(is_dir($this->fixtures));
3132
}
3233

33-
protected function tearDown(): void
34+
#[PostCondition]
35+
protected function cleanup(): void
3436
{
35-
parent::tearDown();
37+
if (PHP_OS_FAMILY === 'Windows') {
38+
return;
39+
}
3640

3741
Filesystem\delete_directory($this->fixtures);
3842

tests/Integration/KeyValue/RedisTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
namespace Tests\Tempest\Integration\KeyValue;
44

5+
use PHPUnit\Framework\Attributes\PostCondition;
6+
use PHPUnit\Framework\Attributes\PreCondition;
57
use Tempest\KeyValue\Redis\Config\RedisConfig;
68
use Tempest\KeyValue\Redis\Redis;
79
use Tempest\KeyValue\Redis\RedisCommandExecuted;
810
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
11+
use Throwable;
912

1013
final class RedisTest extends FrameworkIntegrationTestCase
1114
{
1215
private Redis $redis;
1316

14-
protected function setUp(): void
17+
#[PreCondition]
18+
protected function configure(): void
1519
{
16-
parent::setUp();
17-
1820
$this->eventBus->preventEventHandling();
1921

2022
$this->container->config(new RedisConfig(
@@ -32,12 +34,12 @@ protected function setUp(): void
3234
}
3335
}
3436

35-
protected function tearDown(): void
37+
#[PostCondition]
38+
protected function cleanup(): void
3639
{
3740
try {
3841
$this->redis->flush();
39-
} finally {
40-
parent::tearDown();
42+
} catch (Throwable) {
4143
}
4244
}
4345

0 commit comments

Comments
 (0)