|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpClient\Tests\Exception; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpClient\Exception\HttpExceptionTrait; |
| 16 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Kévin Dunglas <dunglas@gmail.com> |
| 20 | + */ |
| 21 | +class HttpExceptionTraitTest extends TestCase |
| 22 | +{ |
| 23 | + public function provideParseError() |
| 24 | + { |
| 25 | + yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}']; |
| 26 | + yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}']; |
| 27 | + yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}']; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @dataProvider provideParseError |
| 32 | + */ |
| 33 | + public function testParseError(string $mimeType, string $json): void |
| 34 | + { |
| 35 | + $response = $this->createMock(ResponseInterface::class); |
| 36 | + $response |
| 37 | + ->method('getInfo') |
| 38 | + ->will($this->returnValueMap([ |
| 39 | + ['http_code', 400], |
| 40 | + ['url', 'http://example.com'], |
| 41 | + ['raw_headers', [ |
| 42 | + 'HTTP/1.1 400 Bad Request', |
| 43 | + 'Content-Type: '.$mimeType, |
| 44 | + ]], |
| 45 | + ])); |
| 46 | + $response->method('getContent')->willReturn($json); |
| 47 | + |
| 48 | + $e = new TestException($response); |
| 49 | + $this->assertSame(400, $e->getCode()); |
| 50 | + $this->assertSame(<<<ERROR |
| 51 | +An error occurred |
| 52 | +
|
| 53 | +Some details |
| 54 | +ERROR |
| 55 | +, $e->getMessage()); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class TestException extends \Exception |
| 60 | +{ |
| 61 | + use HttpExceptionTrait; |
| 62 | +} |
0 commit comments