ReportControllerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Helpers\Price;
  4. use App\Models\Order;
  5. use App\Models\Product;
  6. use App\Models\ProductSKU;
  7. use App\Models\Reclamation;
  8. use App\Models\ReclamationDetail;
  9. use App\Models\Role;
  10. use App\Models\SparePart;
  11. use App\Models\User;
  12. use Illuminate\Foundation\Testing\RefreshDatabase;
  13. use Tests\TestCase;
  14. class ReportControllerTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. protected bool $seed = true;
  18. protected User $adminUser;
  19. protected User $managerUser;
  20. protected User $brigadierUser;
  21. protected function setUp(): void
  22. {
  23. parent::setUp();
  24. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  25. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  26. $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
  27. }
  28. public function test_guest_cannot_access_reports_index(): void
  29. {
  30. $response = $this->get(route('reports.index'));
  31. $response->assertRedirect(route('login'));
  32. }
  33. public function test_brigadier_cannot_access_reports(): void
  34. {
  35. $response = $this->actingAs($this->brigadierUser)->get(route('reports.index'));
  36. $response->assertStatus(403);
  37. }
  38. public function test_admin_can_access_reports_index(): void
  39. {
  40. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  41. $response->assertStatus(200);
  42. $response->assertViewIs('reports.index');
  43. }
  44. public function test_manager_can_access_reports_index(): void
  45. {
  46. $response = $this->actingAs($this->managerUser)->get(route('reports.index'));
  47. $response->assertStatus(200);
  48. $response->assertViewIs('reports.index');
  49. }
  50. public function test_reports_index_contains_required_view_data(): void
  51. {
  52. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  53. $response->assertViewHas('totalOrders');
  54. $response->assertViewHas('totalMafs');
  55. $response->assertViewHas('managers');
  56. }
  57. public function test_reports_index_has_done_orders_count(): void
  58. {
  59. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  60. $response->assertViewHas('doneOrders', fn($v) => $v >= 0);
  61. }
  62. public function test_reports_index_has_mount_orders_count(): void
  63. {
  64. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  65. $response->assertViewHas('mountOrders', fn($v) => $v >= 0);
  66. }
  67. public function test_reports_index_has_reclamations_data(): void
  68. {
  69. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  70. $response->assertViewHas('totalReclamations');
  71. $response->assertViewHas('reclamationStatuses');
  72. }
  73. public function test_reports_index_aggregates_reclamation_details_and_spare_parts(): void
  74. {
  75. $order = Order::factory()->create(['user_id' => $this->adminUser->id]);
  76. $reclamation = Reclamation::factory()->create([
  77. 'order_id' => $order->id,
  78. 'user_id' => $this->adminUser->id,
  79. ]);
  80. ReclamationDetail::query()->create([
  81. 'reclamation_id' => $reclamation->id,
  82. 'name' => 'Петля',
  83. 'quantity' => 2,
  84. ]);
  85. $sparePart = SparePart::factory()->create([
  86. 'article' => 'SP-REPORT-001',
  87. ]);
  88. $reclamation->spareParts()->attach($sparePart->id, [
  89. 'quantity' => 3,
  90. 'with_documents' => false,
  91. 'status' => 'reserved',
  92. 'reserved_qty' => 3,
  93. 'issued_qty' => 0,
  94. ]);
  95. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  96. $response->assertViewHas('reclamationDetails', function (array $details) {
  97. return ($details['Петля'] ?? null) === 2
  98. && ($details['SP-REPORT-001'] ?? null) === 3;
  99. });
  100. }
  101. public function test_reports_index_has_by_district_data(): void
  102. {
  103. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  104. $response->assertViewHas('byDistrict', fn($v) => is_array($v));
  105. }
  106. public function test_reports_index_has_total_sum(): void
  107. {
  108. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  109. $response->assertViewHas('totalSum');
  110. $response->assertViewHas('totalDoneSum');
  111. }
  112. public function test_reports_index_counts_done_sum_only_for_handed_over_sites(): void
  113. {
  114. $handedOverOrder = Order::factory()->create([
  115. 'user_id' => $this->adminUser->id,
  116. 'order_status_id' => Order::STATUS_HANDED_OVER,
  117. ]);
  118. $handedOverProduct = Product::factory()->create([
  119. 'total_price' => 1000,
  120. ]);
  121. ProductSKU::factory()
  122. ->forOrder($handedOverOrder)
  123. ->forProduct($handedOverProduct)
  124. ->create();
  125. $inMountOrder = Order::factory()->create([
  126. 'user_id' => $this->adminUser->id,
  127. 'order_status_id' => Order::STATUS_IN_MOUNT,
  128. ]);
  129. $inMountProduct = Product::factory()->create([
  130. 'total_price' => 2000,
  131. ]);
  132. ProductSKU::factory()
  133. ->forOrder($inMountOrder)
  134. ->forProduct($inMountProduct)
  135. ->create();
  136. $handedOverWithNotesOrder = Order::factory()->create([
  137. 'user_id' => $this->adminUser->id,
  138. 'order_status_id' => Order::STATUS_HANDED_OVER_WITH_NOTES,
  139. ]);
  140. $handedOverWithNotesProduct = Product::factory()->create([
  141. 'total_price' => 3000,
  142. ]);
  143. ProductSKU::factory()
  144. ->forOrder($handedOverWithNotesOrder)
  145. ->forProduct($handedOverWithNotesProduct)
  146. ->create();
  147. $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
  148. $expectedSum = Price::format(1000);
  149. $response->assertViewHas('totalHandedOverSum', $expectedSum);
  150. $response->assertViewHas('totalDoneSum', $expectedSum);
  151. $response->assertViewHas('byDistrict', function (array $byDistrict) use ($handedOverOrder, $expectedSum) {
  152. return ($byDistrict[$handedOverOrder->district_id]['doneSum'] ?? null) === $expectedSum;
  153. });
  154. }
  155. }