ExportDistrictsServiceTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Tests\Unit\Services\Dictionary;
  3. use App\Models\Dictionary\District;
  4. use App\Models\File;
  5. use App\Models\User;
  6. use App\Services\Export\ExportDistrictsService;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Illuminate\Support\Facades\Storage;
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use Tests\TestCase;
  11. class ExportDistrictsServiceTest extends TestCase
  12. {
  13. use RefreshDatabase;
  14. protected $seed = true;
  15. public function test_export_creates_xlsx_file(): void
  16. {
  17. $user = User::factory()->create();
  18. District::factory()->create(['shortname' => 'ЦАО', 'name' => 'Центральный']);
  19. $service = new ExportDistrictsService();
  20. $link = $service->handle($user->id);
  21. $this->assertStringContainsString('export_districts_', $link);
  22. $this->assertStringContainsString('.xlsx', $link);
  23. // Cleanup
  24. $file = File::where('user_id', $user->id)->latest()->first();
  25. if ($file && file_exists($file->path)) {
  26. unlink($file->path);
  27. }
  28. }
  29. public function test_export_creates_file_record(): void
  30. {
  31. $user = User::factory()->create();
  32. District::factory()->create();
  33. $service = new ExportDistrictsService();
  34. $service->handle($user->id);
  35. $this->assertDatabaseHas('files', [
  36. 'user_id' => $user->id,
  37. 'mime_type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  38. ]);
  39. // Cleanup
  40. $file = File::where('user_id', $user->id)->latest()->first();
  41. if ($file && file_exists($file->path)) {
  42. unlink($file->path);
  43. }
  44. }
  45. public function test_export_includes_all_districts(): void
  46. {
  47. $user = User::factory()->create();
  48. // Create additional test districts
  49. District::factory()->create(['shortname' => 'ТЕСТ1', 'name' => 'Тестовый 1']);
  50. District::factory()->create(['shortname' => 'ТЕСТ2', 'name' => 'Тестовый 2']);
  51. District::factory()->create(['shortname' => 'ТЕСТ3', 'name' => 'Тестовый 3']);
  52. $totalCount = District::count();
  53. $service = new ExportDistrictsService();
  54. $link = $service->handle($user->id);
  55. // Get the file path from the link
  56. $file = File::where('user_id', $user->id)->latest()->first();
  57. $this->assertNotNull($file);
  58. // Verify the file exists and contains correct data
  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. // Check that we have correct number of data rows
  67. $highestRow = $sheet->getHighestRow();
  68. $this->assertEquals($totalCount + 1, $highestRow); // +1 for header
  69. // Cleanup
  70. unlink($file->path);
  71. }
  72. public function test_export_does_not_include_deleted_districts(): void
  73. {
  74. $user = User::factory()->create();
  75. $beforeCount = District::count();
  76. District::factory()->create(['shortname' => 'ТЕСТАКТ', 'name' => 'Тестовый активный']);
  77. $deletedDistrict = District::factory()->create(['shortname' => 'ТЕСТУДЛ', 'name' => 'Тестовый удалённый']);
  78. $deletedDistrict->delete();
  79. $afterCount = District::count(); // Should be beforeCount + 1
  80. $service = new ExportDistrictsService();
  81. $link = $service->handle($user->id);
  82. $file = File::where('user_id', $user->id)->latest()->first();
  83. $spreadsheet = IOFactory::load($file->path);
  84. $sheet = $spreadsheet->getActiveSheet();
  85. // Should have afterCount + 1 rows (header + all non-deleted districts)
  86. $highestRow = $sheet->getHighestRow();
  87. $this->assertEquals($afterCount + 1, $highestRow);
  88. // Cleanup
  89. unlink($file->path);
  90. }
  91. }