From 2ac7be4363f7e86ad2061da2dca557afa3c51bc5 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 29 Jul 2021 18:23:41 +0200 Subject: [PATCH] Extended DomainNotFoundException to allow creating from an authority --- .../src/Exception/DomainNotFoundException.php | 27 +++++++++++++------ .../Exception/DomainNotFoundExceptionTest.php | 17 +++++++++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/module/Core/src/Exception/DomainNotFoundException.php b/module/Core/src/Exception/DomainNotFoundException.php index b1b97c91..cb19608a 100644 --- a/module/Core/src/Exception/DomainNotFoundException.php +++ b/module/Core/src/Exception/DomainNotFoundException.php @@ -17,16 +17,27 @@ class DomainNotFoundException extends DomainException implements ProblemDetailsE private const TITLE = 'Domain not found'; private const TYPE = 'DOMAIN_NOT_FOUND'; + private function __construct(string $message, array $additional) + { + parent::__construct($message); + + $this->detail = $message; + $this->title = self::TITLE; + $this->type = self::TYPE; + $this->status = StatusCodeInterface::STATUS_NOT_FOUND; + $this->additional = $additional; + } + public static function fromId(string $id): self { - $e = new self(sprintf('Domain with id "%s" could not be found', $id)); + return new self(sprintf('Domain with id "%s" could not be found', $id), ['id' => $id]); + } - $e->detail = $e->getMessage(); - $e->title = self::TITLE; - $e->type = self::TYPE; - $e->status = StatusCodeInterface::STATUS_NOT_FOUND; - $e->additional = ['id' => $id]; - - return $e; + public static function fromAuthority(string $authority): self + { + return new self( + sprintf('Domain with authority "%s" could not be found', $authority), + ['authority' => $authority], + ); } } diff --git a/module/Core/test/Exception/DomainNotFoundExceptionTest.php b/module/Core/test/Exception/DomainNotFoundExceptionTest.php index 6ac26efd..5f2b9889 100644 --- a/module/Core/test/Exception/DomainNotFoundExceptionTest.php +++ b/module/Core/test/Exception/DomainNotFoundExceptionTest.php @@ -12,7 +12,7 @@ use function sprintf; class DomainNotFoundExceptionTest extends TestCase { /** @test */ - public function properlyCreatesExceptionFromNotFoundTag(): void + public function properlyCreatesExceptionFromId(): void { $id = '123'; $expectedMessage = sprintf('Domain with id "%s" could not be found', $id); @@ -25,4 +25,19 @@ class DomainNotFoundExceptionTest extends TestCase self::assertEquals(['id' => $id], $e->getAdditionalData()); self::assertEquals(404, $e->getStatus()); } + + /** @test */ + public function properlyCreatesExceptionFromAuthority(): void + { + $authority = 'example.com'; + $expectedMessage = sprintf('Domain with authority "%s" could not be found', $authority); + $e = DomainNotFoundException::fromAuthority($authority); + + self::assertEquals($expectedMessage, $e->getMessage()); + self::assertEquals($expectedMessage, $e->getDetail()); + self::assertEquals('Domain not found', $e->getTitle()); + self::assertEquals('DOMAIN_NOT_FOUND', $e->getType()); + self::assertEquals(['authority' => $authority], $e->getAdditionalData()); + self::assertEquals(404, $e->getStatus()); + } }