-
Notifications
You must be signed in to change notification settings - Fork 11
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 #9 from tjveldhuizen/feature-httpexception
Added Exception class implementing HttpExceptionInterface
- Loading branch information
Showing
4 changed files
with
122 additions
and
3 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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpro\ApiProblemBundle\Exception; | ||
|
||
use Phpro\ApiProblem\ApiProblemInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class ApiProblemHttpException extends HttpException | ||
{ | ||
private $apiProblem; | ||
|
||
public function __construct(ApiProblemInterface $apiProblem) | ||
{ | ||
$data = $apiProblem->toArray(); | ||
$message = $data['detail'] ?? ($data['title'] ?? ''); | ||
$code = (int) ($data['status'] ?? 0); | ||
|
||
parent::__construct($code, $message); | ||
$this->apiProblem = $apiProblem; | ||
} | ||
|
||
public function getApiProblem(): ApiProblemInterface | ||
{ | ||
return $this->apiProblem; | ||
} | ||
|
||
public function getStatusCode() | ||
{ | ||
return parent::getStatusCode() > 0 ? parent::getStatusCode() : Response::HTTP_BAD_REQUEST; | ||
} | ||
|
||
public function getHeaders() | ||
{ | ||
return ['Content-Type' => 'application/problem+json']; | ||
} | ||
} |
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhproTest\ApiProblemBundle\Exception; | ||
|
||
use Phpro\ApiProblem\Http\HttpApiProblem; | ||
use Phpro\ApiProblemBundle\Exception\ApiProblemHttpException; | ||
use Phpro\ApiProblemBundle\Transformer\ApiProblemExceptionTransformer; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class ApiProblemHttpExceptionTest extends TestCase | ||
{ | ||
/** | ||
* @var HttpApiProblem|ObjectProphecy | ||
*/ | ||
private $apiProblem; | ||
|
||
protected function setUp(): void/* The :void return type declaration that should be here would cause a BC issue */ | ||
{ | ||
$this->apiProblem = $this->prophesize(HttpApiProblem::class); | ||
$this->apiProblem->toArray()->willReturn([]); | ||
} | ||
|
||
/** @test */ | ||
public function it_is_accepted_by_the_ApiProblemExceptionTransformer(): void | ||
{ | ||
$transformer = new ApiProblemExceptionTransformer(); | ||
|
||
$this->assertTrue($transformer->accepts(new ApiProblemHttpException($this->apiProblem->reveal()))); | ||
} | ||
|
||
/** @test */ | ||
public function it_is_an_instance_of_HttpException(): void | ||
{ | ||
$exception = new ApiProblemHttpException($this->apiProblem->reveal()); | ||
|
||
$this->assertInstanceOf(HttpException::class, $exception); | ||
} | ||
|
||
/** @test */ | ||
public function it_contains_an_api_problem(): void | ||
{ | ||
$apiProblem = $this->apiProblem->reveal(); | ||
|
||
$exception = new ApiProblemHttpException($apiProblem); | ||
$this->assertEquals($apiProblem, $exception->getApiProblem()); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_the_correct_http_headers(): void | ||
{ | ||
$exception = new ApiProblemHttpException($this->apiProblem->reveal()); | ||
|
||
$this->assertEquals(['Content-Type' => 'application/problem+json'], $exception->getHeaders()); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_the_correct_default_http_statuscode(): void | ||
{ | ||
$exception = new ApiProblemHttpException($this->apiProblem->reveal()); | ||
|
||
$this->assertEquals(400, $exception->getStatusCode()); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_the_correct_specified_http_statuscode(): void | ||
{ | ||
$this->apiProblem->toArray()->willReturn(['status' => 401]); | ||
$exception = new ApiProblemHttpException($this->apiProblem->reveal()); | ||
|
||
$this->assertEquals(401, $exception->getStatusCode()); | ||
} | ||
} |