PdfConverterClientTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Tests\Unit\Services;
  3. use App\Services\PdfConverterClient;
  4. use Illuminate\Support\Facades\Http;
  5. use Tests\TestCase;
  6. class PdfConverterClientTest extends TestCase
  7. {
  8. private string $tempXlsxPath;
  9. private string $tempPdfPath;
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. $this->tempXlsxPath = sys_get_temp_dir() . '/test_pdf_converter_' . uniqid() . '.xlsx';
  14. $this->tempPdfPath = str_replace('.xlsx', '.pdf', $this->tempXlsxPath);
  15. // Create a minimal xlsx file so file_exists() passes inside convert()
  16. file_put_contents($this->tempXlsxPath, 'fake xlsx content');
  17. }
  18. protected function tearDown(): void
  19. {
  20. if (file_exists($this->tempXlsxPath)) {
  21. unlink($this->tempXlsxPath);
  22. }
  23. if (file_exists($this->tempPdfPath)) {
  24. unlink($this->tempPdfPath);
  25. }
  26. parent::tearDown();
  27. }
  28. public function test_convert_writes_pdf_file_on_successful_response(): void
  29. {
  30. $fakePdfContent = '%PDF-1.4 fake pdf content';
  31. Http::fake([
  32. PdfConverterClient::CONVERTER_ADDRESS => Http::response($fakePdfContent, 200),
  33. ]);
  34. PdfConverterClient::convert($this->tempXlsxPath);
  35. $this->assertFileExists($this->tempPdfPath);
  36. $this->assertEquals($fakePdfContent, file_get_contents($this->tempPdfPath));
  37. }
  38. public function test_convert_sends_request_to_correct_url(): void
  39. {
  40. Http::fake([
  41. PdfConverterClient::CONVERTER_ADDRESS => Http::response('pdf-data', 200),
  42. ]);
  43. PdfConverterClient::convert($this->tempXlsxPath);
  44. Http::assertSent(function ($request) {
  45. return $request->url() === PdfConverterClient::CONVERTER_ADDRESS;
  46. });
  47. }
  48. public function test_convert_sends_file_as_multipart_attachment(): void
  49. {
  50. Http::fake([
  51. PdfConverterClient::CONVERTER_ADDRESS => Http::response('pdf-data', 200),
  52. ]);
  53. PdfConverterClient::convert($this->tempXlsxPath);
  54. Http::assertSent(function ($request) {
  55. return str_contains($request->header('Content-Type')[0] ?? '', 'multipart/form-data');
  56. });
  57. }
  58. public function test_convert_does_not_write_pdf_on_error_response(): void
  59. {
  60. Http::fake([
  61. PdfConverterClient::CONVERTER_ADDRESS => Http::response('error', 500),
  62. ]);
  63. PdfConverterClient::convert($this->tempXlsxPath);
  64. $this->assertFileDoesNotExist($this->tempPdfPath);
  65. }
  66. public function test_convert_throws_exception_when_file_does_not_exist(): void
  67. {
  68. $this->expectException(\Exception::class);
  69. $this->expectExceptionMessage('File does not exist');
  70. PdfConverterClient::convert('/non/existent/path/file.xlsx');
  71. }
  72. public function test_convert_replaces_xlsx_extension_with_pdf(): void
  73. {
  74. Http::fake([
  75. PdfConverterClient::CONVERTER_ADDRESS => Http::response('pdf-bytes', 200),
  76. ]);
  77. PdfConverterClient::convert($this->tempXlsxPath);
  78. $expectedPdfPath = str_replace('.xlsx', '.pdf', $this->tempXlsxPath);
  79. $this->assertFileExists($expectedPdfPath);
  80. }
  81. public function test_converter_address_constant_is_correct(): void
  82. {
  83. $this->assertEquals(
  84. 'http://pdf-converter:5000/convert',
  85. PdfConverterClient::CONVERTER_ADDRESS
  86. );
  87. }
  88. }