Extended DomainNotFoundException to allow creating from an authority

This commit is contained in:
Alejandro Celaya 2021-07-29 18:23:41 +02:00 committed by Alejandro Celaya
parent 4ef5ab7a90
commit 2ac7be4363
2 changed files with 35 additions and 9 deletions

View file

@ -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],
);
}
}

View file

@ -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());
}
}