<?php
namespace Medienreaktor\Persons\Tests\Unit\Domain\Model;

/**
 * Test case.
 *
 * @author Tobias Lorz <tobias.lorz@yahoo.de>
 */
class CompanyTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
{
    /**
     * @var \Medienreaktor\Persons\Domain\Model\Company
     */
    protected $subject = null;

    protected function setUp()
    {
        parent::setUp();
        $this->subject = new \Medienreaktor\Persons\Domain\Model\Company();
    }

    protected function tearDown()
    {
        parent::tearDown();
    }

    /**
     * @test
     */
    public function getTitleReturnsInitialValueForString(): void
    {
        self::assertSame(
            '',
            $this->subject->getTitle()
        );
    }

    /**
     * @test
     */
    public function setTitleForStringSetsTitle(): void
    {
        $this->subject->setTitle('Conceived at T3CON10');

        self::assertAttributeEquals(
            'Conceived at T3CON10',
            'title',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function getDescriptionReturnsInitialValueForString(): void
    {
        self::assertSame(
            '',
            $this->subject->getDescription()
        );
    }

    /**
     * @test
     */
    public function setDescriptionForStringSetsDescription(): void
    {
        $this->subject->setDescription('Conceived at T3CON10');

        self::assertAttributeEquals(
            'Conceived at T3CON10',
            'description',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function getImageReturnsInitialValueForFileReference(): void
    {
        self::assertEquals(
            null,
            $this->subject->getImage()
        );
    }

    /**
     * @test
     */
    public function setImageForFileReferenceSetsImage(): void
    {
        $fileReferenceFixture = new \TYPO3\CMS\Extbase\Domain\Model\FileReference();
        $this->subject->setImage($fileReferenceFixture);

        self::assertAttributeEquals(
            $fileReferenceFixture,
            'image',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function getPhoneReturnsInitialValueForString(): void
    {
        self::assertSame(
            '',
            $this->subject->getPhone()
        );
    }

    /**
     * @test
     */
    public function setPhoneForStringSetsPhone(): void
    {
        $this->subject->setPhone('Conceived at T3CON10');

        self::assertAttributeEquals(
            'Conceived at T3CON10',
            'phone',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function getFaxReturnsInitialValueForString(): void
    {
        self::assertSame(
            '',
            $this->subject->getFax()
        );
    }

    /**
     * @test
     */
    public function setFaxForStringSetsFax(): void
    {
        $this->subject->setFax('Conceived at T3CON10');

        self::assertAttributeEquals(
            'Conceived at T3CON10',
            'fax',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function getMailReturnsInitialValueForString(): void
    {
        self::assertSame(
            '',
            $this->subject->getMail()
        );
    }

    /**
     * @test
     */
    public function setMailForStringSetsMail(): void
    {
        $this->subject->setMail('Conceived at T3CON10');

        self::assertAttributeEquals(
            'Conceived at T3CON10',
            'mail',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function getServicesReturnsInitialValueFor(): void
    {
        $newObjectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        self::assertEquals(
            $newObjectStorage,
            $this->subject->getServices()
        );
    }

    /**
     * @test
     */
    public function getDepartmentsReturnsInitialValueForDepartment(): void
    {
        $newObjectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        self::assertEquals(
            $newObjectStorage,
            $this->subject->getDepartments()
        );
    }

    /**
     * @test
     */
    public function setDepartmentsForObjectStorageContainingDepartmentSetsDepartments(): void
    {
        $department = new \Medienreaktor\Persons\Domain\Model\Department();
        $objectStorageHoldingExactlyOneDepartments = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $objectStorageHoldingExactlyOneDepartments->attach($department);
        $this->subject->setDepartments($objectStorageHoldingExactlyOneDepartments);

        self::assertAttributeEquals(
            $objectStorageHoldingExactlyOneDepartments,
            'departments',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function addDepartmentToObjectStorageHoldingDepartments(): void
    {
        $department = new \Medienreaktor\Persons\Domain\Model\Department();
        $departmentsObjectStorageMock = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\ObjectStorage::class)
            ->setMethods(['attach'])
            ->disableOriginalConstructor()
            ->getMock();

        $departmentsObjectStorageMock->expects(self::once())->method('attach')->with(self::equalTo($department));
        $this->inject($this->subject, 'departments', $departmentsObjectStorageMock);

        $this->subject->addDepartment($department);
    }

    /**
     * @test
     */
    public function removeDepartmentFromObjectStorageHoldingDepartments(): void
    {
        $department = new \Medienreaktor\Persons\Domain\Model\Department();
        $departmentsObjectStorageMock = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\ObjectStorage::class)
            ->setMethods(['detach'])
            ->disableOriginalConstructor()
            ->getMock();

        $departmentsObjectStorageMock->expects(self::once())->method('detach')->with(self::equalTo($department));
        $this->inject($this->subject, 'departments', $departmentsObjectStorageMock);

        $this->subject->removeDepartment($department);
    }

    /**
     * @test
     */
    public function getPersonsReturnsInitialValueForPerson(): void
    {
        $newObjectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        self::assertEquals(
            $newObjectStorage,
            $this->subject->getPersons()
        );
    }

    /**
     * @test
     */
    public function setPersonsForObjectStorageContainingPersonSetsPersons(): void
    {
        $person = new \Medienreaktor\Persons\Domain\Model\Person();
        $objectStorageHoldingExactlyOnePersons = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $objectStorageHoldingExactlyOnePersons->attach($person);
        $this->subject->setPersons($objectStorageHoldingExactlyOnePersons);

        self::assertAttributeEquals(
            $objectStorageHoldingExactlyOnePersons,
            'persons',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function addPersonToObjectStorageHoldingPersons(): void
    {
        $person = new \Medienreaktor\Persons\Domain\Model\Person();
        $personsObjectStorageMock = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\ObjectStorage::class)
            ->setMethods(['attach'])
            ->disableOriginalConstructor()
            ->getMock();

        $personsObjectStorageMock->expects(self::once())->method('attach')->with(self::equalTo($person));
        $this->inject($this->subject, 'persons', $personsObjectStorageMock);

        $this->subject->addPerson($person);
    }

    /**
     * @test
     */
    public function removePersonFromObjectStorageHoldingPersons(): void
    {
        $person = new \Medienreaktor\Persons\Domain\Model\Person();
        $personsObjectStorageMock = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\ObjectStorage::class)
            ->setMethods(['detach'])
            ->disableOriginalConstructor()
            ->getMock();

        $personsObjectStorageMock->expects(self::once())->method('detach')->with(self::equalTo($person));
        $this->inject($this->subject, 'persons', $personsObjectStorageMock);

        $this->subject->removePerson($person);
    }

    /**
     * @test
     */
    public function getAddressesReturnsInitialValueForAddress(): void
    {
        $newObjectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        self::assertEquals(
            $newObjectStorage,
            $this->subject->getAddresses()
        );
    }

    /**
     * @test
     */
    public function setAddressesForObjectStorageContainingAddressSetsAddresses(): void
    {
        $address = new \Medienreaktor\Persons\Domain\Model\Address();
        $objectStorageHoldingExactlyOneAddresses = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $objectStorageHoldingExactlyOneAddresses->attach($address);
        $this->subject->setAddresses($objectStorageHoldingExactlyOneAddresses);

        self::assertAttributeEquals(
            $objectStorageHoldingExactlyOneAddresses,
            'addresses',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function addAddressToObjectStorageHoldingAddresses(): void
    {
        $address = new \Medienreaktor\Persons\Domain\Model\Address();
        $addressesObjectStorageMock = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\ObjectStorage::class)
            ->setMethods(['attach'])
            ->disableOriginalConstructor()
            ->getMock();

        $addressesObjectStorageMock->expects(self::once())->method('attach')->with(self::equalTo($address));
        $this->inject($this->subject, 'addresses', $addressesObjectStorageMock);

        $this->subject->addAddress($address);
    }

    /**
     * @test
     */
    public function removeAddressFromObjectStorageHoldingAddresses(): void
    {
        $address = new \Medienreaktor\Persons\Domain\Model\Address();
        $addressesObjectStorageMock = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\ObjectStorage::class)
            ->setMethods(['detach'])
            ->disableOriginalConstructor()
            ->getMock();

        $addressesObjectStorageMock->expects(self::once())->method('detach')->with(self::equalTo($address));
        $this->inject($this->subject, 'addresses', $addressesObjectStorageMock);

        $this->subject->removeAddress($address);
    }

    /**
     * @test
     */
    public function getOrganisationReturnsInitialValueForOrganisation(): void
    {
        self::assertEquals(
            null,
            $this->subject->getOrganisation()
        );
    }

    /**
     * @test
     */
    public function setOrganisationForOrganisationSetsOrganisation(): void
    {
        $organisationFixture = new \Medienreaktor\Persons\Domain\Model\Organisation();
        $this->subject->setOrganisation($organisationFixture);

        self::assertAttributeEquals(
            $organisationFixture,
            'organisation',
            $this->subject
        );
    }

    /**
     * @test
     */
    public function getMainAddressReturnsInitialValueForAddress(): void
    {
        self::assertEquals(
            null,
            $this->subject->getMainAddress()
        );
    }

    /**
     * @test
     */
    public function setMainAddressForAddressSetsMainAddress(): void
    {
        $mainAddressFixture = new \Medienreaktor\Persons\Domain\Model\Address();
        $this->subject->setMainAddress($mainAddressFixture);

        self::assertAttributeEquals(
            $mainAddressFixture,
            'mainAddress',
            $this->subject
        );
    }
}
