ExportTechnicalDescriptionsServiceTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Unit\Services\Export;
  4. use App\Models\CommonCatalogItem;
  5. use App\Models\Role;
  6. use App\Models\Setting;
  7. use App\Models\User;
  8. use App\Services\Export\ExportTechnicalDescriptionsService;
  9. use App\Services\TechnicalDescriptionService;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Support\Facades\Storage;
  12. use Tests\TestCase;
  13. use ZipArchive;
  14. class ExportTechnicalDescriptionsServiceTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. protected bool $seed = true;
  18. public function test_combined_docx_uses_selected_catalog_price_and_description(): void
  19. {
  20. Storage::fake('local');
  21. Storage::fake('public');
  22. $user = User::factory()->create(['role' => Role::ADMIN]);
  23. Setting::set(Setting::KEY_TECHNICAL_DESCRIPTION_PRICE_FIELD, 'project_price');
  24. $item = CommonCatalogItem::factory()->create([
  25. 'article' => 'S1102',
  26. 'print_name' => 'Игровой комплекс Сибирь',
  27. 'product_group' => 'Игровое оборудование',
  28. 'characteristics' => 'Высота 2 метра',
  29. 'technical_description_short' => 'Краткое описание',
  30. 'project_price' => 1234.50,
  31. 'retail_price' => 9999,
  32. ]);
  33. $file = app(ExportTechnicalDescriptionsService::class)->handle(
  34. [$item->id],
  35. TechnicalDescriptionService::MODE_CHARACTERISTICS_SHORT,
  36. false,
  37. $user->id,
  38. );
  39. Storage::disk('local')->assertExists($file->path);
  40. $xml = $this->documentXml(Storage::disk('local')->path($file->path));
  41. $this->assertStringContainsString('1 234.50', $xml);
  42. $this->assertStringNotContainsString('9 999.00', $xml);
  43. $this->assertStringContainsString('Краткое описание', $xml);
  44. $this->assertTrue($file->is_generated);
  45. }
  46. public function test_separate_export_creates_zip_with_docx_per_item(): void
  47. {
  48. Storage::fake('local');
  49. Storage::fake('public');
  50. $user = User::factory()->create(['role' => Role::ADMIN]);
  51. $items = CommonCatalogItem::factory()->count(2)->create();
  52. $file = app(ExportTechnicalDescriptionsService::class)->handle(
  53. $items->modelKeys(),
  54. TechnicalDescriptionService::MODE_FULL,
  55. true,
  56. $user->id,
  57. );
  58. Storage::disk('local')->assertExists($file->path);
  59. $zip = new ZipArchive;
  60. $this->assertTrue($zip->open(Storage::disk('local')->path($file->path)) === true);
  61. $this->assertSame(2, $zip->numFiles);
  62. $this->assertStringEndsWith('.docx', (string) $zip->getNameIndex(0));
  63. $zip->close();
  64. }
  65. private function documentXml(string $path): string
  66. {
  67. $zip = new ZipArchive;
  68. $this->assertTrue($zip->open($path) === true);
  69. $xml = $zip->getFromName('word/document.xml');
  70. $zip->close();
  71. $this->assertIsString($xml);
  72. return $xml;
  73. }
  74. }