| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace Tests\Unit\Services\Dictionary;
- use App\Models\Dictionary\District;
- use App\Services\Import\ImportDistrictsService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- use Tests\TestCase;
- class ImportDistrictsServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- private string $tempFilePath;
- protected function tearDown(): void
- {
- if (isset($this->tempFilePath) && file_exists($this->tempFilePath)) {
- unlink($this->tempFilePath);
- }
- parent::tearDown();
- }
- public function test_import_creates_new_districts(): void
- {
- $this->createTestFile([
- ['', 'НОВ', 'Новый округ'],
- ['', 'ТСТ', 'Тестовый округ'],
- ]);
- $service = new ImportDistrictsService($this->tempFilePath, 1);
- $result = $service->handle();
- $this->assertTrue($result['success']);
- $this->assertEquals(2, $result['imported']);
- $this->assertDatabaseHas('districts', ['shortname' => 'НОВ', 'name' => 'Новый округ']);
- $this->assertDatabaseHas('districts', ['shortname' => 'ТСТ', 'name' => 'Тестовый округ']);
- }
- public function test_import_updates_existing_districts_by_id(): void
- {
- $district = District::factory()->create(['shortname' => 'СТР', 'name' => 'Старый']);
- $this->createTestFile([
- [$district->id, 'НВ', 'Новое название'],
- ]);
- $service = new ImportDistrictsService($this->tempFilePath, 1);
- $result = $service->handle();
- $this->assertTrue($result['success']);
- $this->assertEquals(1, $result['updated']);
- $this->assertDatabaseHas('districts', [
- 'id' => $district->id,
- 'shortname' => 'НВ',
- 'name' => 'Новое название',
- ]);
- }
- public function test_import_updates_existing_districts_by_shortname(): void
- {
- // Удаляем существующий ЦАО из сидера если есть
- District::where('shortname', 'ТЕСТЦАО')->forceDelete();
- $district = District::factory()->create(['shortname' => 'ТЕСТЦАО', 'name' => 'Старое']);
- $this->createTestFile([
- ['', 'ТЕСТЦАО', 'Центральный административный округ'],
- ]);
- $service = new ImportDistrictsService($this->tempFilePath, 1);
- $result = $service->handle();
- $this->assertTrue($result['success']);
- $this->assertEquals(1, $result['updated']);
- $this->assertDatabaseHas('districts', [
- 'id' => $district->id,
- 'shortname' => 'ТЕСТЦАО',
- 'name' => 'Центральный административный округ',
- ]);
- }
- public function test_import_skips_empty_rows(): void
- {
- $this->createTestFile([
- ['', '', ''],
- ['', 'НОВТЕСТ', 'Новый округ тест'],
- ['', '', ''],
- ]);
- $service = new ImportDistrictsService($this->tempFilePath, 1);
- $result = $service->handle();
- $this->assertTrue($result['success']);
- $this->assertEquals(1, $result['imported']);
- $this->assertGreaterThanOrEqual(1, $result['skipped']);
- $this->assertDatabaseHas('districts', ['shortname' => 'НОВТЕСТ', 'name' => 'Новый округ тест']);
- }
- public function test_import_skips_rows_without_name(): void
- {
- $this->createTestFile([
- ['', 'НОВ', ''], // No name
- ['', 'ТСТ', 'Тестовый'],
- ]);
- $service = new ImportDistrictsService($this->tempFilePath, 1);
- $result = $service->handle();
- $this->assertTrue($result['success']);
- $this->assertEquals(1, $result['imported']);
- $this->assertEquals(1, $result['skipped']);
- }
- public function test_import_restores_soft_deleted_districts(): void
- {
- $district = District::factory()->create(['shortname' => 'УДЛ', 'name' => 'Удалённый']);
- $district->delete();
- $this->createTestFile([
- ['', 'УДЛ', 'Восстановленный'],
- ]);
- $service = new ImportDistrictsService($this->tempFilePath, 1);
- $result = $service->handle();
- $this->assertTrue($result['success']);
- $this->assertEquals(1, $result['updated']);
- $this->assertDatabaseHas('districts', [
- 'id' => $district->id,
- 'shortname' => 'УДЛ',
- 'name' => 'Восстановленный',
- 'deleted_at' => null,
- ]);
- }
- public function test_import_generates_shortname_if_missing(): void
- {
- $this->createTestFile([
- ['', '', 'Длинное название округа'],
- ]);
- $service = new ImportDistrictsService($this->tempFilePath, 1);
- $result = $service->handle();
- $this->assertTrue($result['success']);
- $this->assertEquals(1, $result['imported']);
- $this->assertDatabaseHas('districts', [
- 'shortname' => 'Длинное на',
- 'name' => 'Длинное название округа',
- ]);
- }
- public function test_import_returns_logs(): void
- {
- $this->createTestFile([
- ['', 'НОВ', 'Новый округ'],
- ]);
- $service = new ImportDistrictsService($this->tempFilePath, 1);
- $result = $service->handle();
- $this->assertArrayHasKey('logs', $result);
- $this->assertNotEmpty($result['logs']);
- }
- private function createTestFile(array $rows): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- // Headers
- $sheet->setCellValue('A1', 'ID');
- $sheet->setCellValue('B1', 'Сокращение');
- $sheet->setCellValue('C1', 'Название');
- // Data rows
- $rowNum = 2;
- foreach ($rows as $row) {
- $sheet->setCellValue('A' . $rowNum, $row[0] ?? '');
- $sheet->setCellValue('B' . $rowNum, $row[1] ?? '');
- $sheet->setCellValue('C' . $rowNum, $row[2] ?? '');
- $rowNum++;
- }
- $this->tempFilePath = sys_get_temp_dir() . '/test_districts_' . uniqid() . '.xlsx';
- $writer = new Xlsx($spreadsheet);
- $writer->save($this->tempFilePath);
- }
- }
|