<?php

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

use Opentalent\OtCore\Domain\Repository\OrganizationRepository;
use Prophecy\PhpUnit\ProphecyTrait;


class OrganizationRepositoryTest extends AbstractApiRepositoryTestCase
{
    use ProphecyTrait;

    const TESTED_CLASS = "Opentalent\OtCore\Domain\Repository\OrganizationRepository";

    /**
     * findById should return an the Organization matching the given id
     *
     * @test
     */
    public function findById() {
        $organization_id = 1;

        $expected_uri = "api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1&page=1&totalItems=1&itemsPerPage=8";
        $this->injectClientFor($expected_uri);

        $actual = $this->repository->findById($organization_id);
        $this->assertEquals($organization_id, $actual->getId());
    }

    /**
     * findById should return an the Organization matching the given id
     *
     * @test
     */
    public function findByName() {
        $name = 'a name';

        $expected_uri = "api/public/organizations?_format=json&filter%5Bwhere%5D%5Bname%5D=a+name&page=1&totalItems=1&itemsPerPage=8";
        $this->injectClientFor($expected_uri);

        $actual = $this->repository->findByName($name);
        $this->assertEquals(1, $actual->getId());
    }

    /**
     * findById should return an the Organization matching the given id
     *
     * @test
     */
    public function findByInexistantName() {
        $name = 'a unknown name';

        $expected_uri = "api/public/organizations?_format=json&filter%5Bwhere%5D%5Bname%5D=a+unknown+name&page=1&totalItems=1&itemsPerPage=8";
        $this->injectClientFor($expected_uri);

        $actual = $this->repository->findByName($name);
        $this->assertEquals(1, $actual->getId());
    }

    /**
     * findById should return an the Organization matching the given id
     *
     * @test
     */
    public function findChildrenById() {
        $organization_id = 1;

        $expected_uri = "api/public/organizations?_format=json&parentId=1&children=1&page=1&itemsPerPage=8";
        $this->injectClientFor($expected_uri);

        $actual = $this->repository->findChildrenById($organization_id);
        $this->assertEquals($organization_id, $actual->getMembers()[0]->getId());
    }

    /**
     * memberToObject should return null if the given
     * member has not the good type
     *
     * @test
     */
    public function memberToObjectError() {
        $this->assertEquals(
            null,
            $this->callMemberToObject(['@type' => 'Invalid'])
        );
    }
}
