ReportControllerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Order;
  4. use App\Models\Reclamation;
  5. use App\Models\ReclamationDetail;
  6. use App\Models\Role;
  7. use App\Models\SparePart;
  8. use App\Models\User;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Tests\TestCase;
  11. class ReportControllerTest extends TestCase
  12. {
  13. use RefreshDatabase;
  14. protected bool $seed = true;
  15. protected User $adminUser;
  16. protected User $managerUser;
  17. protected User $brigadierUser;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  22. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  23. $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
  24. }
  25. public function test_guest_cannot_access_reports_index(): void
  26. {
  27. $response = $this->get(route('reports.index'));
  28. $response->assertRedirect(route('login'));
  29. }
  30. public function test_brigadier_cannot_access_reports(): void
  31. {
  32. $response = $this->actingAs($this->brigadierUser)->get(route('reports.index'));
  33. $response->assertStatus(403);
  34. }
  35. public function test_admin_can_access_reports_index(): void
  36. {
  37. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  38. $response->assertStatus(200);
  39. $response->assertViewIs('reports.index');
  40. }
  41. public function test_manager_can_access_reports_index(): void
  42. {
  43. $response = $this->actingAs($this->managerUser)->get(route('reports.index'));
  44. $response->assertStatus(200);
  45. $response->assertViewIs('reports.index');
  46. }
  47. public function test_reports_index_contains_required_view_data(): void
  48. {
  49. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  50. $response->assertViewHas('totalOrders');
  51. $response->assertViewHas('totalMafs');
  52. $response->assertViewHas('managers');
  53. }
  54. public function test_reports_index_has_done_orders_count(): void
  55. {
  56. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  57. $response->assertViewHas('doneOrders', fn($v) => $v >= 0);
  58. }
  59. public function test_reports_index_has_mount_orders_count(): void
  60. {
  61. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  62. $response->assertViewHas('mountOrders', fn($v) => $v >= 0);
  63. }
  64. public function test_reports_index_has_reclamations_data(): void
  65. {
  66. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  67. $response->assertViewHas('totalReclamations');
  68. $response->assertViewHas('reclamationStatuses');
  69. }
  70. public function test_reports_index_aggregates_reclamation_details_and_spare_parts(): void
  71. {
  72. $order = Order::factory()->create(['user_id' => $this->adminUser->id]);
  73. $reclamation = Reclamation::factory()->create([
  74. 'order_id' => $order->id,
  75. 'user_id' => $this->adminUser->id,
  76. ]);
  77. ReclamationDetail::query()->create([
  78. 'reclamation_id' => $reclamation->id,
  79. 'name' => 'Петля',
  80. 'quantity' => 2,
  81. ]);
  82. $sparePart = SparePart::factory()->create([
  83. 'article' => 'SP-REPORT-001',
  84. ]);
  85. $reclamation->spareParts()->attach($sparePart->id, [
  86. 'quantity' => 3,
  87. 'with_documents' => false,
  88. 'status' => 'reserved',
  89. 'reserved_qty' => 3,
  90. 'issued_qty' => 0,
  91. ]);
  92. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  93. $response->assertViewHas('reclamationDetails', function (array $details) {
  94. return ($details['Петля'] ?? null) === 2
  95. && ($details['SP-REPORT-001'] ?? null) === 3;
  96. });
  97. }
  98. public function test_reports_index_has_by_district_data(): void
  99. {
  100. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  101. $response->assertViewHas('byDistrict', fn($v) => is_array($v));
  102. }
  103. public function test_reports_index_has_total_sum(): void
  104. {
  105. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  106. $response->assertViewHas('totalSum');
  107. $response->assertViewHas('totalDoneSum');
  108. }
  109. }