<?php

namespace Tests\Unit\Dashboard\Stats;

use App\Dashboard\Stats\Breakdown;
use App\Dashboard\Stats\BreakdownResult;
use Tests\TestCase;

class BreakdownResultTest extends TestCase
{
    public function test_it_sums_its_rows(): void
    {
        $result = new BreakdownResult([
            new Breakdown('A', 3),
            new Breakdown('B', 4),
        ]);

        $this->assertSame(7, $result->total());
    }

    public function test_it_reports_a_zero_total_when_empty(): void
    {
        $this->assertSame(0, (new BreakdownResult([]))->total());
    }

    public function test_it_calculates_a_row_share_of_the_total(): void
    {
        $result = new BreakdownResult([
            new Breakdown('A', 3),
            new Breakdown('B', 1),
        ]);

        $this->assertSame(75.0, $result->shareOf($result->rows[0]));
        $this->assertSame(25.0, $result->shareOf($result->rows[1]));
    }

    public function test_it_reports_a_zero_share_when_nothing_was_counted(): void
    {
        $result = new BreakdownResult([new Breakdown('A', 0)]);

        $this->assertSame(0.0, $result->shareOf($result->rows[0]));
    }
}
