<?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\TicketBundle\Tests\Serializer\Normalizer;

use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\TicketBundle\Action\Ticket\SetAddresseesCommand;
use Chill\TicketBundle\Serializer\Normalizer\SetAddresseesCommandDenormalizer;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

/**
 * @internal
 *
 * @coversNothing
 */
class SetAddresseesCommandDenormalizerTest extends TestCase
{
    use ProphecyTrait;

    public function testSupportsDenormalization()
    {
        $denormalizer = new SetAddresseesCommandDenormalizer();

        self::assertTrue($denormalizer->supportsDenormalization('', SetAddresseesCommand::class, 'json'));
        self::assertFalse($denormalizer->supportsDenormalization('', stdClass::class, 'json'));
    }

    public function testDenormalize()
    {
        $denormalizer = $this->buildDenormalizer();

        $actual = $denormalizer->denormalize(['addressees' => [['type' => 'user'], ['type' => 'user_group']]], SetAddresseesCommand::class, 'json');

        self::assertInstanceOf(SetAddresseesCommand::class, $actual);
        self::assertIsArray($actual->addressees);
        self::assertCount(2, $actual->addressees);
        self::assertInstanceOf(User::class, $actual->addressees[0]);
        self::assertInstanceOf(UserGroup::class, $actual->addressees[1]);
    }

    private function buildDenormalizer(): SetAddresseesCommandDenormalizer
    {
        $normalizer = $this->prophesize(DenormalizerInterface::class);
        $normalizer->denormalize(Argument::any(), User::class, 'json', Argument::any())
            ->willReturn(new User());
        $normalizer->denormalize(Argument::any(), UserGroup::class, 'json', Argument::any())
            ->willReturn(new UserGroup());

        $denormalizer = new SetAddresseesCommandDenormalizer();
        $denormalizer->setDenormalizer($normalizer->reveal());

        return $denormalizer;
    }
}
