| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace Tests\Feature;
- use App\Models\Order;
- use App\Models\Reclamation;
- use App\Models\ReclamationDetail;
- use App\Models\Role;
- use App\Models\SparePart;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class ReportControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected bool $seed = true;
- protected User $adminUser;
- protected User $managerUser;
- protected User $brigadierUser;
- protected function setUp(): void
- {
- parent::setUp();
- $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
- $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
- $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
- }
- public function test_guest_cannot_access_reports_index(): void
- {
- $response = $this->get(route('reports.index'));
- $response->assertRedirect(route('login'));
- }
- public function test_brigadier_cannot_access_reports(): void
- {
- $response = $this->actingAs($this->brigadierUser)->get(route('reports.index'));
- $response->assertStatus(403);
- }
- public function test_admin_can_access_reports_index(): void
- {
- $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
- $response->assertStatus(200);
- $response->assertViewIs('reports.index');
- }
- public function test_manager_can_access_reports_index(): void
- {
- $response = $this->actingAs($this->managerUser)->get(route('reports.index'));
- $response->assertStatus(200);
- $response->assertViewIs('reports.index');
- }
- public function test_reports_index_contains_required_view_data(): void
- {
- $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
- $response->assertViewHas('totalOrders');
- $response->assertViewHas('totalMafs');
- $response->assertViewHas('managers');
- }
- public function test_reports_index_has_done_orders_count(): void
- {
- $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
- $response->assertViewHas('doneOrders', fn($v) => $v >= 0);
- }
- public function test_reports_index_has_mount_orders_count(): void
- {
- $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
- $response->assertViewHas('mountOrders', fn($v) => $v >= 0);
- }
- public function test_reports_index_has_reclamations_data(): void
- {
- $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
- $response->assertViewHas('totalReclamations');
- $response->assertViewHas('reclamationStatuses');
- }
- public function test_reports_index_aggregates_reclamation_details_and_spare_parts(): void
- {
- $order = Order::factory()->create(['user_id' => $this->adminUser->id]);
- $reclamation = Reclamation::factory()->create([
- 'order_id' => $order->id,
- 'user_id' => $this->adminUser->id,
- ]);
- ReclamationDetail::query()->create([
- 'reclamation_id' => $reclamation->id,
- 'name' => 'Петля',
- 'quantity' => 2,
- ]);
- $sparePart = SparePart::factory()->create([
- 'article' => 'SP-REPORT-001',
- ]);
- $reclamation->spareParts()->attach($sparePart->id, [
- 'quantity' => 3,
- 'with_documents' => false,
- 'status' => 'reserved',
- 'reserved_qty' => 3,
- 'issued_qty' => 0,
- ]);
- $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
- $response->assertViewHas('reclamationDetails', function (array $details) {
- return ($details['Петля'] ?? null) === 2
- && ($details['SP-REPORT-001'] ?? null) === 3;
- });
- }
- public function test_reports_index_has_by_district_data(): void
- {
- $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
- $response->assertViewHas('byDistrict', fn($v) => is_array($v));
- }
- public function test_reports_index_has_total_sum(): void
- {
- $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
- $response->assertViewHas('totalSum');
- $response->assertViewHas('totalDoneSum');
- }
- }
|