<?php

namespace NoahVet\Reef\Test\A_Unit\Infrastructure\Serialization\Jane;

use NoahVet\Reef\Domain\Serialization\Json\JsonSerializerFacadeInterface;
use NoahVet\Reef\Infrastructure\Serialization\Jane\JaneDeserializer;
use NoahVet\Reef\Jane\Normalizer\JaneObjectNormalizer;
use PHPUnit\Framework\TestCase;

class JaneDeserializerTest extends TestCase
{
    private JaneDeserializer $subject;

    private JsonSerializerFacadeInterface $serializer;

    protected function setUp(): void
    {
        $this->serializer = $this->createMock(JsonSerializerFacadeInterface::class);
        $this->subject = new JaneDeserializer(serializer: $this->serializer);
    }

    public function testDeserialize(): void
    {
        $data = '{"name":"test"}';
        $expected = new \stdClass();

        $this->serializer->expects($this->once())
            ->method('deserialize')
            ->willReturn($expected)
        ;

        $result = $this->subject->deserialize(data: $data, type: \stdClass::class);

        $this->assertSame($expected, $result);
    }

    public function testDeserializeWithJaneObjectNormalizers(): void
    {
        $normalizer = new JaneObjectNormalizer();
        $subject = new JaneDeserializer(
            serializer: $this->serializer,
            janeObjectNormalizers: [$normalizer],
        );

        $data = '{"name":"test"}';
        $expected = new \stdClass();

        $this->serializer->expects($this->once())
            ->method('deserialize')
            ->with(
                data: $data,
                type: \stdClass::class,
                normalizers: [$normalizer],
            )
            ->willReturn($expected)
        ;

        $result = $subject->deserialize(data: $data, type: \stdClass::class);

        $this->assertSame($expected, $result);
    }
}
