From 1e0d1485dd32120f41cfc4faca512aa8fe4a1f9a Mon Sep 17 00:00:00 2001 From: Orlando Charles Date: Mon, 8 Nov 2021 19:04:19 -0600 Subject: [PATCH] Fix decode return type (#6) --- src/CER.php | 2 +- src/Certificate.php | 2 +- src/KEY.php | 2 +- tests/KEYTest.php | 14 +++++++++++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/CER.php b/src/CER.php index d6cdc53..226c0ad 100644 --- a/src/CER.php +++ b/src/CER.php @@ -29,7 +29,7 @@ public function __construct(string $cerFile) ); } - public function decode(): string + public function decode(): ?string { $prefix = "-----BEGIN CERTIFICATE-----\n"; $suffix = "-----END CERTIFICATE-----\n"; diff --git a/src/Certificate.php b/src/Certificate.php index 32740a4..537bd55 100644 --- a/src/Certificate.php +++ b/src/Certificate.php @@ -17,7 +17,7 @@ abstract class Certificate { use HasFiles; - abstract public function decode(): string; + abstract public function decode(): ?string; public function save(string $directory, string $filename) { diff --git a/src/KEY.php b/src/KEY.php index f6b95e3..8510a72 100644 --- a/src/KEY.php +++ b/src/KEY.php @@ -29,7 +29,7 @@ public function __construct(string $keyFile, string $password) $this->password = $password; } - public function decode(): string + public function decode(): ?string { return shell_exec(sprintf( "openssl pkcs8 -inform DER -in %s -passin pass:%s", diff --git a/tests/KEYTest.php b/tests/KEYTest.php index d2b0931..c1df708 100644 --- a/tests/KEYTest.php +++ b/tests/KEYTest.php @@ -16,7 +16,8 @@ class KEYTest extends TestCase { - public function testConvertKeyToPem() + /** @test */ + public function it_should_decode_the_given_key() { $keyFileName = './tests/files/CSD01_AAA010101AAA.key'; $password = '12345678a'; @@ -28,4 +29,15 @@ public function testConvertKeyToPem() file_get_contents("{$keyFileName}.pem") ); } + + /** @test */ + public function it_should_return_null_when_the_password_doesnt_decode_the_key() + { + $keyFileName = './tests/files/CSD01_AAA010101AAA.key'; + $password = '87654321a'; + + $strategy = new KEY($keyFileName, $password); + + $this->assertEquals($strategy->decode(), null); + } }