<?php

namespace Opentalent\OtCore\Tests\Unit\Domain;

use AssertionError;
use Nimut\TestingFramework\TestCase\UnitTestCase;
use Opentalent\OtCore\Domain\Model\Member;

class MemberTest extends UnitTestCase
{
    /**
     * Object should instantiate correctly, and properties
     * set with setters should be retrieved by getters
     *
     * @test
     */
    public function instantiation() {
        $member = new Member();

        $member->setId(1);
        $member->setOrganizationId(2);
        $member->setGender('M');
        $member->setGivenName('given name');
        $member->setName('name');
        $member->setStartDate(new \DateTime('2021-01-01'));
        $member->setEndDate(new \DateTime('2021-01-01'));
        $member->setInstrumentGroup('instrument group');
        $member->setInstrument('instrument');
        $member->setMission('mission');
        $member->setPersonId(3);
        $member->setImage('image');

        $this->assertEquals(1, $member->getId());
        $this->assertEquals(2, $member->getOrganizationId());
        $this->assertEquals('M', $member->getGender());
        $this->assertEquals('given name', $member->getGivenName());
        $this->assertEquals('name', $member->getName());
        $this->assertEquals(new \DateTime('2021-01-01'), $member->getStartDate());
        $this->assertEquals(new \DateTime('2021-01-01'), $member->getEndDate());
        $this->assertEquals('instrument group', $member->getInstrumentGroup());
        $this->assertEquals('instrument', $member->getInstrument());
        $this->assertEquals('mission', $member->getMission());
        $this->assertEquals(3, $member->getPersonId());
        $this->assertEquals('image', $member->getImage());
    }

    /**
     * Nullable fields should be able to be set to null,
     * while non nullable shall raise an exception if set to null
     *
     * @test
     */
    public function nullableOrNot() {

        $member = new Member();

        // non-nullable properties
        try {
            $member->setId(null);
            throw new AssertionError('Member::setId should not accept a null value');
        } catch (\TypeError $e) {}
        try {
            $member->setOrganizationId(null);
            throw new AssertionError('Member::setOrganizationId should not accept a null value');
        } catch (\TypeError $e) {}
        try {
            $member->setPersonId(null);
            throw new AssertionError('Member::setPersonId should not accept a null value');
        } catch (\TypeError $e) {}

        // nullable properties
        $member->setGender(null);
        $member->setGivenName(null);
        $member->setName(null);
        $member->setStartDate(null);
        $member->setEndDate(null);
        $member->setInstrumentGroup(null);
        $member->setInstrument(null);
        $member->setMission(null);
        $member->setImage(null);

        // Just to avoid this test to be considered as risky
        $this->assertEquals(1, 1);
    }

    /**
     * getFullName should return the concatenated name of the member,
     * regarding which informations are available
     *
     * @test
     */
    public function getFullNameWithNameAndGivenName() {
        $member = new Member();
        $member->setGivenName('Benedict');
        $member->setName('Comcomber');
        $this->assertEquals(
            "Benedict Comcomber",
            $member->getFullName()
        );
    }

    /**
     * getFullName should return the concatenated name of the member,
     * regarding which informations are available
     *
     * @test
     */
    public function getFullNameWithNameAndGender() {
        $member = new Member();
        $member->setName('Comcomber');
        $this->assertEquals(
            "M. Comcomber",
            $member->getFullName()
        );
    }

    /**
     * getFullName should return the concatenated name of the member,
     * regarding which informations are available
     *
     * @test
     */
    public function getFullNameWithNoInformation() {
        $member = new Member();
        $this->assertEquals(
            "",
            $member->getFullName()
        );
    }
}
