create(['role' => Role::ADMIN]); $item = CommonCatalogItem::factory()->create([ 'article' => 'S1102', 'calculator_name' => 'Актуальное название', 'print_name' => null, 'technical_description' => null, 'project_price' => 1000, ]); $directory = sys_get_temp_dir().'/technical-description-test-'.uniqid(); mkdir($directory); file_put_contents( $directory.'/S1102.jpg', UploadedFile::fake()->image('S1102.jpg', 80, 80)->getContent(), ); try { $stats = app(ImportTechnicalDescriptionsService::class)->handle( [$this->sourceProduct()], $directory, $user->id, ); } finally { unlink($directory.'/S1102.jpg'); rmdir($directory); } $item->refresh(); $this->assertSame('Актуальное название', $item->calculator_name); $this->assertSame('Игровой комплекс Сибирь', $item->print_name); $this->assertSame('Полное описание', $item->technical_description); $this->assertSame(1000.0, $item->project_price); $this->assertNotNull($item->imageFile); Storage::disk('public')->assertExists($item->imageFile->path); $this->assertSame(1, $stats['updated']); $this->assertSame(1, $stats['images_imported']); } public function test_import_skips_ambiguous_article_by_default(): void { $user = User::factory()->create(['role' => Role::ADMIN]); CommonCatalogItem::factory()->create(['article' => 'S1102', 'calculator_name' => 'Вариант 1']); CommonCatalogItem::factory()->create(['article' => 'S1102', 'calculator_name' => 'Вариант 2']); $stats = app(ImportTechnicalDescriptionsService::class)->handle( [$this->sourceProduct()], null, $user->id, ); $this->assertSame(1, $stats['ambiguous']); $this->assertSame(['S1102'], $stats['ambiguous_articles']); $this->assertDatabaseMissing('common_catalog_items', ['print_name' => 'Игровой комплекс Сибирь']); } public function test_dry_run_does_not_change_data(): void { $user = User::factory()->create(['role' => Role::ADMIN]); $item = CommonCatalogItem::factory()->create([ 'article' => 'S1102', 'print_name' => null, ]); $stats = app(ImportTechnicalDescriptionsService::class)->handle( [$this->sourceProduct()], null, $user->id, dryRun: true, ); $this->assertNull($item->refresh()->print_name); $this->assertSame(1, $stats['updated']); } /** @return array */ private function sourceProduct(): array { return [ 'article' => 'S1102', 'series' => 'Сибирь', 'name' => 'Старое название', 'name_for_form' => 'Игровой комплекс Сибирь', 'product_group' => 'Детское игровое оборудование', 'characteristics' => 'Характеристики', 'tech_description' => 'Полное описание', 'tech_description_short' => 'Краткое описание', 'image_path' => 'S1102.jpg', ]; } }