ExportAreasServiceTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Tests\Unit\Services\Dictionary;
  3. use App\Models\Dictionary\Area;
  4. use App\Models\Dictionary\District;
  5. use App\Models\File;
  6. use App\Models\User;
  7. use App\Services\Export\ExportAreasService;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Support\Facades\Storage;
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use Tests\TestCase;
  12. class ExportAreasServiceTest extends TestCase
  13. {
  14. use RefreshDatabase;
  15. protected $seed = true;
  16. public function test_export_creates_xlsx_file(): void
  17. {
  18. $user = User::factory()->create();
  19. $district = District::factory()->create();
  20. Area::factory()->create(['name' => 'Тверской', 'district_id' => $district->id]);
  21. $service = new ExportAreasService();
  22. $link = $service->handle($user->id);
  23. $this->assertStringContainsString('export_areas_', $link);
  24. $this->assertStringContainsString('.xlsx', $link);
  25. // Cleanup
  26. $file = File::where('user_id', $user->id)->latest()->first();
  27. if ($file && file_exists($file->path)) {
  28. unlink($file->path);
  29. }
  30. }
  31. public function test_export_creates_file_record(): void
  32. {
  33. $user = User::factory()->create();
  34. $district = District::factory()->create();
  35. Area::factory()->create(['district_id' => $district->id]);
  36. $service = new ExportAreasService();
  37. $service->handle($user->id);
  38. $this->assertDatabaseHas('files', [
  39. 'user_id' => $user->id,
  40. 'mime_type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  41. ]);
  42. // Cleanup
  43. $file = File::where('user_id', $user->id)->latest()->first();
  44. if ($file && file_exists($file->path)) {
  45. unlink($file->path);
  46. }
  47. }
  48. public function test_export_includes_district_shortname(): void
  49. {
  50. $user = User::factory()->create();
  51. // Clear existing areas from seeder
  52. Area::query()->forceDelete();
  53. $district = District::factory()->create(['shortname' => 'ЦАО', 'name' => 'Центральный']);
  54. Area::factory()->create(['name' => 'Тверской', 'district_id' => $district->id]);
  55. $service = new ExportAreasService();
  56. $link = $service->handle($user->id);
  57. $file = File::where('user_id', $user->id)->latest()->first();
  58. $this->assertNotNull($file);
  59. $this->assertFileExists($file->path);
  60. $spreadsheet = IOFactory::load($file->path);
  61. $sheet = $spreadsheet->getActiveSheet();
  62. // Check headers
  63. $this->assertEquals('ID', $sheet->getCell('A1')->getValue());
  64. $this->assertEquals('Название', $sheet->getCell('B1')->getValue());
  65. $this->assertEquals('Округ (сокращение)', $sheet->getCell('C1')->getValue());
  66. $this->assertEquals('ID округа', $sheet->getCell('D1')->getValue());
  67. // Check data
  68. $this->assertEquals('Тверской', $sheet->getCell('B2')->getValue());
  69. $this->assertEquals('ЦАО', $sheet->getCell('C2')->getValue());
  70. $this->assertEquals($district->id, $sheet->getCell('D2')->getValue());
  71. // Cleanup
  72. unlink($file->path);
  73. }
  74. public function test_export_includes_all_areas(): void
  75. {
  76. $user = User::factory()->create();
  77. // Clear existing areas from seeder
  78. Area::query()->forceDelete();
  79. $district = District::factory()->create();
  80. Area::factory()->count(5)->create(['district_id' => $district->id]);
  81. $service = new ExportAreasService();
  82. $service->handle($user->id);
  83. $file = File::where('user_id', $user->id)->latest()->first();
  84. $spreadsheet = IOFactory::load($file->path);
  85. $sheet = $spreadsheet->getActiveSheet();
  86. // Should have 6 rows: header + 5 areas
  87. $highestRow = $sheet->getHighestRow();
  88. $this->assertEquals(6, $highestRow);
  89. // Cleanup
  90. unlink($file->path);
  91. }
  92. public function test_export_does_not_include_deleted_areas(): void
  93. {
  94. $user = User::factory()->create();
  95. // Clear existing areas from seeder
  96. Area::query()->forceDelete();
  97. $district = District::factory()->create();
  98. Area::factory()->create(['name' => 'Активный', 'district_id' => $district->id]);
  99. $deletedArea = Area::factory()->create(['name' => 'Удалённый', 'district_id' => $district->id]);
  100. $deletedArea->delete();
  101. $service = new ExportAreasService();
  102. $service->handle($user->id);
  103. $file = File::where('user_id', $user->id)->latest()->first();
  104. $spreadsheet = IOFactory::load($file->path);
  105. $sheet = $spreadsheet->getActiveSheet();
  106. // Should have only 2 rows: header + 1 area
  107. $highestRow = $sheet->getHighestRow();
  108. $this->assertEquals(2, $highestRow);
  109. // Cleanup
  110. unlink($file->path);
  111. }
  112. public function test_export_sorts_areas_by_name(): void
  113. {
  114. $user = User::factory()->create();
  115. // Clear existing areas from seeder
  116. Area::query()->forceDelete();
  117. $district = District::factory()->create();
  118. Area::factory()->create(['name' => 'Яузский', 'district_id' => $district->id]);
  119. Area::factory()->create(['name' => 'Арбат', 'district_id' => $district->id]);
  120. Area::factory()->create(['name' => 'Тверской', 'district_id' => $district->id]);
  121. $service = new ExportAreasService();
  122. $service->handle($user->id);
  123. $file = File::where('user_id', $user->id)->latest()->first();
  124. $spreadsheet = IOFactory::load($file->path);
  125. $sheet = $spreadsheet->getActiveSheet();
  126. // Check order
  127. $this->assertEquals('Арбат', $sheet->getCell('B2')->getValue());
  128. $this->assertEquals('Тверской', $sheet->getCell('B3')->getValue());
  129. $this->assertEquals('Яузский', $sheet->getCell('B4')->getValue());
  130. // Cleanup
  131. unlink($file->path);
  132. }
  133. }