ExportCommonCatalogServiceTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Unit\Services\Export;
  4. use App\Models\CommonCatalogItem;
  5. use App\Models\File;
  6. use App\Models\User;
  7. use App\Services\Export\ExportCommonCatalogService;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Http\UploadedFile;
  10. use Illuminate\Support\Facades\Storage;
  11. use PhpOffice\PhpSpreadsheet\IOFactory;
  12. use Tests\TestCase;
  13. class ExportCommonCatalogServiceTest extends TestCase
  14. {
  15. use RefreshDatabase;
  16. protected bool $seed = true;
  17. public function test_export_uses_approved_columns_and_preserves_article_as_string(): void
  18. {
  19. Storage::fake('public');
  20. $user = User::factory()->create();
  21. $uploadedImage = UploadedFile::fake()->image('0254.png', 100, 100);
  22. $imagePath = 'common_catalog/items/1/image/0254.png';
  23. Storage::disk('public')->put($imagePath, $uploadedImage->getContent());
  24. $image = File::query()->create([
  25. 'user_id' => $user->id,
  26. 'original_name' => '0254.png',
  27. 'mime_type' => 'image/png',
  28. 'path' => $imagePath,
  29. 'link' => '/storage/'.$imagePath,
  30. ]);
  31. CommonCatalogItem::factory()->create([
  32. 'article' => '0254',
  33. 'calculator_name' => 'Оборудование для благоустройства',
  34. 'image_file_id' => $image->id,
  35. ]);
  36. (new ExportCommonCatalogService)->handle($user->id);
  37. $file = File::query()->where('user_id', $user->id)->latest('id')->firstOrFail();
  38. Storage::disk('public')->assertExists($file->path);
  39. $sheet = IOFactory::load(Storage::disk('public')->path($file->path))->getActiveSheet();
  40. $this->assertSame(ExportCommonCatalogService::HEADERS, $sheet->rangeToArray('A1:S1')[0]);
  41. $this->assertSame('0254', $sheet->getCell('C2')->getValue());
  42. $this->assertSame('Оборудование для благоустройства', $sheet->getCell('D2')->getValue());
  43. $this->assertCount(1, $sheet->getDrawingCollection());
  44. }
  45. }