ImportReclamationsServiceTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace Tests\Unit\Services\Import;
  3. use App\Models\Import;
  4. use App\Models\Order;
  5. use App\Models\Product;
  6. use App\Models\ProductSKU;
  7. use App\Models\Reclamation;
  8. use App\Models\ReclamationStatus;
  9. use App\Models\User;
  10. use App\Services\ImportReclamationsService;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Illuminate\Support\Facades\Storage;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  15. use Tests\TestCase;
  16. class ImportReclamationsServiceTest extends TestCase
  17. {
  18. use RefreshDatabase;
  19. protected $seed = true;
  20. private array $tempFiles = [];
  21. protected function tearDown(): void
  22. {
  23. foreach ($this->tempFiles as $path) {
  24. if (file_exists($path)) {
  25. unlink($path);
  26. }
  27. }
  28. parent::tearDown();
  29. }
  30. // -------------------------------------------------------------------------
  31. // Helper: create xlsx file with reclamation headers and optional data rows,
  32. // upload it to Storage::disk('upload') and return the Import model.
  33. // -------------------------------------------------------------------------
  34. private function createImportWithFile(array $dataRows = [], ?array $customHeaders = null): Import
  35. {
  36. $import = Import::factory()->create([
  37. 'type' => 'reclamations',
  38. 'filename' => 'test_reclamations_' . uniqid() . '.xlsx',
  39. 'status' => 'new',
  40. ]);
  41. $tempPath = $this->buildXlsxFile($customHeaders ?? array_keys(ImportReclamationsService::HEADERS), $dataRows);
  42. Storage::fake('upload');
  43. Storage::disk('upload')->put($import->filename, file_get_contents($tempPath));
  44. return $import;
  45. }
  46. private function buildXlsxFile(array $headers, array $dataRows): string
  47. {
  48. $spreadsheet = new Spreadsheet();
  49. $sheet = $spreadsheet->getActiveSheet();
  50. // Write headers in row 1
  51. foreach ($headers as $colIdx => $header) {
  52. $sheet->setCellValue(chr(65 + $colIdx) . '1', $header);
  53. }
  54. // Write data rows starting from row 2
  55. foreach ($dataRows as $rowIdx => $row) {
  56. foreach ($row as $colIdx => $value) {
  57. $sheet->setCellValue(chr(65 + $colIdx) . ($rowIdx + 2), $value);
  58. }
  59. }
  60. $path = sys_get_temp_dir() . '/test_reclamations_' . uniqid() . '.xlsx';
  61. (new XlsxWriter($spreadsheet))->save($path);
  62. $this->tempFiles[] = $path;
  63. return $path;
  64. }
  65. // -------------------------------------------------------------------------
  66. // Tests
  67. // -------------------------------------------------------------------------
  68. /**
  69. * When the file does not exist in the upload disk, prepare() should fail
  70. * and handle() must return false.
  71. */
  72. public function test_handle_returns_false_when_file_not_found(): void
  73. {
  74. Storage::fake('upload');
  75. $import = Import::factory()->create([
  76. 'type' => 'reclamations',
  77. 'filename' => 'nonexistent_file_' . uniqid() . '.xlsx',
  78. 'status' => 'new',
  79. ]);
  80. $service = new ImportReclamationsService($import);
  81. $result = $service->handle();
  82. $this->assertFalse($result);
  83. }
  84. /**
  85. * A file that exists but has wrong column headers must cause prepare() to
  86. * throw "Invalid headers", so handle() returns false.
  87. */
  88. public function test_handle_returns_false_with_wrong_headers(): void
  89. {
  90. $import = $this->createImportWithFile([], ['Wrong', 'Headers', 'Here']);
  91. $service = new ImportReclamationsService($import);
  92. $result = $service->handle();
  93. $this->assertFalse($result);
  94. }
  95. /**
  96. * Correct headers, one data row, but the status name in the row does not
  97. * match any row in reclamation_statuses → the row is skipped with
  98. * status_not_found, but handle() still returns true (processing completes).
  99. */
  100. public function test_handle_returns_false_when_status_not_found(): void
  101. {
  102. $import = $this->createImportWithFile([
  103. [
  104. 'Округ ЦАО', // Округ
  105. 'Тверской', // Район
  106. 'ул. Тестовая, 1', // Адрес
  107. 'ART-001', // Артикул
  108. '1.01.01', // Тип
  109. 'RFID-UNKNOWN-999', // RFID
  110. 'Нет', // Гарантии
  111. 'Покраска', // Что сделано
  112. 45000, // Дата создания (excel serial)
  113. 45001, // Дата начала работ
  114. 45002, // Дата завершения работ
  115. 2024, // Год поставки МАФ
  116. 'Вандализм', // Причина
  117. 'НесуществующийСтатус', // Статус
  118. '', // Комментарий
  119. ],
  120. ]);
  121. $service = new ImportReclamationsService($import);
  122. // handle() returns true (the import ran), but the row was skipped
  123. $result = $service->handle();
  124. $this->assertTrue($result);
  125. $this->assertDatabaseMissing('reclamations', ['reason' => 'Вандализм']);
  126. }
  127. /**
  128. * Full happy path: a ProductSKU with a known RFID exists, the status
  129. * exists in the DB (seeded), the import creates a Reclamation and attaches
  130. * the SKU to it.
  131. */
  132. public function test_handle_creates_reclamation_when_sku_found(): void
  133. {
  134. // ReclamationStatus is seeded via $seed = true (ReclamationStatusSeeder).
  135. $status = ReclamationStatus::query()->first();
  136. $this->assertNotNull($status, 'ReclamationStatusSeeder must create at least one status');
  137. $order = Order::factory()->create(['year' => (int) date('Y')]);
  138. $product = Product::factory()->create(['year' => (int) date('Y')]);
  139. $rfid = 'RFID-TEST-' . uniqid();
  140. $sku = ProductSKU::factory()->create([
  141. 'rfid' => $rfid,
  142. 'order_id' => $order->id,
  143. 'product_id' => $product->id,
  144. 'year' => (int) date('Y'),
  145. ]);
  146. $import = $this->createImportWithFile([
  147. [
  148. 'Округ ЦАО', // Округ
  149. 'Тверской', // Район
  150. 'ул. Тестовая, 1', // Адрес
  151. 'ART-001', // Артикул
  152. '1.01.01', // Тип
  153. $rfid, // RFID
  154. 'Нет', // Гарантии
  155. 'Покраска', // Что сделано
  156. 46023, // Дата создания (2026-01-01 в Excel serial)
  157. 46024, // Дата начала работ (2026-01-02)
  158. 46054, // Дата завершения работ (2026-02-01)
  159. (int) date('Y'), // Год поставки МАФ
  160. 'Вандализм', // Причина
  161. $status->name, // Статус
  162. 'Тест комментарий',// Комментарий
  163. ],
  164. ]);
  165. $service = new ImportReclamationsService($import);
  166. $result = $service->handle();
  167. $this->assertTrue($result);
  168. $this->assertDatabaseHas('reclamations', [
  169. 'order_id' => $order->id,
  170. 'status_id' => $status->id,
  171. 'reason' => 'Вандализм',
  172. ]);
  173. }
  174. /**
  175. * When the RFID in the row does not match any ProductSKU the row is
  176. * logged with sku_not_found and skipped; handle() still returns true.
  177. */
  178. public function test_handle_skips_row_when_sku_not_found(): void
  179. {
  180. $status = ReclamationStatus::query()->first();
  181. $this->assertNotNull($status);
  182. $import = $this->createImportWithFile([
  183. [
  184. 'Округ ЦАО',
  185. 'Тверской',
  186. 'ул. Тестовая, 1',
  187. 'ART-001',
  188. '1.01.01',
  189. 'RFID-DOES-NOT-EXIST-' . uniqid(), // unknown RFID
  190. 'Нет',
  191. 'Покраска',
  192. '',
  193. '',
  194. '',
  195. (int) date('Y'),
  196. 'Вандализм',
  197. $status->name,
  198. '',
  199. ],
  200. ]);
  201. $service = new ImportReclamationsService($import);
  202. $result = $service->handle();
  203. $this->assertTrue($result);
  204. $this->assertDatabaseMissing('reclamations', ['reason' => 'Вандализм']);
  205. }
  206. /**
  207. * A file that contains only the header row (no data rows) should be
  208. * processed successfully and return true.
  209. */
  210. public function test_handle_returns_true_with_empty_data_rows(): void
  211. {
  212. $import = $this->createImportWithFile([]);
  213. $service = new ImportReclamationsService($import);
  214. $result = $service->handle();
  215. $this->assertTrue($result);
  216. }
  217. }