create(); $district = District::factory()->create(); Area::factory()->create(['name' => 'Тверской', 'district_id' => $district->id]); $service = new ExportAreasService(); $link = $service->handle($user->id); $this->assertStringContainsString('export_areas_', $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 = District::factory()->create(); Area::factory()->create(['district_id' => $district->id]); $service = new ExportAreasService(); $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_district_shortname(): void { $user = User::factory()->create(); // Clear existing areas from seeder Area::query()->forceDelete(); $district = District::factory()->create(['shortname' => 'ЦАО', 'name' => 'Центральный']); Area::factory()->create(['name' => 'Тверской', 'district_id' => $district->id]); $service = new ExportAreasService(); $link = $service->handle($user->id); $file = File::where('user_id', $user->id)->latest()->first(); $this->assertNotNull($file); $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()); $this->assertEquals('ID округа', $sheet->getCell('D1')->getValue()); // Check data $this->assertEquals('Тверской', $sheet->getCell('B2')->getValue()); $this->assertEquals('ЦАО', $sheet->getCell('C2')->getValue()); $this->assertEquals($district->id, $sheet->getCell('D2')->getValue()); // Cleanup unlink($file->path); } public function test_export_includes_all_areas(): void { $user = User::factory()->create(); // Clear existing areas from seeder Area::query()->forceDelete(); $district = District::factory()->create(); Area::factory()->count(5)->create(['district_id' => $district->id]); $service = new ExportAreasService(); $service->handle($user->id); $file = File::where('user_id', $user->id)->latest()->first(); $spreadsheet = IOFactory::load($file->path); $sheet = $spreadsheet->getActiveSheet(); // Should have 6 rows: header + 5 areas $highestRow = $sheet->getHighestRow(); $this->assertEquals(6, $highestRow); // Cleanup unlink($file->path); } public function test_export_does_not_include_deleted_areas(): void { $user = User::factory()->create(); // Clear existing areas from seeder Area::query()->forceDelete(); $district = District::factory()->create(); Area::factory()->create(['name' => 'Активный', 'district_id' => $district->id]); $deletedArea = Area::factory()->create(['name' => 'Удалённый', 'district_id' => $district->id]); $deletedArea->delete(); $service = new ExportAreasService(); $service->handle($user->id); $file = File::where('user_id', $user->id)->latest()->first(); $spreadsheet = IOFactory::load($file->path); $sheet = $spreadsheet->getActiveSheet(); // Should have only 2 rows: header + 1 area $highestRow = $sheet->getHighestRow(); $this->assertEquals(2, $highestRow); // Cleanup unlink($file->path); } public function test_export_sorts_areas_by_name(): void { $user = User::factory()->create(); // Clear existing areas from seeder Area::query()->forceDelete(); $district = District::factory()->create(); Area::factory()->create(['name' => 'Яузский', 'district_id' => $district->id]); Area::factory()->create(['name' => 'Арбат', 'district_id' => $district->id]); Area::factory()->create(['name' => 'Тверской', 'district_id' => $district->id]); $service = new ExportAreasService(); $service->handle($user->id); $file = File::where('user_id', $user->id)->latest()->first(); $spreadsheet = IOFactory::load($file->path); $sheet = $spreadsheet->getActiveSheet(); // Check order $this->assertEquals('Арбат', $sheet->getCell('B2')->getValue()); $this->assertEquals('Тверской', $sheet->getCell('B3')->getValue()); $this->assertEquals('Яузский', $sheet->getCell('B4')->getValue()); // Cleanup unlink($file->path); } }