|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhproTest\ApiProblemBundle\Exception; |
| 6 | + |
| 7 | +use Phpro\ApiProblem\Http\HttpApiProblem; |
| 8 | +use Phpro\ApiProblemBundle\Exception\ApiProblemHttpException; |
| 9 | +use Phpro\ApiProblemBundle\Transformer\ApiProblemExceptionTransformer; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Prophecy\Prophecy\ObjectProphecy; |
| 12 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 13 | + |
| 14 | +class ApiProblemHttpExceptionTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var HttpApiProblem|ObjectProphecy |
| 18 | + */ |
| 19 | + private $apiProblem; |
| 20 | + |
| 21 | + protected function setUp(): void/* The :void return type declaration that should be here would cause a BC issue */ |
| 22 | + { |
| 23 | + $this->apiProblem = $this->prophesize(HttpApiProblem::class); |
| 24 | + $this->apiProblem->toArray()->willReturn([]); |
| 25 | + } |
| 26 | + |
| 27 | + /** @test */ |
| 28 | + public function it_is_accepted_by_the_ApiProblemExceptionTransformer(): void |
| 29 | + { |
| 30 | + $transformer = new ApiProblemExceptionTransformer(); |
| 31 | + |
| 32 | + $this->assertTrue($transformer->accepts(new ApiProblemHttpException($this->apiProblem->reveal()))); |
| 33 | + } |
| 34 | + |
| 35 | + /** @test */ |
| 36 | + public function it_is_an_instance_of_HttpException(): void |
| 37 | + { |
| 38 | + $exception = new ApiProblemHttpException($this->apiProblem->reveal()); |
| 39 | + |
| 40 | + $this->assertInstanceOf(HttpException::class, $exception); |
| 41 | + } |
| 42 | + |
| 43 | + /** @test */ |
| 44 | + public function it_contains_an_api_problem(): void |
| 45 | + { |
| 46 | + $apiProblem = $this->apiProblem->reveal(); |
| 47 | + |
| 48 | + $exception = new ApiProblemHttpException($apiProblem); |
| 49 | + $this->assertEquals($apiProblem, $exception->getApiProblem()); |
| 50 | + } |
| 51 | + |
| 52 | + /** @test */ |
| 53 | + public function it_returns_the_correct_http_headers(): void |
| 54 | + { |
| 55 | + $exception = new ApiProblemHttpException($this->apiProblem->reveal()); |
| 56 | + |
| 57 | + $this->assertEquals(['Content-Type' => 'application/problem+json'], $exception->getHeaders()); |
| 58 | + } |
| 59 | + |
| 60 | + /** @test */ |
| 61 | + public function it_returns_the_correct_default_http_statuscode(): void |
| 62 | + { |
| 63 | + $exception = new ApiProblemHttpException($this->apiProblem->reveal()); |
| 64 | + |
| 65 | + $this->assertEquals(400, $exception->getStatusCode()); |
| 66 | + } |
| 67 | + |
| 68 | + /** @test */ |
| 69 | + public function it_returns_the_correct_specified_http_statuscode(): void |
| 70 | + { |
| 71 | + $this->apiProblem->toArray()->willReturn(['status' => 401]); |
| 72 | + $exception = new ApiProblemHttpException($this->apiProblem->reveal()); |
| 73 | + |
| 74 | + $this->assertEquals(401, $exception->getStatusCode()); |
| 75 | + } |
| 76 | +} |
0 commit comments