| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace Tests\Unit\Services;
- use App\Models\Dictionary\Area;
- use App\Models\Dictionary\District;
- use App\Models\Import;
- use App\Models\MafOrder;
- use App\Models\ObjectType;
- use App\Models\Order;
- use App\Models\OrderStatus;
- use App\Models\Product;
- use App\Models\ProductSKU;
- use App\Models\User;
- use App\Services\ImportOrdersService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Storage;
- use Tests\TestCase;
- class ImportOrdersServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- private string $testFilePath = 'tests/fixtures/test_orders_import.xlsx';
- protected function setUp(): void
- {
- parent::setUp();
- // Copy test file to upload storage if it exists
- if (file_exists(base_path($this->testFilePath))) {
- Storage::disk('upload')->put(
- 'test_orders_import.xlsx',
- file_get_contents(base_path($this->testFilePath))
- );
- }
- }
- protected function tearDown(): void
- {
- Storage::disk('upload')->delete('test_orders_import.xlsx');
- parent::tearDown();
- }
- private function createReferenceData(): array
- {
- $district = District::factory()->create(['name' => 'ЦАО', 'shortname' => 'ЦАО']);
- $area = Area::factory()->create(['name' => 'Тверской', 'district_id' => $district->id]);
- $user = User::factory()->create(['name' => 'Тест Менеджер']);
- $objectType = ObjectType::factory()->create(['name' => 'Детская площадка']);
- $orderStatus = OrderStatus::firstOrCreate(['name' => 'Новый']);
- return compact('district', 'area', 'user', 'objectType', 'orderStatus');
- }
- private function hasTestFile(): bool
- {
- return file_exists(base_path($this->testFilePath));
- }
- // ==================== Constants ====================
- public function test_service_has_headers_mapping(): void
- {
- $this->assertIsArray(ImportOrdersService::HEADERS);
- $this->assertArrayHasKey('Округ', ImportOrdersService::HEADERS);
- $this->assertArrayHasKey('Район', ImportOrdersService::HEADERS);
- $this->assertArrayHasKey('Адрес объекта', ImportOrdersService::HEADERS);
- $this->assertArrayHasKey('Артикул', ImportOrdersService::HEADERS);
- $this->assertArrayHasKey('Год установки', ImportOrdersService::HEADERS);
- }
- public function test_headers_mapping_contains_required_fields(): void
- {
- $headers = ImportOrdersService::HEADERS;
- // Order fields
- $this->assertEquals('orders.name', $headers['Название площадки']);
- $this->assertEquals('orders.object_address', $headers['Адрес объекта']);
- $this->assertEquals('orders.year', $headers['Год установки']);
- // Product fields
- $this->assertEquals('products.article', $headers['Артикул']);
- $this->assertEquals('products.nomenclature_number', $headers['Номер номенклатуры']);
- // References
- $this->assertEquals('districts.name', $headers['Округ']);
- $this->assertEquals('areas.name', $headers['Район']);
- $this->assertEquals('users.name', $headers['Менеджер']);
- }
- // ==================== Service instantiation ====================
- public function test_service_can_be_instantiated(): void
- {
- $import = Import::factory()->create();
- $service = new ImportOrdersService($import);
- $this->assertInstanceOf(ImportOrdersService::class, $service);
- }
- // ==================== Handle method (requires Excel file) ====================
- public function test_handle_returns_false_when_file_not_found(): void
- {
- $import = Import::factory()->create([
- 'filename' => 'non_existent_file.xlsx',
- 'status' => 'PENDING',
- ]);
- $service = new ImportOrdersService($import);
- $result = $service->handle();
- $this->assertFalse($result);
- $import->refresh();
- $this->assertEquals('ERROR', $import->status);
- }
- // ==================== Integration tests ====================
- public function test_import_creates_order_from_valid_row(): void
- {
- if (!$this->hasTestFile()) {
- $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
- }
- $this->createReferenceData();
- $import = Import::factory()->create([
- 'filename' => 'test_orders_import.xlsx',
- 'status' => 'PENDING',
- ]);
- $initialOrderCount = Order::withoutGlobalScopes()->count();
- $initialProductCount = Product::withoutGlobalScopes()->count();
- $service = new ImportOrdersService($import);
- $result = $service->handle();
- $this->assertTrue($result);
- $import->refresh();
- $this->assertEquals('DONE', $import->status);
- // Should create 2 orders (rows 1,2 same address; row 5 different address; row 3 skipped; row 4 duplicate)
- $this->assertGreaterThan($initialOrderCount, Order::withoutGlobalScopes()->count());
- // Should create products
- $this->assertGreaterThan($initialProductCount, Product::withoutGlobalScopes()->count());
- // Verify order created with correct address
- $this->assertDatabaseHas('orders', [
- 'object_address' => 'ул. Тестовая, д. 1',
- 'year' => 2025,
- ]);
- }
- public function test_import_creates_product_when_not_exists(): void
- {
- if (!$this->hasTestFile()) {
- $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
- }
- $this->createReferenceData();
- $import = Import::factory()->create([
- 'filename' => 'test_orders_import.xlsx',
- 'status' => 'PENDING',
- ]);
- // Verify no products with test nomenclature exist
- $this->assertDatabaseMissing('products', ['nomenclature_number' => 'NOM-001']);
- $service = new ImportOrdersService($import);
- $service->handle();
- // Verify product was created
- $this->assertDatabaseHas('products', [
- 'nomenclature_number' => 'NOM-001',
- 'article' => 'ART-001',
- 'year' => 2025,
- ]);
- }
- public function test_import_skips_row_when_district_not_found(): void
- {
- if (!$this->hasTestFile()) {
- $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
- }
- $this->createReferenceData();
- $import = Import::factory()->create([
- 'filename' => 'test_orders_import.xlsx',
- 'status' => 'PENDING',
- ]);
- $service = new ImportOrdersService($import);
- $service->handle();
- // Row 3 has invalid district "НЕСУЩЕСТВУЮЩИЙ_ОКРУГ" - should be skipped
- $this->assertDatabaseMissing('orders', [
- 'object_address' => 'ул. Ошибочная, д. 999',
- ]);
- }
- public function test_import_increments_maf_order_quantity(): void
- {
- if (!$this->hasTestFile()) {
- $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
- }
- $this->createReferenceData();
- $import = Import::factory()->create([
- 'filename' => 'test_orders_import.xlsx',
- 'status' => 'PENDING',
- ]);
- $service = new ImportOrdersService($import);
- $service->handle();
- // Rows 1 and 2 have same MAF order number "MAF-001"
- // Should create one maf_order with quantity = 2
- $mafOrder = MafOrder::withoutGlobalScopes()
- ->where('order_number', 'MAF-001')
- ->where('year', 2025)
- ->first();
- $this->assertNotNull($mafOrder);
- $this->assertEquals(2, $mafOrder->quantity);
- }
- public function test_import_skips_duplicate_product_sku(): void
- {
- if (!$this->hasTestFile()) {
- $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
- }
- $this->createReferenceData();
- $import = Import::factory()->create([
- 'filename' => 'test_orders_import.xlsx',
- 'status' => 'PENDING',
- ]);
- $service = new ImportOrdersService($import);
- $service->handle();
- // Row 4 is duplicate of Row 1 (same RFID, product, order, maf, factory_number, etc.)
- // Should only have 1 SKU with RFID-001
- $skuCount = ProductSKU::withoutGlobalScopes()
- ->where('rfid', 'RFID-001')
- ->where('year', 2025)
- ->count();
- $this->assertEquals(1, $skuCount);
- }
- public function test_import_creates_product_sku_without_maf(): void
- {
- if (!$this->hasTestFile()) {
- $this->markTestSkipped('Requires valid Excel test file at ' . $this->testFilePath);
- }
- $this->createReferenceData();
- $import = Import::factory()->create([
- 'filename' => 'test_orders_import.xlsx',
- 'status' => 'PENDING',
- ]);
- $service = new ImportOrdersService($import);
- $service->handle();
- // Row 5 has no MAF order - should create SKU with status "требуется"
- $sku = ProductSKU::withoutGlobalScopes()
- ->where('rfid', 'RFID-004')
- ->where('year', 2025)
- ->first();
- $this->assertNotNull($sku);
- $this->assertNull($sku->maf_order_id);
- $this->assertEquals('требуется', $sku->status);
- }
- }
|