ExportCommonCatalogServiceTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 'print_name' => 'Теневой навес для детского сада',
  40. 'product_group' => 'Теневые навесы',
  41. 'characteristics' => 'Размер 9 × 3 м',
  42. 'technical_description' => 'Полное техническое описание',
  43. 'technical_description_short' => 'Краткое техническое описание',
  44. ]);
  45. (new ExportCommonCatalogService)->handle($user->id);
  46. $file = File::query()->where('user_id', $user->id)->latest('id')->firstOrFail();
  47. Storage::disk('public')->assertExists($file->path);
  48. $sheet = IOFactory::load(Storage::disk('public')->path($file->path))->getActiveSheet();
  49. $this->assertSame(ExportCommonCatalogService::HEADERS, $sheet->rangeToArray('A1:AD1')[0]);
  50. $this->assertEquals(ExportCommonCatalogService::SUBHEADERS, $sheet->rangeToArray('A2:AD2')[0]);
  51. $this->assertSame('0254', $sheet->getCell('A3')->getValue());
  52. $this->assertSame('Оборудование для благоустройства', $sheet->getCell('B3')->getValue());
  53. $this->assertSame('от 180', $sheet->getCell('D3')->getValue());
  54. $this->assertSame('да', $sheet->getCell('V3')->getValue());
  55. $this->assertSame(1418500.0, $sheet->getCell('W3')->getValue());
  56. $this->assertSame(1888023.5, $sheet->getCell('AD3')->getValue());
  57. $this->assertCount(1, $sheet->getDrawingCollection());
  58. $technicalDescriptionsSheet = $sheet->getParent()->getSheetByName(
  59. ExportCommonCatalogService::TECHNICAL_DESCRIPTIONS_SHEET,
  60. );
  61. $this->assertNotNull($technicalDescriptionsSheet);
  62. $this->assertSame(
  63. ExportCommonCatalogService::TECHNICAL_DESCRIPTION_HEADERS,
  64. $technicalDescriptionsSheet->rangeToArray('A1:G1')[0],
  65. );
  66. $this->assertSame('0254', $technicalDescriptionsSheet->getCell('A2')->getValue());
  67. $this->assertSame('Теневой навес для детского сада', $technicalDescriptionsSheet->getCell('C2')->getValue());
  68. $this->assertSame('Полное техническое описание', $technicalDescriptionsSheet->getCell('F2')->getValue());
  69. }
  70. }