Skip to content

Commit 9be08e1

Browse files
apply suggestions from code review
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
1 parent 374c54a commit 9be08e1

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

tests/system/Encryption/KeyRotationDecoratorTest.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use Config\Encryption as EncryptionConfig;
1919
use PHPUnit\Framework\Attributes\Group;
20+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
2021

2122
/**
2223
* @internal
@@ -31,10 +32,9 @@ protected function setUp(): void
3132
$this->encryption = new Encryption();
3233
}
3334

35+
#[RequiresPhpExtension('openssl')]
3436
public function testEncryptionUsesCurrentKey(): void
3537
{
36-
$this->ensureOpenSSL();
37-
3838
$currentKey = 'current-encryption-key';
3939
$previousKey = 'previous-encryption-key';
4040

@@ -54,10 +54,9 @@ public function testEncryptionUsesCurrentKey(): void
5454
$encrypter->decrypt($encrypted, ['key' => $previousKey]);
5555
}
5656

57+
#[RequiresPhpExtension('openssl')]
5758
public function testKeyRotationDecryptsOldData(): void
5859
{
59-
$this->ensureOpenSSL();
60-
6160
$oldKey = 'old-encryption-key';
6261
$newKey = 'new-encryption-key';
6362

@@ -79,10 +78,9 @@ public function testKeyRotationDecryptsOldData(): void
7978
$this->assertSame($message, $newEncrypter->decrypt($encrypted));
8079
}
8180

81+
#[RequiresPhpExtension('openssl')]
8282
public function testMultiplePreviousKeysFallback(): void
8383
{
84-
$this->ensureOpenSSL();
85-
8684
$key1 = 'first-key-very-long';
8785
$key2 = 'second-key-very-long';
8886
$key3 = 'third-key-very-long';
@@ -112,10 +110,9 @@ public function testMultiplePreviousKeysFallback(): void
112110
$this->assertSame($message2, $encrypter3->decrypt($encrypted2));
113111
}
114112

113+
#[RequiresPhpExtension('openssl')]
115114
public function testExplicitKeyPreventsRotation(): void
116115
{
117-
$this->ensureOpenSSL();
118-
119116
$currentKey = 'current-key-very-long';
120117
$previousKey = 'previous-key-very-long';
121118
$explicitKey = 'explicit-key-very-long';
@@ -137,10 +134,9 @@ public function testExplicitKeyPreventsRotation(): void
137134
$encrypter->decrypt($encrypted, ['key' => $explicitKey]);
138135
}
139136

137+
#[RequiresPhpExtension('openssl')]
140138
public function testEmptyPreviousKeysNoFallback(): void
141139
{
142-
$this->ensureOpenSSL();
143-
144140
$key1 = 'first-key-very-long';
145141
$key2 = 'second-key-very-long';
146142

@@ -161,10 +157,9 @@ public function testEmptyPreviousKeysNoFallback(): void
161157
$encrypter2->decrypt($encrypted);
162158
}
163159

160+
#[RequiresPhpExtension('openssl')]
164161
public function testAllKeysFailThrowsOriginalException(): void
165162
{
166-
$this->ensureOpenSSL();
167-
168163
$correctKey = 'correct-key-very-long';
169164
$wrongKey1 = 'wrong-key-1-very-long';
170165
$wrongKey2 = 'wrong-key-2-very-long';
@@ -188,10 +183,9 @@ public function testAllKeysFailThrowsOriginalException(): void
188183
$encrypterWrong->decrypt($encrypted);
189184
}
190185

186+
#[RequiresPhpExtension('openssl')]
191187
public function testPropertyAccessDelegation(): void
192188
{
193-
$this->ensureOpenSSL();
194-
195189
$params = new EncryptionConfig();
196190
$params->driver = 'OpenSSL';
197191
$params->key = 'test-key-very-long';
@@ -204,12 +198,9 @@ public function testPropertyAccessDelegation(): void
204198
$this->assertSame('test-key-very-long', $encrypter->key);
205199
}
206200

201+
#[RequiresPhpExtension('sodium')]
207202
public function testKeyRotationWithSodiumHandler(): void
208203
{
209-
if (! extension_loaded('sodium')) {
210-
$this->markTestSkipped('Sodium is not available.');
211-
}
212-
213204
$oldKey = sodium_crypto_secretbox_keygen();
214205
$newKey = sodium_crypto_secretbox_keygen();
215206

@@ -233,10 +224,9 @@ public function testKeyRotationWithSodiumHandler(): void
233224
$this->assertSame($newMessage, $newEncrypter->decrypt($newEncrypted));
234225
}
235226

227+
#[RequiresPhpExtension('openssl')]
236228
public function testRealisticKeyRotationScenario(): void
237229
{
238-
$this->ensureOpenSSL();
239-
240230
$q1Key = 'q1-2026-key-very-long';
241231
$q2Key = 'q2-2026-key-very-long';
242232
$q3Key = 'q3-2026-key-very-long';
@@ -290,11 +280,4 @@ public function testRealisticKeyRotationScenario(): void
290280
$this->expectException(EncryptionException::class);
291281
$encrypterQ4->decrypt($encryptedQ1);
292282
}
293-
294-
private function ensureOpenSSL(): void
295-
{
296-
if (! extension_loaded('openssl')) {
297-
$this->markTestSkipped('OpenSSL is not available.');
298-
}
299-
}
300283
}

0 commit comments

Comments
 (0)