|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
| 5 | +namespace Gotenberg\Test; |
| 6 | + |
5 | 7 | use Gotenberg\Exceptions\GotenbergApiErrored; |
6 | 8 | use Gotenberg\Exceptions\NoOutputFileInResponse; |
7 | 9 | use Gotenberg\Gotenberg; |
8 | | -use Gotenberg\Test\DummyClient; |
| 10 | +use Gotenberg\Test\Helpers\Dummies\DummyClient; |
9 | 11 | use GuzzleHttp\Psr7\Request; |
10 | 12 | use GuzzleHttp\Psr7\Response; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use PHPUnit\Framework\Attributes\Test; |
| 15 | + |
| 16 | +use function sys_get_temp_dir; |
| 17 | +use function unlink; |
| 18 | + |
| 19 | +use const DIRECTORY_SEPARATOR; |
11 | 20 |
|
12 | | -it( |
13 | | - 'sends a request', |
14 | | - function (): void { |
| 21 | +final class GotenbergTest extends TestCase |
| 22 | +{ |
| 23 | + #[Test] |
| 24 | + public function it_sends_a_request(): void |
| 25 | + { |
15 | 26 | $response = new Response(200, ['Gotenberg-Trace' => 'debug']); |
16 | 27 | $client = new DummyClient($response); |
17 | 28 |
|
18 | 29 | $response = Gotenberg::send(new Request('POST', 'https://my.url'), $client); |
19 | 30 |
|
20 | | - expect($response)->not()->toBeNull(); |
21 | | - }, |
22 | | -); |
| 31 | + $this->assertNotNull($response); |
| 32 | + } |
23 | 33 |
|
24 | | -it( |
25 | | - 'sends a request and throws an exception if response is not 2xx', |
26 | | - function (bool $withTrace): void { |
| 34 | + #[Test] |
| 35 | + #[DataProvider('provideTraceData')] |
| 36 | + public function it_sends_a_request_and_throws_an_exception_if_response_is_not_2xx(bool $withTrace): void |
| 37 | + { |
27 | 38 | $response = new Response(400, $withTrace ? ['Gotenberg-Trace' => 'debug'] : [], 'Bad Request'); |
28 | 39 | $client = new DummyClient($response); |
29 | 40 |
|
30 | 41 | try { |
31 | 42 | Gotenberg::send(new Request('POST', 'https://my.url'), $client); |
| 43 | + $this->fail('Exception was expected but not thrown.'); |
32 | 44 | } catch (GotenbergApiErrored $e) { |
33 | | - expect($e->getCode())->toEqual(400); |
34 | | - expect($e->getMessage())->toEqual('Bad Request'); |
35 | | - expect($e->getGotenbergTrace())->toEqual($withTrace ? 'debug' : ''); |
36 | | - expect($e->getResponse())->toBe($response); |
37 | | - |
38 | | - throw $e; |
| 45 | + $this->assertSame(400, $e->getCode()); |
| 46 | + $this->assertSame('Bad Request', $e->getMessage()); |
| 47 | + $this->assertSame($withTrace ? 'debug' : '', $e->getGotenbergTrace()); |
| 48 | + $this->assertSame($response, $e->getResponse()); |
39 | 49 | } |
40 | | - }, |
41 | | -)->with([ |
42 | | - 'with trace' => [ true ], |
43 | | - 'without trace' => [ false ], |
44 | | -])->throws(GotenbergApiErrored::class); |
45 | | - |
46 | | -it( |
47 | | - 'saves the output file', |
48 | | - function (): void { |
| 50 | + } |
| 51 | + |
| 52 | + /** @return array<string, array{bool}> */ |
| 53 | + public static function provideTraceData(): array |
| 54 | + { |
| 55 | + return [ |
| 56 | + 'with trace' => [true], |
| 57 | + 'without trace' => [false], |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + #[Test] |
| 62 | + public function it_saves_the_output_file(): void |
| 63 | + { |
49 | 64 | $response = new Response(200, ['Content-Disposition' => 'attachment; filename=my.pdf']); |
50 | 65 | $client = new DummyClient($response); |
51 | 66 |
|
52 | | - $filename = Gotenberg::save(new Request('POST', 'https://my.url'), sys_get_temp_dir(), $client); |
| 67 | + $tempDir = sys_get_temp_dir(); |
| 68 | + $filename = Gotenberg::save(new Request('POST', 'https://my.url'), $tempDir, $client); |
| 69 | + |
| 70 | + $filePath = $tempDir . DIRECTORY_SEPARATOR . 'my.pdf'; |
53 | 71 |
|
54 | | - expect(unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'my.pdf'))->toBeTrue(); |
55 | | - expect($filename)->toEqual('my.pdf'); |
56 | | - }, |
57 | | -); |
| 72 | + // verify file exists and delete it |
| 73 | + $this->assertTrue(unlink($filePath)); |
| 74 | + $this->assertSame('my.pdf', $filename); |
| 75 | + } |
58 | 76 |
|
59 | | -it( |
60 | | - 'throws an exception if there is no attachment', |
61 | | - function (string|null $contentDisposition): void { |
| 77 | + #[Test] |
| 78 | + #[DataProvider('provideContentDispositionData')] |
| 79 | + public function it_throws_an_exception_if_there_is_no_attachment(string|null $contentDisposition): void |
| 80 | + { |
62 | 81 | $response = new Response(200, $contentDisposition === null ? [] : ['Content-Disposition' => $contentDisposition]); |
63 | 82 | $client = new DummyClient($response); |
64 | 83 |
|
| 84 | + $this->expectException(NoOutputFileInResponse::class); |
| 85 | + |
65 | 86 | Gotenberg::save(new Request('POST', 'https://my.url'), sys_get_temp_dir(), $client); |
66 | | - }, |
67 | | -)->with([ |
68 | | - 'without content disposition' => [ null ], |
69 | | - 'with content disposition' => [ 'no attachment' ], |
70 | | -])->throws(NoOutputFileInResponse::class); |
| 87 | + } |
| 88 | + |
| 89 | + /** @return array<string, array{0: string|null}> */ |
| 90 | + public static function provideContentDispositionData(): array |
| 91 | + { |
| 92 | + return [ |
| 93 | + 'without content disposition' => [null], |
| 94 | + 'with content disposition' => ['no attachment'], |
| 95 | + ]; |
| 96 | + } |
| 97 | +} |
0 commit comments