<?php

namespace Tests\Unit\Domain\Source;

use Infinito\Domain\Source\SourceMemberManager;
use Infinito\Domain\Source\SourceMemberManagerInterface;
use Infinito\Entity\Source\PureSource;
use Infinito\Entity\Source\SourceInterface;
use PHPUnit\Framework\TestCase;

class SourceMemberManagerTest extends TestCase
{
    /**
     * @var SourceInterface
     */
    private $source;

    /**
     * @var SourceMemberManagerInterface
     */
    private $sourceMemberManager;

    public function setUp(): void
    {
        $this->source = new PureSource();
        $this->sourceMemberManager = new SourceMemberManager($this->source);
    }

    public function testAddAndRemoveMember(): void
    {
        $member = new PureSource();
        $this->assertNull($this->sourceMemberManager->addMember($member));
        $this->assertEquals($member, $this->source->getMemberRelation()->getMembers()->get(0)->getSource());
        $this->assertEquals($this->source, $member->getMemberRelation()->getMemberships()->get(0)->getSource());
        $this->assertNull($this->sourceMemberManager->removeMember($member));
        $this->assertEquals(0, $this->source->getMemberRelation()->getMembers()->count());
        $this->assertEquals(0, $member->getMemberRelation()->getMemberships()->count());
    }

    public function testAddAndRemoveMembership(): void
    {
        $membership = new PureSource();
        $this->assertNull($this->sourceMemberManager->addMembership($membership));
        $this->assertEquals($membership, $this->source->getMemberRelation()->getMemberships()->get(0)->getSource());
        $this->assertEquals($this->source, $membership->getMemberRelation()->getMembers()->get(0)->getSource());
        $this->assertNull($this->sourceMemberManager->removeMembership($membership));
        $this->assertEquals(0, $this->source->getMemberRelation()->getMemberships()->count());
        $this->assertEquals(0, $membership->getMemberRelation()->getMembers()->count());
    }
}
