-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from IlicMiljan/improve-exception-handling
[Breaking Change] Services - Improve Exception Handling
- Loading branch information
Showing
13 changed files
with
269 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Cipher\Exception; | ||
|
||
use RuntimeException; | ||
use Throwable; | ||
|
||
class InvalidKeyLength extends RuntimeException implements CipherException | ||
{ | ||
public function __construct( | ||
private int $expectedLength, | ||
?Throwable $previous = null | ||
) { | ||
parent::__construct( | ||
'The provided key is too short.', | ||
0, | ||
$previous | ||
); | ||
} | ||
|
||
public function getExpectedLength(): int | ||
{ | ||
return $this->expectedLength; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Exception; | ||
|
||
use Throwable; | ||
|
||
interface EncryptionServiceException extends Throwable | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Exception; | ||
|
||
use RuntimeException; | ||
use Throwable; | ||
|
||
class ValueMustBeObject extends RuntimeException implements EncryptionServiceException | ||
{ | ||
public function __construct( | ||
private string $type, | ||
?Throwable $previous = null | ||
) { | ||
parent::__construct( | ||
'The value must be an object.', | ||
0, | ||
$previous | ||
); | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Exception; | ||
|
||
use RuntimeException; | ||
use Throwable; | ||
|
||
class ValueMustBeString extends RuntimeException implements EncryptionServiceException | ||
{ | ||
public function __construct( | ||
private string $type, | ||
?Throwable $previous = null | ||
) { | ||
parent::__construct( | ||
'The value must be a string.', | ||
0, | ||
$previous | ||
); | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Tests\Cipher\Exception; | ||
|
||
use IlicMiljan\SecureProps\Cipher\Exception\CipherException; | ||
use IlicMiljan\SecureProps\Cipher\Exception\InvalidKeyLength; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
class InvalidKeyLengthTest extends TestCase | ||
{ | ||
private int $expectedLength; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->expectedLength = 32; | ||
} | ||
|
||
public function testCanBeCreated(): void | ||
{ | ||
$exception = new InvalidKeyLength($this->expectedLength); | ||
|
||
$this->assertInstanceOf(InvalidKeyLength::class, $exception); | ||
} | ||
|
||
public function testReturnsExpectedLength(): void | ||
{ | ||
$exception = new InvalidKeyLength($this->expectedLength); | ||
|
||
$this->assertEquals($this->expectedLength, $exception->getExpectedLength()); | ||
} | ||
|
||
public function testPreviousExceptionIsStored(): void | ||
{ | ||
$previous = new RuntimeException('Previous exception'); | ||
$exception = new InvalidKeyLength($this->expectedLength, $previous); | ||
|
||
$this->assertSame($previous, $exception->getPrevious()); | ||
} | ||
|
||
public function testImplementsCipherExceptionInterface(): void | ||
{ | ||
$exception = new InvalidKeyLength($this->expectedLength); | ||
|
||
$this->assertInstanceOf(CipherException::class, $exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Tests\Exception; | ||
|
||
use IlicMiljan\SecureProps\Exception\EncryptionServiceException; | ||
use IlicMiljan\SecureProps\Exception\ValueMustBeObject; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
class ValueMustBeObjectTest extends TestCase | ||
{ | ||
private string $type; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->type = 'string'; | ||
} | ||
|
||
public function testCanBeCreated(): void | ||
{ | ||
$exception = new ValueMustBeObject($this->type); | ||
|
||
$this->assertInstanceOf(ValueMustBeObject::class, $exception); | ||
} | ||
|
||
public function testReturnsType(): void | ||
{ | ||
$exception = new ValueMustBeObject($this->type); | ||
|
||
$this->assertEquals($this->type, $exception->getType()); | ||
} | ||
|
||
public function testPreviousExceptionIsStored(): void | ||
{ | ||
$previous = new RuntimeException('Previous exception'); | ||
$exception = new ValueMustBeObject($this->type, $previous); | ||
|
||
$this->assertSame($previous, $exception->getPrevious()); | ||
} | ||
|
||
public function testImplementsEncryptionServiceExceptionInterface(): void | ||
{ | ||
$exception = new ValueMustBeObject($this->type); | ||
|
||
$this->assertInstanceOf(EncryptionServiceException::class, $exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace IlicMiljan\SecureProps\Tests\Exception; | ||
|
||
use IlicMiljan\SecureProps\Exception\EncryptionServiceException; | ||
use IlicMiljan\SecureProps\Exception\ValueMustBeString; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
class ValueMustBeStringTest extends TestCase | ||
{ | ||
private string $type; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->type = 'object'; | ||
} | ||
|
||
public function testCanBeCreated(): void | ||
{ | ||
$exception = new ValueMustBeString($this->type); | ||
|
||
$this->assertInstanceOf(ValueMustBeString::class, $exception); | ||
} | ||
|
||
public function testReturnsType(): void | ||
{ | ||
$exception = new ValueMustBeString($this->type); | ||
|
||
$this->assertEquals($this->type, $exception->getType()); | ||
} | ||
|
||
public function testPreviousExceptionIsStored(): void | ||
{ | ||
$previous = new RuntimeException('Previous exception'); | ||
$exception = new ValueMustBeString($this->type, $previous); | ||
|
||
$this->assertSame($previous, $exception->getPrevious()); | ||
} | ||
|
||
public function testImplementsEncryptionServiceExceptionInterface(): void | ||
{ | ||
$exception = new ValueMustBeString($this->type); | ||
|
||
$this->assertInstanceOf(EncryptionServiceException::class, $exception); | ||
} | ||
} |
Oops, something went wrong.