ExportCommonCatalogServiceTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. 'dimension_length' => 'от 180',
  36. 'calculator_enabled' => true,
  37. 'builders_price' => 1418500,
  38. 'recommended_plus_10_price' => 1888023.50,
  39. ]);
  40. (new ExportCommonCatalogService)->handle($user->id);
  41. $file = File::query()->where('user_id', $user->id)->latest('id')->firstOrFail();
  42. Storage::disk('public')->assertExists($file->path);
  43. $sheet = IOFactory::load(Storage::disk('public')->path($file->path))->getActiveSheet();
  44. $this->assertSame(ExportCommonCatalogService::HEADERS, $sheet->rangeToArray('A1:AD1')[0]);
  45. $this->assertEquals(ExportCommonCatalogService::SUBHEADERS, $sheet->rangeToArray('A2:AD2')[0]);
  46. $this->assertSame('0254', $sheet->getCell('A3')->getValue());
  47. $this->assertSame('Оборудование для благоустройства', $sheet->getCell('B3')->getValue());
  48. $this->assertSame('от 180', $sheet->getCell('D3')->getValue());
  49. $this->assertSame('да', $sheet->getCell('V3')->getValue());
  50. $this->assertSame(1418500.0, $sheet->getCell('W3')->getValue());
  51. $this->assertSame(1888023.5, $sheet->getCell('AD3')->getValue());
  52. $this->assertCount(1, $sheet->getDrawingCollection());
  53. }
  54. }