Implemented HttpException
parent
2588bbf7bc
commit
9788c5095a
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Http\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
class HttpException extends RuntimeException
|
||||
{
|
||||
/** @var int */
|
||||
protected $statusCode;
|
||||
|
||||
/** @var array */
|
||||
protected $headers = [];
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
* @param string $message
|
||||
* @param array $headers
|
||||
* @param int $code
|
||||
* @param Throwable|null $previous
|
||||
*/
|
||||
public function __construct(
|
||||
int $statusCode,
|
||||
string $message = '',
|
||||
array $headers = [],
|
||||
int $code = 0,
|
||||
Throwable $previous = null
|
||||
) {
|
||||
$this->headers = $headers;
|
||||
$this->statusCode = $statusCode;
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\Exceptions;
|
||||
|
||||
use Engelsystem\Http\Exceptions\HttpException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class HttpExceptionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Http\Exceptions\HttpException::__construct
|
||||
* @covers \Engelsystem\Http\Exceptions\HttpException::getStatusCode
|
||||
* @covers \Engelsystem\Http\Exceptions\HttpException::getHeaders
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
$exception = new HttpException(123);
|
||||
$this->assertEquals(123, $exception->getStatusCode());
|
||||
$this->assertEquals('', $exception->getMessage());
|
||||
$this->assertEquals([], $exception->getHeaders());
|
||||
|
||||
$exception = new HttpException(404, 'Nothing found', ['page' => '/test']);
|
||||
$this->assertEquals('Nothing found', $exception->getMessage());
|
||||
$this->assertEquals(['page' => '/test'], $exception->getHeaders());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue