<?php

namespace Opentalent\OtCore\Tests\Unit\Domain\Repository;

use Opentalent\OtCore\Domain\Repository\ApiPagedCollection;
use Opentalent\OtCore\Domain\Repository\BaseApiRepository;
use Prophecy\PhpUnit\ProphecyTrait;

/**
 * Proxy to reveal the private methods of the abstract class BaseApiRepository
 * @package Opentalent\OtCore\Tests\Unit\Domain\Repository
 */
class ConcreteBaseApiRepository extends BaseApiRepository {
    protected function memberToObject(array $record): ?object { return (object)$record; }
    public function getApiFirstRecord(array $params = [], $forceUri = null): object { return parent::getApiFirstRecord($params, $forceUri); }
    public function getApiRecords($params = [], $forceUri = null): ApiPagedCollection { return parent::getApiRecords($params, $forceUri); }
}

class BaseApiRepositoryTest extends AbstractApiRepositoryTestCase
{
    use ProphecyTrait;

    const TESTED_CLASS = 'Opentalent\OtCore\Tests\Unit\Domain\Repository\ConcreteBaseApiRepository';

    /**
     * getApiFirstRecord should return the first member of the api response
     * this member has been processed by the memberToObject method, which does nothing here
     *
     * @test
     */
    public function getApiFirstRecord() {
        $base_uri = "api/public/organizations";
        $params = ['filter[where][id]' => 1];
        $processed_uri = $base_uri . "?_format=json&filter%5Bwhere%5D%5Bid%5D=1&page=1&totalItems=1&itemsPerPage=8";
        $this->injectClientFor($processed_uri);

        $actual = $this->repository->getApiFirstRecord($params, $base_uri);

        $this->assertEquals('PortailOrganization', $actual->{'@type'});
    }

    /**
     * getApiFirstRecord should return the members of the api response
     * these members has been processed by the memberToObject method, which does nothing here
     *
     * @test
     */
    public function getApiRecords() {
        $base_uri = "api/public/organizations";
        $params = ['filter[where][id]' => 1];
        $processed_uri = $base_uri . "?_format=json&filter%5Bwhere%5D%5Bid%5D=1&itemsPerPage=8";
        $this->injectClientFor($processed_uri);

        $actual = $this->repository->getApiRecords($params, $base_uri);

        $this->assertEquals('PortailOrganization', $actual->getMembers()[0]->{'@type'});
    }
}
