| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace Tests\Unit\Services;
- use App\Services\PdfConverterClient;
- use Illuminate\Support\Facades\Http;
- use Tests\TestCase;
- class PdfConverterClientTest extends TestCase
- {
- private string $tempXlsxPath;
- private string $tempPdfPath;
- protected function setUp(): void
- {
- parent::setUp();
- $this->tempXlsxPath = sys_get_temp_dir() . '/test_pdf_converter_' . uniqid() . '.xlsx';
- $this->tempPdfPath = str_replace('.xlsx', '.pdf', $this->tempXlsxPath);
- // Create a minimal xlsx file so file_exists() passes inside convert()
- file_put_contents($this->tempXlsxPath, 'fake xlsx content');
- }
- protected function tearDown(): void
- {
- if (file_exists($this->tempXlsxPath)) {
- unlink($this->tempXlsxPath);
- }
- if (file_exists($this->tempPdfPath)) {
- unlink($this->tempPdfPath);
- }
- parent::tearDown();
- }
- public function test_convert_writes_pdf_file_on_successful_response(): void
- {
- $fakePdfContent = '%PDF-1.4 fake pdf content';
- Http::fake([
- PdfConverterClient::CONVERTER_ADDRESS => Http::response($fakePdfContent, 200),
- ]);
- PdfConverterClient::convert($this->tempXlsxPath);
- $this->assertFileExists($this->tempPdfPath);
- $this->assertEquals($fakePdfContent, file_get_contents($this->tempPdfPath));
- }
- public function test_convert_sends_request_to_correct_url(): void
- {
- Http::fake([
- PdfConverterClient::CONVERTER_ADDRESS => Http::response('pdf-data', 200),
- ]);
- PdfConverterClient::convert($this->tempXlsxPath);
- Http::assertSent(function ($request) {
- return $request->url() === PdfConverterClient::CONVERTER_ADDRESS;
- });
- }
- public function test_convert_sends_file_as_multipart_attachment(): void
- {
- Http::fake([
- PdfConverterClient::CONVERTER_ADDRESS => Http::response('pdf-data', 200),
- ]);
- PdfConverterClient::convert($this->tempXlsxPath);
- Http::assertSent(function ($request) {
- return str_contains($request->header('Content-Type')[0] ?? '', 'multipart/form-data');
- });
- }
- public function test_convert_does_not_write_pdf_on_error_response(): void
- {
- Http::fake([
- PdfConverterClient::CONVERTER_ADDRESS => Http::response('error', 500),
- ]);
- PdfConverterClient::convert($this->tempXlsxPath);
- $this->assertFileDoesNotExist($this->tempPdfPath);
- }
- public function test_convert_throws_exception_when_file_does_not_exist(): void
- {
- $this->expectException(\Exception::class);
- $this->expectExceptionMessage('File does not exist');
- PdfConverterClient::convert('/non/existent/path/file.xlsx');
- }
- public function test_convert_replaces_xlsx_extension_with_pdf(): void
- {
- Http::fake([
- PdfConverterClient::CONVERTER_ADDRESS => Http::response('pdf-bytes', 200),
- ]);
- PdfConverterClient::convert($this->tempXlsxPath);
- $expectedPdfPath = str_replace('.xlsx', '.pdf', $this->tempXlsxPath);
- $this->assertFileExists($expectedPdfPath);
- }
- public function test_converter_address_constant_is_correct(): void
- {
- $this->assertEquals(
- 'http://pdf-converter:5000/convert',
- PdfConverterClient::CONVERTER_ADDRESS
- );
- }
- }
|