| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace Tests\Unit\Services\Dictionary;
- use App\Models\Dictionary\Area;
- use App\Models\Dictionary\District;
- use App\Models\File;
- use App\Models\User;
- use App\Services\Export\ExportAreasService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Storage;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use Tests\TestCase;
- class ExportAreasServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- public function test_export_creates_xlsx_file(): void
- {
- $user = User::factory()->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);
- }
- }
|