This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw more specific exceptions on bind and unbind
- Loading branch information
Showing
7 changed files
with
208 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace DASPRiD\Formidable\Mapping\Exception; | ||
|
||
use DomainException; | ||
use Throwable; | ||
|
||
final class BindFailureException extends DomainException implements ExceptionInterface | ||
{ | ||
use NestedMappingExceptionTrait; | ||
|
||
public static function fromBindException(string $mappingKey, Throwable $previous) : self | ||
{ | ||
return self::fromException('bind', $mappingKey, $previous); | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace DASPRiD\Formidable\Mapping\Exception; | ||
|
||
use Throwable; | ||
|
||
trait NestedMappingExceptionTrait | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $mappingKey; | ||
|
||
protected static function fromException(string $verb, string $mappingKey, Throwable $previous) : self | ||
{ | ||
if ($previous instanceof self) { | ||
$mappingKey = sprintf('%s.%s', $mappingKey, $previous->mappingKey); | ||
$actualException = $previous->getActualException(); | ||
} else { | ||
$actualException = $previous; | ||
} | ||
|
||
$exception = new self( | ||
sprintf('Failed to %s %s: %s', $verb, $mappingKey, $actualException->getMessage()), | ||
0, | ||
$previous | ||
); | ||
$exception->mappingKey = $mappingKey; | ||
|
||
return $exception; | ||
} | ||
|
||
public function getActualException() : Throwable | ||
{ | ||
$previous = $this->getPrevious(); | ||
|
||
if ($previous instanceof self) { | ||
return $previous->getActualException(); | ||
} | ||
|
||
return $previous; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace DASPRiD\Formidable\Mapping\Exception; | ||
|
||
use DomainException; | ||
use Throwable; | ||
|
||
final class UnbindFailureException extends DomainException implements ExceptionInterface | ||
{ | ||
use NestedMappingExceptionTrait; | ||
|
||
public static function fromUnbindException(string $mappingKey, Throwable $previous) : self | ||
{ | ||
return self::fromException('unbind', $mappingKey, $previous); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace DASPRiD\FormidableTest\Mapping\Exception; | ||
|
||
use DASPRiD\Formidable\Mapping\Exception\BindFailureException; | ||
use Exception; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
/** | ||
* @covers DASPRiD\Formidable\Mapping\Exception\BindFailureException | ||
* @covers DASPRiD\Formidable\Mapping\Exception\NestedMappingExceptionTrait | ||
*/ | ||
class BindFailureExceptionTest extends TestCase | ||
{ | ||
public function testFromBindExceptionWithGenericException() | ||
{ | ||
$previous = new Exception('test'); | ||
$exception = BindFailureException::fromBindException('foo', $previous); | ||
|
||
$this->assertSame( | ||
'Failed to bind foo: test', | ||
$exception->getMessage() | ||
); | ||
$this->assertSame($previous, $exception->getPrevious()); | ||
} | ||
|
||
public function testFromBindExceptionWithNestedBindFailureException() | ||
{ | ||
$previous = BindFailureException::fromBindException( | ||
'bar', | ||
BindFailureException::fromBindException('baz', new Exception('test')) | ||
); | ||
$exception = BindFailureException::fromBindException('foo', $previous); | ||
|
||
$this->assertSame( | ||
'Failed to bind foo.bar.baz: test', | ||
$exception->getMessage() | ||
); | ||
$this->assertSame($previous, $exception->getPrevious()); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace DASPRiD\FormidableTest\Mapping\Exception; | ||
|
||
use DASPRiD\Formidable\Mapping\Exception\UnbindFailureException; | ||
use Exception; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
/** | ||
* @covers DASPRiD\Formidable\Mapping\Exception\UnbindFailureException | ||
* @covers DASPRiD\Formidable\Mapping\Exception\NestedMappingExceptionTrait | ||
*/ | ||
class UnbindFailureExceptionTest extends TestCase | ||
{ | ||
public function testFromUnbindExceptionWithGenericException() | ||
{ | ||
$previous = new Exception('test'); | ||
$exception = UnbindFailureException::fromUnbindException('foo', $previous); | ||
|
||
$this->assertSame( | ||
'Failed to unbind foo: test', | ||
$exception->getMessage() | ||
); | ||
$this->assertSame($previous, $exception->getPrevious()); | ||
} | ||
|
||
public function testFromUnbindExceptionWithNestedBindFailureException() | ||
{ | ||
$previous = UnbindFailureException::fromUnbindException( | ||
'bar', | ||
UnbindFailureException::fromUnbindException('baz', new Exception('test')) | ||
); | ||
$exception = UnbindFailureException::fromUnbindException('foo', $previous); | ||
|
||
$this->assertSame( | ||
'Failed to unbind foo.bar.baz: test', | ||
$exception->getMessage() | ||
); | ||
$this->assertSame($previous, $exception->getPrevious()); | ||
} | ||
} |
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