Deduplicate crypto gateway tests
This commit is contained in:
70
tests/Unit/Support/CryptoTest.php
Normal file
70
tests/Unit/Support/CryptoTest.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Tests\Unit\Support;
|
||||
|
||||
use MintyPHP\Support\Crypto;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CryptoTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
if (!defined('APP_CRYPTO_KEY')) {
|
||||
define('APP_CRYPTO_KEY', str_repeat('ab', 32));
|
||||
}
|
||||
}
|
||||
|
||||
public function testIsConfiguredReturnsTrueWhenKeyIsSet(): void
|
||||
{
|
||||
self::assertTrue(Crypto::isConfigured());
|
||||
}
|
||||
|
||||
public function testEncryptDecryptRoundTrip(): void
|
||||
{
|
||||
$plaintext = 'client-secret-for-sso';
|
||||
$encrypted = Crypto::encryptString($plaintext);
|
||||
|
||||
self::assertNotSame($plaintext, $encrypted);
|
||||
self::assertSame($plaintext, Crypto::decryptString($encrypted));
|
||||
}
|
||||
|
||||
public function testEncryptProducesDifferentCiphertextEachTime(): void
|
||||
{
|
||||
$plaintext = 'same-settings-value';
|
||||
|
||||
self::assertNotSame(
|
||||
Crypto::encryptString($plaintext),
|
||||
Crypto::encryptString($plaintext)
|
||||
);
|
||||
}
|
||||
|
||||
public function testDecryptThrowsOnInvalidOrTamperedPayload(): void
|
||||
{
|
||||
$encrypted = Crypto::encryptString('secret');
|
||||
$tampered = substr($encrypted, 0, 5) . 'Z' . substr($encrypted, 6);
|
||||
|
||||
try {
|
||||
Crypto::decryptString('garbage-data');
|
||||
self::fail('Expected invalid payload to throw.');
|
||||
} catch (\RuntimeException) {
|
||||
}
|
||||
|
||||
try {
|
||||
Crypto::decryptString(base64_encode('{}'));
|
||||
self::fail('Expected malformed payload to throw.');
|
||||
} catch (\RuntimeException) {
|
||||
}
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
Crypto::decryptString($tampered);
|
||||
}
|
||||
|
||||
public function testEncryptEmptyStringProducesMalformedPayloadOnDecrypt(): void
|
||||
{
|
||||
$encrypted = Crypto::encryptString('');
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessage('malformed');
|
||||
Crypto::decryptString($encrypted);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user