ImportTechnicalDescriptionsServiceTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Unit\Services\Import;
  4. use App\Models\CommonCatalogItem;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use App\Services\Import\ImportTechnicalDescriptionsService;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Http\UploadedFile;
  10. use Illuminate\Support\Facades\Storage;
  11. use Tests\TestCase;
  12. class ImportTechnicalDescriptionsServiceTest extends TestCase
  13. {
  14. use RefreshDatabase;
  15. protected bool $seed = true;
  16. public function test_import_fills_only_empty_fields_and_imports_image(): void
  17. {
  18. Storage::fake('public');
  19. $user = User::factory()->create(['role' => Role::ADMIN]);
  20. $item = CommonCatalogItem::factory()->create([
  21. 'article' => 'S1102',
  22. 'calculator_name' => 'Актуальное название',
  23. 'print_name' => null,
  24. 'technical_description' => null,
  25. 'project_price' => 1000,
  26. ]);
  27. $directory = sys_get_temp_dir().'/technical-description-test-'.uniqid();
  28. mkdir($directory);
  29. file_put_contents(
  30. $directory.'/S1102.jpg',
  31. UploadedFile::fake()->image('S1102.jpg', 80, 80)->getContent(),
  32. );
  33. try {
  34. $stats = app(ImportTechnicalDescriptionsService::class)->handle(
  35. [$this->sourceProduct()],
  36. $directory,
  37. $user->id,
  38. );
  39. } finally {
  40. unlink($directory.'/S1102.jpg');
  41. rmdir($directory);
  42. }
  43. $item->refresh();
  44. $this->assertSame('Актуальное название', $item->calculator_name);
  45. $this->assertSame('Игровой комплекс Сибирь', $item->print_name);
  46. $this->assertSame('Полное описание', $item->technical_description);
  47. $this->assertSame(1000.0, $item->project_price);
  48. $this->assertNotNull($item->imageFile);
  49. Storage::disk('public')->assertExists($item->imageFile->path);
  50. $this->assertSame(1, $stats['updated']);
  51. $this->assertSame(1, $stats['images_imported']);
  52. }
  53. public function test_import_skips_ambiguous_article_by_default(): void
  54. {
  55. $user = User::factory()->create(['role' => Role::ADMIN]);
  56. CommonCatalogItem::factory()->create(['article' => 'S1102', 'calculator_name' => 'Вариант 1']);
  57. CommonCatalogItem::factory()->create(['article' => 'S1102', 'calculator_name' => 'Вариант 2']);
  58. $stats = app(ImportTechnicalDescriptionsService::class)->handle(
  59. [$this->sourceProduct()],
  60. null,
  61. $user->id,
  62. );
  63. $this->assertSame(1, $stats['ambiguous']);
  64. $this->assertSame(['S1102'], $stats['ambiguous_articles']);
  65. $this->assertDatabaseMissing('common_catalog_items', ['print_name' => 'Игровой комплекс Сибирь']);
  66. }
  67. public function test_dry_run_does_not_change_data(): void
  68. {
  69. $user = User::factory()->create(['role' => Role::ADMIN]);
  70. $item = CommonCatalogItem::factory()->create([
  71. 'article' => 'S1102',
  72. 'print_name' => null,
  73. ]);
  74. $stats = app(ImportTechnicalDescriptionsService::class)->handle(
  75. [$this->sourceProduct()],
  76. null,
  77. $user->id,
  78. dryRun: true,
  79. );
  80. $this->assertNull($item->refresh()->print_name);
  81. $this->assertSame(1, $stats['updated']);
  82. }
  83. /** @return array<string, string> */
  84. private function sourceProduct(): array
  85. {
  86. return [
  87. 'article' => 'S1102',
  88. 'series' => 'Сибирь',
  89. 'name' => 'Старое название',
  90. 'name_for_form' => 'Игровой комплекс Сибирь',
  91. 'product_group' => 'Детское игровое оборудование',
  92. 'characteristics' => 'Характеристики',
  93. 'tech_description' => 'Полное описание',
  94. 'tech_description_short' => 'Краткое описание',
  95. 'image_path' => 'S1102.jpg',
  96. ];
  97. }
  98. }