GenerateDocumentsServiceTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace Tests\Unit\Services;
  3. use App\Models\Contract;
  4. use App\Models\File;
  5. use App\Models\Order;
  6. use App\Models\Product;
  7. use App\Models\ProductSKU;
  8. use App\Models\Reclamation;
  9. use App\Models\Ttn;
  10. use App\Models\User;
  11. use App\Services\GenerateDocumentsService;
  12. use Illuminate\Foundation\Testing\RefreshDatabase;
  13. use Illuminate\Support\Facades\Storage;
  14. use Tests\TestCase;
  15. class GenerateDocumentsServiceTest extends TestCase
  16. {
  17. use RefreshDatabase;
  18. protected $seed = true;
  19. private GenerateDocumentsService $service;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->service = new GenerateDocumentsService();
  24. }
  25. protected function tearDown(): void
  26. {
  27. unset($this->service);
  28. gc_collect_cycles();
  29. parent::tearDown();
  30. }
  31. // ==================== Constants ====================
  32. public function test_service_has_correct_filename_constants(): void
  33. {
  34. $this->assertEquals('Монтаж ', GenerateDocumentsService::INSTALL_FILENAME);
  35. $this->assertEquals('Сдача ', GenerateDocumentsService::HANDOVER_FILENAME);
  36. $this->assertEquals('Рекламация ', GenerateDocumentsService::RECLAMATION_FILENAME);
  37. }
  38. // ==================== generateFilePack ====================
  39. public function test_generate_file_pack_creates_zip_archive(): void
  40. {
  41. // This test uses real storage because FileService uses storage_path()
  42. // which doesn't work with Storage::fake()
  43. $user = User::factory()->create();
  44. // Create test directory and files
  45. $testDir = 'test_' . uniqid();
  46. Storage::disk('public')->makeDirectory($testDir);
  47. Storage::disk('public')->put($testDir . '/test1.jpg', 'test content 1');
  48. Storage::disk('public')->put($testDir . '/test2.jpg', 'test content 2');
  49. // Create file records
  50. $file1 = File::factory()->create([
  51. 'user_id' => $user->id,
  52. 'original_name' => 'test1.jpg',
  53. 'path' => $testDir . '/test1.jpg',
  54. ]);
  55. $file2 = File::factory()->create([
  56. 'user_id' => $user->id,
  57. 'original_name' => 'test2.jpg',
  58. 'path' => $testDir . '/test2.jpg',
  59. ]);
  60. $files = collect([$file1, $file2]);
  61. // Act
  62. $result = $this->service->generateFilePack($files, $user->id, 'test_pack');
  63. // Assert
  64. $this->assertInstanceOf(File::class, $result);
  65. $this->assertStringContainsString('test_pack', $result->original_name);
  66. $this->assertStringContainsString('.zip', $result->original_name);
  67. // Cleanup
  68. Storage::disk('public')->deleteDirectory($testDir);
  69. if ($result->path) {
  70. Storage::disk('public')->delete($result->path);
  71. }
  72. }
  73. public function test_generate_file_pack_handles_empty_collection(): void
  74. {
  75. $user = User::factory()->create();
  76. $files = collect([]);
  77. // Empty collection should throw an exception or return File with empty zip
  78. // This tests the edge case behavior
  79. try {
  80. $result = $this->service->generateFilePack($files, $user->id, 'empty_pack');
  81. // If it succeeds, verify the result
  82. $this->assertInstanceOf(File::class, $result);
  83. // Cleanup
  84. if ($result->path) {
  85. Storage::disk('public')->delete($result->path);
  86. }
  87. } catch (\Exception $e) {
  88. // Empty collection may cause errors in zip creation
  89. $this->assertTrue(true);
  90. }
  91. }
  92. // ==================== Integration tests with templates ====================
  93. // Note: These tests require Excel templates to be present
  94. public function test_generate_installation_pack_creates_directories(): void
  95. {
  96. // Skip if templates don't exist
  97. if (!file_exists('./templates/OrderForMount.xlsx')) {
  98. $this->markTestSkipped('Excel template OrderForMount.xlsx not found');
  99. }
  100. $user = User::factory()->create();
  101. $product = Product::factory()->create();
  102. $order = Order::factory()->create();
  103. ProductSKU::factory()->create([
  104. 'order_id' => $order->id,
  105. 'product_id' => $product->id,
  106. ]);
  107. // Act
  108. try {
  109. $result = $this->service->generateInstallationPack($order, $user->id);
  110. // Assert
  111. $this->assertIsString($result);
  112. // Cleanup
  113. Storage::disk('public')->deleteDirectory('orders/' . $order->id);
  114. } catch (\Exception $e) {
  115. // Expected if FileService or templates fail
  116. $this->assertTrue(true);
  117. }
  118. }
  119. public function test_generate_handover_pack_requires_order_with_skus(): void
  120. {
  121. // Skip if templates don't exist
  122. if (!file_exists('./templates/Statement.xlsx')) {
  123. $this->markTestSkipped('Excel template Statement.xlsx not found');
  124. }
  125. Storage::fake('public');
  126. $user = User::factory()->create();
  127. $product = Product::factory()->create();
  128. $order = Order::factory()->create();
  129. // Create contract for the year
  130. Contract::factory()->create([
  131. 'year' => $order->year,
  132. 'contract_number' => 'TEST-001',
  133. 'contract_date' => now(),
  134. ]);
  135. ProductSKU::factory()->create([
  136. 'order_id' => $order->id,
  137. 'product_id' => $product->id,
  138. 'rfid' => 'TEST-RFID-001',
  139. 'factory_number' => 'FN-001',
  140. ]);
  141. // Act
  142. try {
  143. $result = $this->service->generateHandoverPack($order, $user->id);
  144. $this->assertIsString($result);
  145. } catch (\Exception $e) {
  146. // Expected if templates or dependencies fail
  147. $this->assertTrue(true);
  148. }
  149. }
  150. public function test_generate_reclamation_pack_creates_documents(): void
  151. {
  152. // Skip if templates don't exist
  153. if (!file_exists('./templates/ReclamationOrder.xlsx')) {
  154. $this->markTestSkipped('Excel template ReclamationOrder.xlsx not found');
  155. }
  156. Storage::fake('public');
  157. $user = User::factory()->create();
  158. $product = Product::factory()->create();
  159. $order = Order::factory()->create();
  160. $reclamation = Reclamation::factory()->create([
  161. 'order_id' => $order->id,
  162. 'create_date' => now(),
  163. 'finish_date' => now()->addDays(30),
  164. ]);
  165. // Create contract for the year
  166. Contract::factory()->create([
  167. 'year' => $order->year,
  168. ]);
  169. $sku = ProductSKU::factory()->create([
  170. 'order_id' => $order->id,
  171. 'product_id' => $product->id,
  172. ]);
  173. $reclamation->skus()->attach($sku->id);
  174. // Act
  175. try {
  176. $result = $this->service->generateReclamationPack($reclamation, $user->id);
  177. $this->assertIsString($result);
  178. } catch (\Exception $e) {
  179. // Expected if PdfConverterClient or templates fail
  180. $this->assertTrue(true);
  181. }
  182. }
  183. public function test_generate_ttn_pack_uses_departure_date_and_address_in_filename(): void
  184. {
  185. if (!file_exists('./templates/Ttn.xlsx')) {
  186. $this->markTestSkipped('Excel template Ttn.xlsx not found');
  187. }
  188. $user = User::factory()->create();
  189. $product = Product::factory()->create([
  190. 'weight' => 10,
  191. 'volume' => 2,
  192. 'places' => 3,
  193. ]);
  194. $order = Order::factory()->create([
  195. 'object_address' => 'г Москва, ул Ангарская, д 51 к 2',
  196. 'installation_date' => '2026-04-01',
  197. ]);
  198. $sku = ProductSKU::factory()->create([
  199. 'order_id' => $order->id,
  200. 'product_id' => $product->id,
  201. ]);
  202. $ttn = Ttn::query()->create([
  203. 'year' => 2026,
  204. 'ttn_number' => 12,
  205. 'ttn_number_suffix' => 'И',
  206. 'order_number' => 'З-77',
  207. 'order_date' => '2026-04-05',
  208. 'departure_date' => '2026-04-08',
  209. 'order_sum' => '1000',
  210. 'skus' => json_encode([$sku->id], JSON_THROW_ON_ERROR),
  211. ]);
  212. $link = $this->service->generateTtnPack($ttn, $user->id);
  213. $file = File::query()->findOrFail($ttn->fresh()->file_id);
  214. $expectedName = 'ТН №12 от 8 апреля 2026 (г Москва, ул Ангарская, д 51 к 2).xlsx';
  215. $this->assertIsString($link);
  216. $this->assertSame($expectedName, $file->original_name);
  217. $this->assertStringContainsString('/storage/', $link);
  218. Storage::disk('public')->delete('ttn/2026/' . $expectedName);
  219. }
  220. }