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