create(); District::factory()->create(['shortname' => 'ЦАО', 'name' => 'Центральный']); $service = new ExportDistrictsService(); $link = $service->handle($user->id); $this->assertStringContainsString('export_districts_', $link); $this->assertStringContainsString('.xlsx', $link); // Cleanup $file = File::where('user_id', $user->id)->latest()->first(); if ($file && file_exists($file->path)) { unlink($file->path); } } public function test_export_creates_file_record(): void { $user = User::factory()->create(); District::factory()->create(); $service = new ExportDistrictsService(); $service->handle($user->id); $this->assertDatabaseHas('files', [ 'user_id' => $user->id, 'mime_type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ]); // Cleanup $file = File::where('user_id', $user->id)->latest()->first(); if ($file && file_exists($file->path)) { unlink($file->path); } } public function test_export_includes_all_districts(): void { $user = User::factory()->create(); // Create additional test districts District::factory()->create(['shortname' => 'ТЕСТ1', 'name' => 'Тестовый 1']); District::factory()->create(['shortname' => 'ТЕСТ2', 'name' => 'Тестовый 2']); District::factory()->create(['shortname' => 'ТЕСТ3', 'name' => 'Тестовый 3']); $totalCount = District::count(); $service = new ExportDistrictsService(); $link = $service->handle($user->id); // Get the file path from the link $file = File::where('user_id', $user->id)->latest()->first(); $this->assertNotNull($file); // Verify the file exists and contains correct data $this->assertFileExists($file->path); $spreadsheet = IOFactory::load($file->path); $sheet = $spreadsheet->getActiveSheet(); // Check headers $this->assertEquals('ID', $sheet->getCell('A1')->getValue()); $this->assertEquals('Сокращение', $sheet->getCell('B1')->getValue()); $this->assertEquals('Название', $sheet->getCell('C1')->getValue()); // Check that we have correct number of data rows $highestRow = $sheet->getHighestRow(); $this->assertEquals($totalCount + 1, $highestRow); // +1 for header // Cleanup unlink($file->path); } public function test_export_does_not_include_deleted_districts(): void { $user = User::factory()->create(); $beforeCount = District::count(); District::factory()->create(['shortname' => 'ТЕСТАКТ', 'name' => 'Тестовый активный']); $deletedDistrict = District::factory()->create(['shortname' => 'ТЕСТУДЛ', 'name' => 'Тестовый удалённый']); $deletedDistrict->delete(); $afterCount = District::count(); // Should be beforeCount + 1 $service = new ExportDistrictsService(); $link = $service->handle($user->id); $file = File::where('user_id', $user->id)->latest()->first(); $spreadsheet = IOFactory::load($file->path); $sheet = $spreadsheet->getActiveSheet(); // Should have afterCount + 1 rows (header + all non-deleted districts) $highestRow = $sheet->getHighestRow(); $this->assertEquals($afterCount + 1, $highestRow); // Cleanup unlink($file->path); } }