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

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

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

    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 getCompanysReturnsInitialValueForCompany(): void
    {
        $newObjectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        self::assertEquals(
            $newObjectStorage,
            $this->subject->getCompanys()
        );
    }

    /**
     * @test
     */
    public function setCompanysForObjectStorageContainingCompanySetsCompanys(): void
    {
        $company = new \Medienreaktor\Persons\Domain\Model\Company();
        $objectStorageHoldingExactlyOneCompanys = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $objectStorageHoldingExactlyOneCompanys->attach($company);
        $this->subject->setCompanys($objectStorageHoldingExactlyOneCompanys);

        self::assertAttributeEquals(
            $objectStorageHoldingExactlyOneCompanys,
            'companys',
            $this->subject
        );
    }

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

        $companysObjectStorageMock->expects(self::once())->method('attach')->with(self::equalTo($company));
        $this->inject($this->subject, 'companys', $companysObjectStorageMock);

        $this->subject->addCompany($company);
    }

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

        $companysObjectStorageMock->expects(self::once())->method('detach')->with(self::equalTo($company));
        $this->inject($this->subject, 'companys', $companysObjectStorageMock);

        $this->subject->removeCompany($company);
    }
}
