<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\MainBundle\Tests\Services\Regroupement;

use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Regroupment;
use Chill\MainBundle\Service\Regroupement\RegroupementFiltering;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 *
 * @coversNothing
 */
class RegroupementFilteringTest extends TestCase
{
    private static RegroupementFiltering $regroupementFiltering;

    public static function setUpBeforeClass(): void
    {
        self::$regroupementFiltering = new RegroupementFiltering();
    }

    /**
     * @dataProvider provideDataForFilterContainsAtLeastOnCenter
     */
    public function testFilterContainsAtLeastOnCenter(array $groups, array $centers, array $expected): void
    {
        $actual = self::$regroupementFiltering->filterContainsAtLeastOneCenter($groups, $centers);

        self::assertEquals(count($expected), count($actual));
        self::assertTrue(array_is_list($actual));

        foreach ($expected as $center) {
            self::assertContains($center, $actual);
        }
    }

    public static function provideDataForFilterContainsAtLeastOnCenter(): iterable
    {

        $centerA = new Center();
        $centerB = new Center();
        $centerC = new Center();
        $centerD = new Center();

        $groupA = new Regroupment();
        $groupA->addCenter($centerA)->addCenter($centerB);

        $groupB = new Regroupment();
        $groupB->addCenter($centerA)->addCenter($centerB)->addCenter($centerC);

        $groupC = new Regroupment();
        $groupC->addCenter($centerA)->addCenter($centerD);

        yield [
            [$groupA, $groupB],
            [],
            [],
        ];

        yield [
            [$groupA, $groupB],
            [$centerA, $centerB, $centerC],
            [$groupA, $groupB],
        ];

        yield [
            [$groupA, $groupC],
            [$centerD],
            [$groupC],
        ];

        yield [
            [$groupA],
            [$centerB, $centerD],
            [$groupA],
        ];

        yield [
            [$groupA],
            [new Center()],
            [],
        ];

    }
}
