| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- declare(strict_types=1);
- namespace Tests\Unit\Services\Export;
- use App\Models\CommonCatalogItem;
- use App\Models\Role;
- use App\Models\Setting;
- use App\Models\User;
- use App\Services\Export\ExportTechnicalDescriptionsService;
- use App\Services\TechnicalDescriptionService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Storage;
- use Tests\TestCase;
- use ZipArchive;
- class ExportTechnicalDescriptionsServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected bool $seed = true;
- public function test_combined_docx_uses_selected_catalog_price_and_description(): void
- {
- Storage::fake('local');
- Storage::fake('public');
- $user = User::factory()->create(['role' => Role::ADMIN]);
- Setting::set(Setting::KEY_TECHNICAL_DESCRIPTION_PRICE_FIELD, 'project_price');
- $item = CommonCatalogItem::factory()->create([
- 'article' => 'S1102',
- 'print_name' => 'Игровой комплекс Сибирь',
- 'product_group' => 'Игровое оборудование',
- 'characteristics' => 'Высота 2 метра',
- 'technical_description_short' => 'Краткое описание',
- 'project_price' => 1234.50,
- 'retail_price' => 9999,
- ]);
- $file = app(ExportTechnicalDescriptionsService::class)->handle(
- [$item->id],
- TechnicalDescriptionService::MODE_CHARACTERISTICS_SHORT,
- false,
- $user->id,
- );
- Storage::disk('local')->assertExists($file->path);
- $xml = $this->documentXml(Storage::disk('local')->path($file->path));
- $this->assertStringContainsString('1 234.50', $xml);
- $this->assertStringNotContainsString('9 999.00', $xml);
- $this->assertStringContainsString('Краткое описание', $xml);
- $this->assertTrue($file->is_generated);
- }
- public function test_separate_export_creates_zip_with_docx_per_item(): void
- {
- Storage::fake('local');
- Storage::fake('public');
- $user = User::factory()->create(['role' => Role::ADMIN]);
- $items = CommonCatalogItem::factory()->count(2)->create();
- $file = app(ExportTechnicalDescriptionsService::class)->handle(
- $items->modelKeys(),
- TechnicalDescriptionService::MODE_FULL,
- true,
- $user->id,
- );
- Storage::disk('local')->assertExists($file->path);
- $zip = new ZipArchive;
- $this->assertTrue($zip->open(Storage::disk('local')->path($file->path)) === true);
- $this->assertSame(2, $zip->numFiles);
- $this->assertStringEndsWith('.docx', (string) $zip->getNameIndex(0));
- $zip->close();
- }
- private function documentXml(string $path): string
- {
- $zip = new ZipArchive;
- $this->assertTrue($zip->open($path) === true);
- $xml = $zip->getFromName('word/document.xml');
- $zip->close();
- $this->assertIsString($xml);
- return $xml;
- }
- }
|