| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- declare(strict_types=1);
- namespace Tests\Unit\Services\Import;
- use App\Models\CommonCatalogItem;
- use App\Models\Role;
- use App\Models\User;
- use App\Services\Import\ImportTechnicalDescriptionsService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Storage;
- use Tests\TestCase;
- class ImportTechnicalDescriptionsServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected bool $seed = true;
- public function test_import_fills_only_empty_fields_and_imports_image(): void
- {
- Storage::fake('public');
- $user = User::factory()->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<string, string> */
- private function sourceProduct(): array
- {
- return [
- 'article' => 'S1102',
- 'series' => 'Сибирь',
- 'name' => 'Старое название',
- 'name_for_form' => 'Игровой комплекс Сибирь',
- 'product_group' => 'Детское игровое оборудование',
- 'characteristics' => 'Характеристики',
- 'tech_description' => 'Полное описание',
- 'tech_description_short' => 'Краткое описание',
- 'image_path' => 'S1102.jpg',
- ];
- }
- }
|