|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use GuzzleHttp\Psr7\HttpFactory; |
| 6 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SimpleAsFuck\ApiToolkit\Service\Http\MessageService; |
| 10 | +use SimpleAsFuck\Validator\Factory\UnexpectedValueException; |
| 11 | + |
| 12 | +#[CoversClass(MessageService::class)] |
| 13 | +final class MessageServiceTest extends TestCase |
| 14 | +{ |
| 15 | + private HttpFactory $httpFactory; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + $this->httpFactory = new HttpFactory(); |
| 20 | + } |
| 21 | + |
| 22 | + #[DataProvider('dataParseErrorMessage')] |
| 23 | + public function testParseErrorMessage(string $expectedErrorMessage, string $invalidContent): void |
| 24 | + { |
| 25 | + $this->expectExceptionMessage($expectedErrorMessage); |
| 26 | + |
| 27 | + $message = $this->httpFactory->createResponse()->withBody($this->httpFactory->createStream($invalidContent)); |
| 28 | + MessageService::parseJsonFromBody(new UnexpectedValueException(), $message, 'Test body', false); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @return non-empty-array<non-empty-array<string>> |
| 33 | + */ |
| 34 | + public static function dataParseErrorMessage(): array |
| 35 | + { |
| 36 | + return [ |
| 37 | + ['Test body must be valid json, invalid content: \'\'', ''], |
| 38 | + ['Test body must be valid json, invalid content: \'kjdfhgroigiosdiugaeiufsabdv\'', 'kjdfhgroigiosdiugaeiufsabdv'], |
| 39 | + ['Test body must be valid json, invalid content: \'kjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigi\' (truncated)', 'kjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdv'], |
| 40 | + ]; |
| 41 | + } |
| 42 | +} |
0 commit comments