<?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\PersonBundle\Tests\Export\Aggregator\SocialWorkAggregators;

use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Test\Export\AbstractAggregatorTest;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
use Chill\PersonBundle\Export\Aggregator\SocialWorkAggregators\ReferrerAggregator;
use Doctrine\ORM\EntityManagerInterface;

/**
 * @internal
 *
 * @coversNothing
 */
final class ReferrerAggregatorTest extends AbstractAggregatorTest
{
    private ReferrerAggregator $aggregator;

    protected function setUp(): void
    {
        self::bootKernel();

        $this->aggregator = self::getContainer()->get(ReferrerAggregator::class);
    }

    public function getAggregator()
    {
        return $this->aggregator;
    }

    public static function getFormData(): array
    {
        return [
            [], // there are previous saved export which does not contains any data
            [
                'referrer_at' => new RollingDate(RollingDate::T_TODAY),
            ],
        ];
    }

    public static function getQueryBuilders(): iterable
    {
        self::bootKernel();
        $em = self::getContainer()
            ->get(EntityManagerInterface::class);

        return [
            $em->createQueryBuilder()
                ->select('count(acpw.id)')
                ->from(AccompanyingPeriodWork::class, 'acpw'),
        ];
    }

    /**
     * @dataProvider dataProviderFormDataToNormalize
     */
    public function testDataNormalization(array $data, int $version, array $customAssert): void
    {
        $aggregator = $this->getAggregator();

        $normalized = $aggregator->normalizeFormData($data);
        $actual = $aggregator->denormalizeFormData($normalized, $version);

        self::assertIsArray($actual);
    }

    /**
     * A list of data to normalize.
     *
     * @return iterable{array}
     */
    public static function dataProviderFormDataToNormalize(): iterable
    {
        foreach (self::getFormData() as $data) {
            yield [$data, 1, []];
        }
    }
}
