| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace Tests\Unit\Services;
- use App\Models\Contract;
- use App\Models\File;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\ProductSKU;
- use App\Models\Reclamation;
- use App\Models\User;
- use App\Services\GenerateDocumentsService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Storage;
- use Tests\TestCase;
- class GenerateDocumentsServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- private GenerateDocumentsService $service;
- protected function setUp(): void
- {
- parent::setUp();
- $this->service = new GenerateDocumentsService();
- }
- // ==================== Constants ====================
- public function test_service_has_correct_filename_constants(): void
- {
- $this->assertEquals('Монтаж ', GenerateDocumentsService::INSTALL_FILENAME);
- $this->assertEquals('Сдача ', GenerateDocumentsService::HANDOVER_FILENAME);
- $this->assertEquals('Рекламация ', GenerateDocumentsService::RECLAMATION_FILENAME);
- }
- // ==================== generateFilePack ====================
- public function test_generate_file_pack_creates_zip_archive(): void
- {
- // This test uses real storage because FileService uses storage_path()
- // which doesn't work with Storage::fake()
- $user = User::factory()->create();
- // Create test directory and files
- $testDir = 'test_' . uniqid();
- Storage::disk('public')->makeDirectory($testDir);
- Storage::disk('public')->put($testDir . '/test1.jpg', 'test content 1');
- Storage::disk('public')->put($testDir . '/test2.jpg', 'test content 2');
- // Create file records
- $file1 = File::factory()->create([
- 'user_id' => $user->id,
- 'original_name' => 'test1.jpg',
- 'path' => $testDir . '/test1.jpg',
- ]);
- $file2 = File::factory()->create([
- 'user_id' => $user->id,
- 'original_name' => 'test2.jpg',
- 'path' => $testDir . '/test2.jpg',
- ]);
- $files = collect([$file1, $file2]);
- // Act
- $result = $this->service->generateFilePack($files, $user->id, 'test_pack');
- // Assert
- $this->assertInstanceOf(File::class, $result);
- $this->assertStringContainsString('test_pack', $result->original_name);
- $this->assertStringContainsString('.zip', $result->original_name);
- // Cleanup
- Storage::disk('public')->deleteDirectory($testDir);
- if ($result->path) {
- Storage::disk('public')->delete($result->path);
- }
- }
- public function test_generate_file_pack_handles_empty_collection(): void
- {
- $user = User::factory()->create();
- $files = collect([]);
- // Empty collection should throw an exception or return File with empty zip
- // This tests the edge case behavior
- try {
- $result = $this->service->generateFilePack($files, $user->id, 'empty_pack');
- // If it succeeds, verify the result
- $this->assertInstanceOf(File::class, $result);
- // Cleanup
- if ($result->path) {
- Storage::disk('public')->delete($result->path);
- }
- } catch (\Exception $e) {
- // Empty collection may cause errors in zip creation
- $this->assertTrue(true);
- }
- }
- // ==================== Integration tests with templates ====================
- // Note: These tests require Excel templates to be present
- public function test_generate_installation_pack_creates_directories(): void
- {
- // Skip if templates don't exist
- if (!file_exists('./templates/OrderForMount.xlsx')) {
- $this->markTestSkipped('Excel template OrderForMount.xlsx not found');
- }
- $user = User::factory()->create();
- $product = Product::factory()->create();
- $order = Order::factory()->create();
- ProductSKU::factory()->create([
- 'order_id' => $order->id,
- 'product_id' => $product->id,
- ]);
- // Act
- try {
- $result = $this->service->generateInstallationPack($order, $user->id);
- // Assert
- $this->assertIsString($result);
- // Cleanup
- Storage::disk('public')->deleteDirectory('orders/' . $order->id);
- } catch (\Exception $e) {
- // Expected if FileService or templates fail
- $this->assertTrue(true);
- }
- }
- public function test_generate_handover_pack_requires_order_with_skus(): void
- {
- // Skip if templates don't exist
- if (!file_exists('./templates/Statement.xlsx')) {
- $this->markTestSkipped('Excel template Statement.xlsx not found');
- }
- Storage::fake('public');
- $user = User::factory()->create();
- $product = Product::factory()->create();
- $order = Order::factory()->create();
- // Create contract for the year
- Contract::factory()->create([
- 'year' => $order->year,
- 'contract_number' => 'TEST-001',
- 'contract_date' => now(),
- ]);
- ProductSKU::factory()->create([
- 'order_id' => $order->id,
- 'product_id' => $product->id,
- 'rfid' => 'TEST-RFID-001',
- 'factory_number' => 'FN-001',
- ]);
- // Act
- try {
- $result = $this->service->generateHandoverPack($order, $user->id);
- $this->assertIsString($result);
- } catch (\Exception $e) {
- // Expected if templates or dependencies fail
- $this->assertTrue(true);
- }
- }
- public function test_generate_reclamation_pack_creates_documents(): void
- {
- // Skip if templates don't exist
- if (!file_exists('./templates/ReclamationOrder.xlsx')) {
- $this->markTestSkipped('Excel template ReclamationOrder.xlsx not found');
- }
- Storage::fake('public');
- $user = User::factory()->create();
- $product = Product::factory()->create();
- $order = Order::factory()->create();
- $reclamation = Reclamation::factory()->create([
- 'order_id' => $order->id,
- 'create_date' => now(),
- 'finish_date' => now()->addDays(30),
- ]);
- // Create contract for the year
- Contract::factory()->create([
- 'year' => $order->year,
- ]);
- $sku = ProductSKU::factory()->create([
- 'order_id' => $order->id,
- 'product_id' => $product->id,
- ]);
- $reclamation->skus()->attach($sku->id);
- // Act
- try {
- $result = $this->service->generateReclamationPack($reclamation, $user->id);
- $this->assertIsString($result);
- } catch (\Exception $e) {
- // Expected if PdfConverterClient or templates fail
- $this->assertTrue(true);
- }
- }
- }
|