<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\Shop\Repository;

use CpCreation\VitiCore\Shop\Model\ShopConfig;
use CpCreation\VitiCore\Shop\Model\Translation\ShopConfigTranslation;
use CpCreation\VitiCore\Shop\Repository\ShopConfigRepository;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\Common\EventManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Events;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Persistence\ManagerRegistry;
use Knp\DoctrineBehaviors\Contract\Provider\LocaleProviderInterface;
use Knp\DoctrineBehaviors\EventSubscriber\TranslatableEventSubscriber;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Doctrine\UuidType;

final class ShopConfigRepositoryTest extends TestCase
{
    public function testFindOneLoadsEveryTranslation(): void
    {
        if (!Type::hasType('uuid')) {
            Type::addType('uuid', UuidType::class);
        }

        $configuration = ORMSetup::createAttributeMetadataConfiguration([
            \dirname(__DIR__, 3) . '/src/Shop/Model',
        ], true);

        $localeProvider = $this->createMock(LocaleProviderInterface::class);
        $localeProvider->method('provideCurrentLocale')->willReturn('fr');
        $localeProvider->method('provideFallbackLocale')->willReturn('en');

        $translatableSubscriber = new TranslatableEventSubscriber($localeProvider, 'LAZY', 'LAZY');
        $eventManager = new EventManager();
        $eventManager->addEventListener([
            Events::loadClassMetadata,
            Events::postLoad,
            Events::prePersist,
        ], $translatableSubscriber);

        $connection = DriverManager::getConnection([
            'driver' => 'pdo_sqlite',
            'memory' => true,
        ], $configuration, $eventManager);
        $entityManager = new EntityManager($connection, $configuration);

        $schemaTool = new SchemaTool($entityManager);
        $schemaTool->createSchema([
            $entityManager->getClassMetadata(ShopConfig::class),
            $entityManager->getClassMetadata(ShopConfigTranslation::class),
        ]);

        $config = new ShopConfig();

        foreach (['fr', 'en'] as $locale) {
            $translation = new ShopConfigTranslation();
            $translation->setLocale($locale);
            $config->addTranslation($translation);
        }

        $entityManager->persist($config);
        $entityManager->flush();
        $entityManager->clear();

        $registry = $this->createMock(ManagerRegistry::class);
        $registry->method('getManagerForClass')
            ->with(ShopConfig::class)
            ->willReturn($entityManager);

        $repository = new ShopConfigRepository($registry);
        $loadedConfig = $repository->findOne();

        self::assertNotNull($loadedConfig);
        self::assertCount(2, $loadedConfig->getTranslations());
        self::assertSame(['en', 'fr'], $loadedConfig->getTranslations()->getKeys());
    }
}
