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'); } public function test_reports_index_counts_done_sum_only_for_handed_over_sites(): void { $handedOverOrder = Order::factory()->create([ 'user_id' => $this->adminUser->id, 'order_status_id' => Order::STATUS_HANDED_OVER, ]); $handedOverProduct = Product::factory()->create([ 'total_price' => 1000, ]); ProductSKU::factory() ->forOrder($handedOverOrder) ->forProduct($handedOverProduct) ->create(); $inMountOrder = Order::factory()->create([ 'user_id' => $this->adminUser->id, 'order_status_id' => Order::STATUS_IN_MOUNT, ]); $inMountProduct = Product::factory()->create([ 'total_price' => 2000, ]); ProductSKU::factory() ->forOrder($inMountOrder) ->forProduct($inMountProduct) ->create(); $handedOverWithNotesOrder = Order::factory()->create([ 'user_id' => $this->adminUser->id, 'order_status_id' => Order::STATUS_HANDED_OVER_WITH_NOTES, ]); $handedOverWithNotesProduct = Product::factory()->create([ 'total_price' => 3000, ]); ProductSKU::factory() ->forOrder($handedOverWithNotesOrder) ->forProduct($handedOverWithNotesProduct) ->create(); $response = $this->actingAs($this->adminUser)->get(route('reports.index')); $expectedSum = Price::format(1000); $response->assertViewHas('totalHandedOverSum', $expectedSum); $response->assertViewHas('totalDoneSum', $expectedSum); $response->assertViewHas('byDistrict', function (array $byDistrict) use ($handedOverOrder, $expectedSum) { $districtName = $handedOverOrder->district->shortname; return ($byDistrict[$districtName]['doneSum'] ?? null) === $expectedSum; }); } }