ImportReclamationsServiceTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // Create a user and set it as the default MAF order user to avoid FK errors
  138. $user = User::factory()->create();
  139. \App\Models\Setting::set(
  140. \App\Models\Setting::KEY_DEFAULT_MAF_ORDER_USER_ID,
  141. $user->id
  142. );
  143. $order = Order::factory()->create(['year' => (int) date('Y')]);
  144. $product = Product::factory()->create(['year' => (int) date('Y')]);
  145. $rfid = 'RFID-TEST-' . uniqid();
  146. $sku = ProductSKU::factory()->create([
  147. 'rfid' => $rfid,
  148. 'order_id' => $order->id,
  149. 'product_id' => $product->id,
  150. 'year' => (int) date('Y'),
  151. ]);
  152. $import = $this->createImportWithFile([
  153. [
  154. 'Округ ЦАО', // Округ
  155. 'Тверской', // Район
  156. 'ул. Тестовая, 1', // Адрес
  157. 'ART-001', // Артикул
  158. '1.01.01', // Тип
  159. $rfid, // RFID
  160. 'Нет', // Гарантии
  161. 'Покраска', // Что сделано
  162. 46023, // Дата создания (2026-01-01 в Excel serial)
  163. 46024, // Дата начала работ (2026-01-02)
  164. 46054, // Дата завершения работ (2026-02-01)
  165. (int) date('Y'), // Год поставки МАФ
  166. 'Вандализм', // Причина
  167. $status->name, // Статус
  168. 'Тест комментарий',// Комментарий
  169. ],
  170. ]);
  171. $service = new ImportReclamationsService($import);
  172. $result = $service->handle();
  173. $this->assertTrue($result);
  174. $this->assertDatabaseHas('reclamations', [
  175. 'order_id' => $order->id,
  176. 'status_id' => $status->id,
  177. 'reason' => 'Вандализм',
  178. ]);
  179. }
  180. /**
  181. * When the RFID in the row does not match any ProductSKU the row is
  182. * logged with sku_not_found and skipped; handle() still returns true.
  183. */
  184. public function test_handle_skips_row_when_sku_not_found(): void
  185. {
  186. $status = ReclamationStatus::query()->first();
  187. $this->assertNotNull($status);
  188. $import = $this->createImportWithFile([
  189. [
  190. 'Округ ЦАО',
  191. 'Тверской',
  192. 'ул. Тестовая, 1',
  193. 'ART-001',
  194. '1.01.01',
  195. 'RFID-DOES-NOT-EXIST-' . uniqid(), // unknown RFID
  196. 'Нет',
  197. 'Покраска',
  198. '',
  199. '',
  200. '',
  201. (int) date('Y'),
  202. 'Вандализм',
  203. $status->name,
  204. '',
  205. ],
  206. ]);
  207. $service = new ImportReclamationsService($import);
  208. $result = $service->handle();
  209. $this->assertTrue($result);
  210. $this->assertDatabaseMissing('reclamations', ['reason' => 'Вандализм']);
  211. }
  212. /**
  213. * A file that contains only the header row (no data rows) should be
  214. * processed successfully and return true.
  215. */
  216. public function test_handle_returns_true_with_empty_data_rows(): void
  217. {
  218. $import = $this->createImportWithFile([]);
  219. $service = new ImportReclamationsService($import);
  220. $result = $service->handle();
  221. $this->assertTrue($result);
  222. }
  223. }