ImportOrdersServiceTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace Tests\Unit\Services;
  3. use App\Models\Dictionary\Area;
  4. use App\Models\Dictionary\District;
  5. use App\Models\Import;
  6. use App\Models\MafOrder;
  7. use App\Models\ObjectType;
  8. use App\Models\Order;
  9. use App\Models\OrderStatus;
  10. use App\Models\Product;
  11. use App\Models\ProductSKU;
  12. use App\Models\User;
  13. use App\Services\ImportOrdersService;
  14. use Illuminate\Foundation\Testing\RefreshDatabase;
  15. use Illuminate\Support\Facades\Storage;
  16. use Tests\TestCase;
  17. class ImportOrdersServiceTest extends TestCase
  18. {
  19. use RefreshDatabase;
  20. protected $seed = true;
  21. private string $testFilePath = 'tests/fixtures/test_orders_import.xlsx';
  22. protected function setUp(): void
  23. {
  24. parent::setUp();
  25. // Copy test file to upload storage if it exists
  26. if (file_exists(base_path($this->testFilePath))) {
  27. Storage::disk('upload')->put(
  28. 'test_orders_import.xlsx',
  29. file_get_contents(base_path($this->testFilePath))
  30. );
  31. }
  32. }
  33. protected function tearDown(): void
  34. {
  35. Storage::disk('upload')->delete('test_orders_import.xlsx');
  36. parent::tearDown();
  37. }
  38. private function createReferenceData(): array
  39. {
  40. $district = District::factory()->create(['name' => 'ЦАО', 'shortname' => 'ЦАО']);
  41. $area = Area::factory()->create(['name' => 'Тверской', 'district_id' => $district->id]);
  42. $user = User::factory()->create(['name' => 'Тест Менеджер']);
  43. $objectType = ObjectType::factory()->create(['name' => 'Детская площадка']);
  44. $orderStatus = OrderStatus::firstOrCreate(['name' => 'Новый']);
  45. return compact('district', 'area', 'user', 'objectType', 'orderStatus');
  46. }
  47. private function hasTestFile(): bool
  48. {
  49. return file_exists(base_path($this->testFilePath));
  50. }
  51. // ==================== Constants ====================
  52. public function test_service_has_headers_mapping(): void
  53. {
  54. $this->assertIsArray(ImportOrdersService::HEADERS);
  55. $this->assertArrayHasKey('Округ', ImportOrdersService::HEADERS);
  56. $this->assertArrayHasKey('Район', ImportOrdersService::HEADERS);
  57. $this->assertArrayHasKey('Адрес объекта', ImportOrdersService::HEADERS);
  58. $this->assertArrayHasKey('Артикул', ImportOrdersService::HEADERS);
  59. $this->assertArrayHasKey('Год установки', ImportOrdersService::HEADERS);
  60. }
  61. public function test_headers_mapping_contains_required_fields(): void
  62. {
  63. $headers = ImportOrdersService::HEADERS;
  64. // Order fields
  65. $this->assertEquals('orders.name', $headers['Название площадки']);
  66. $this->assertEquals('orders.object_address', $headers['Адрес объекта']);
  67. $this->assertEquals('orders.year', $headers['Год установки']);
  68. // Product fields
  69. $this->assertEquals('products.article', $headers['Артикул']);
  70. $this->assertEquals('products.nomenclature_number', $headers['Номер номенклатуры']);
  71. // References
  72. $this->assertEquals('districts.name', $headers['Округ']);
  73. $this->assertEquals('areas.name', $headers['Район']);
  74. $this->assertEquals('users.name', $headers['Менеджер']);
  75. }
  76. // ==================== Service instantiation ====================
  77. public function test_service_can_be_instantiated(): void
  78. {
  79. $import = Import::factory()->create();
  80. $service = new ImportOrdersService($import);
  81. $this->assertInstanceOf(ImportOrdersService::class, $service);
  82. }
  83. // ==================== Handle method (requires Excel file) ====================
  84. public function test_handle_returns_false_when_file_not_found(): void
  85. {
  86. $import = Import::factory()->create([
  87. 'filename' => 'non_existent_file.xlsx',
  88. 'status' => 'PENDING',
  89. ]);
  90. $service = new ImportOrdersService($import);
  91. $result = $service->handle();
  92. $this->assertFalse($result);
  93. $import->refresh();
  94. $this->assertEquals('ERROR', $import->status);
  95. }
  96. // ==================== Integration tests ====================
  97. public function test_import_creates_order_from_valid_row(): void
  98. {
  99. if (!$this->hasTestFile()) {
  100. $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
  101. }
  102. $this->createReferenceData();
  103. $import = Import::factory()->create([
  104. 'filename' => 'test_orders_import.xlsx',
  105. 'status' => 'PENDING',
  106. ]);
  107. $initialOrderCount = Order::withoutGlobalScopes()->count();
  108. $initialProductCount = Product::withoutGlobalScopes()->count();
  109. $service = new ImportOrdersService($import);
  110. $result = $service->handle();
  111. $this->assertTrue($result);
  112. $import->refresh();
  113. $this->assertEquals('DONE', $import->status);
  114. // Should create 2 orders (rows 1,2 same address; row 5 different address; row 3 skipped; row 4 duplicate)
  115. $this->assertGreaterThan($initialOrderCount, Order::withoutGlobalScopes()->count());
  116. // Should create products
  117. $this->assertGreaterThan($initialProductCount, Product::withoutGlobalScopes()->count());
  118. // Verify order created with correct address
  119. $this->assertDatabaseHas('orders', [
  120. 'object_address' => 'ул. Тестовая, д. 1',
  121. 'year' => 2025,
  122. ]);
  123. }
  124. public function test_import_creates_product_when_not_exists(): void
  125. {
  126. if (!$this->hasTestFile()) {
  127. $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
  128. }
  129. $this->createReferenceData();
  130. $import = Import::factory()->create([
  131. 'filename' => 'test_orders_import.xlsx',
  132. 'status' => 'PENDING',
  133. ]);
  134. // Verify no products with test nomenclature exist
  135. $this->assertDatabaseMissing('products', ['nomenclature_number' => 'NOM-001']);
  136. $service = new ImportOrdersService($import);
  137. $service->handle();
  138. // Verify product was created
  139. $this->assertDatabaseHas('products', [
  140. 'nomenclature_number' => 'NOM-001',
  141. 'article' => 'ART-001',
  142. 'year' => 2025,
  143. ]);
  144. }
  145. public function test_import_skips_row_when_district_not_found(): void
  146. {
  147. if (!$this->hasTestFile()) {
  148. $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
  149. }
  150. $this->createReferenceData();
  151. $import = Import::factory()->create([
  152. 'filename' => 'test_orders_import.xlsx',
  153. 'status' => 'PENDING',
  154. ]);
  155. $service = new ImportOrdersService($import);
  156. $service->handle();
  157. // Row 3 has invalid district "НЕСУЩЕСТВУЮЩИЙ_ОКРУГ" - should be skipped
  158. $this->assertDatabaseMissing('orders', [
  159. 'object_address' => 'ул. Ошибочная, д. 999',
  160. ]);
  161. }
  162. public function test_import_increments_maf_order_quantity(): void
  163. {
  164. if (!$this->hasTestFile()) {
  165. $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
  166. }
  167. $this->createReferenceData();
  168. $import = Import::factory()->create([
  169. 'filename' => 'test_orders_import.xlsx',
  170. 'status' => 'PENDING',
  171. ]);
  172. $service = new ImportOrdersService($import);
  173. $service->handle();
  174. // Rows 1 and 2 have same MAF order number "MAF-001"
  175. // Should create one maf_order with quantity = 2
  176. $mafOrder = MafOrder::withoutGlobalScopes()
  177. ->where('order_number', 'MAF-001')
  178. ->where('year', 2025)
  179. ->first();
  180. $this->assertNotNull($mafOrder);
  181. $this->assertEquals(2, $mafOrder->quantity);
  182. }
  183. public function test_import_skips_duplicate_product_sku(): void
  184. {
  185. if (!$this->hasTestFile()) {
  186. $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
  187. }
  188. $this->createReferenceData();
  189. $import = Import::factory()->create([
  190. 'filename' => 'test_orders_import.xlsx',
  191. 'status' => 'PENDING',
  192. ]);
  193. $service = new ImportOrdersService($import);
  194. $service->handle();
  195. // Row 4 is duplicate of Row 1 (same RFID, product, order, maf, factory_number, etc.)
  196. // Should only have 1 SKU with RFID-001
  197. $skuCount = ProductSKU::withoutGlobalScopes()
  198. ->where('rfid', 'RFID-001')
  199. ->where('year', 2025)
  200. ->count();
  201. $this->assertEquals(1, $skuCount);
  202. }
  203. public function test_import_creates_product_sku_without_maf(): void
  204. {
  205. if (!$this->hasTestFile()) {
  206. $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
  207. }
  208. $this->createReferenceData();
  209. $import = Import::factory()->create([
  210. 'filename' => 'test_orders_import.xlsx',
  211. 'status' => 'PENDING',
  212. ]);
  213. $service = new ImportOrdersService($import);
  214. $service->handle();
  215. // Row 5 has no MAF order - should create SKU with status "требуется"
  216. $sku = ProductSKU::withoutGlobalScopes()
  217. ->where('rfid', 'RFID-004')
  218. ->where('year', 2025)
  219. ->first();
  220. $this->assertNotNull($sku);
  221. $this->assertNull($sku->maf_order_id);
  222. $this->assertEquals('требуется', $sku->status);
  223. }
  224. }