Done!

Unit Test - asserting Array Collection of Object against expected json Data



namespace Converter\Filters;

use CalendarResult;
use DateOfCalendar;


class FilterResultsOfByNights extends TestCase
{
    /** @var FilterResultsOfByNights */
    private $sut;

    /** @var CalendarResult */
    private $providedCalendarResults;

    public function setUp()
    {
        parent::setUp();

        $this->serializer = SerializerBuilder::create()->setPropertyNamingStrategy(
            new SerializedNameAnnotationStrategy(
                new IdenticalPropertyNamingStrategy()
            )
        )->build();

        $this->providedCalendarResults = new CalendarResult();

        $this->sut = new FilterCalendarResults();

    /**
     * @test
     */
    public function it_returns_original_calendar_result(): void
    {
        $expectedSerializedResult = '
            {
               "01 Dec 2022":{
                  "date":"01 Dec 2022",
                  "roomToBook":50,
                  "availability":"yes"
                  },
               "02 Dec 2022":{
                  "date":"02 Dec 2022",
                  "roomToBook":20,
                  "availability":"yes"
               }
            }
        ';

        // Given
        $providedDateOne = (new DateOfCalendar())
            ->setDate('01 Dec 2022')
            ->setRoomToBook(50)
            ->setAvailability('bookable_rooms')
        ;

        $providedDateTwo = (new DateOfCalendar())
            ->setDate('02 Dec 2022')
            ->setRoomToBook(20)
            ->setAvailability('bookable_rooms')
        ;

        $providedNights = 1;

        $this->providedCalendarResults->addDate($providedDateOne);
        $this->providedCalendarResults->addDate($providedDateTwo);

        // say actual result is collection of DateOfCalendar objet where property are private
        $actualResults = $this->sut->filter($this->providedCalendarResults, $providedNights);

        self::assertCount(2, $actualResults->getDatesArray());

        self::assertSame(
            $this->convertJsonToArray($expectedSerializedResult),
            // json_ecode wont encode provate property so need to use serializer
            $this->passThroughSerializer($actualResults->getDatesArray())
        );
    }

    private function convertJsonToArray(string $jsonData): array
    {
        return json_decode(
            $jsonData,
            true
        );
    }


    private function passThroughSerializer($value): array
    {
        $jsonData = $this->serializer->serialize($value, 'json');
        return $this->serializer->deserialize(
            $jsonData, 'array', 'json'
        );
    }
}