<?php

/** @noinspection PhpUnhandledExceptionInspection */

namespace App\Tests\Unit\Service\Organization;

use App\Entity\Network\NetworkOrganization;
use App\Entity\Organization\Organization;
use App\Entity\Organization\Parameters;
use App\Entity\Organization\Settings;
use App\Entity\Organization\Subdomain;
use App\Enum\Organization\OrganizationIdsEnum;
use App\Enum\Organization\SettingsProductEnum;
use App\Repository\Network\NetworkOrganizationRepository;
use App\Service\Network\Utils as NetworkUtils;
use App\Service\Organization\Utils as OrganizationUtils;
use App\Service\Utils\DatesUtils;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class UtilsTest extends TestCase
{
    private MockObject|NetworkOrganizationRepository $networkOrganizationRepository;
    private MockObject|NetworkUtils $networkUtils;

    public function setUp(): void
    {
        $this->networkOrganizationRepository = $this->getMockBuilder(NetworkOrganizationRepository::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->disableOriginalConstructor()->getMock();
    }

    public function getOrganizationUtilsMockFor(string $methodName): MockObject|OrganizationUtils
    {
        return $this->getMockBuilder(OrganizationUtils::class)
            ->setConstructorArgs([$this->networkOrganizationRepository, $this->networkUtils])
            ->setMethodsExcept([$methodName])
            ->getMock();
    }

    private function createOrganizationWithProductMock(SettingsProductEnum $product): Organization
    {
        $settings = $this->getMockBuilder(Settings::class)->getMock();
        $settings->method('getProduct')->willReturn($product);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getSettings')->willReturn($settings);

        return $organization;
    }

    /**
     * @see OrganizationUtils::isStructure()
     */
    public function testIsStructure(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isStructure');

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
        $this->assertTrue($organizationUtils->isStructure($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
        $this->assertTrue($organizationUtils->isStructure($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
        $this->assertTrue($organizationUtils->isStructure($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
        $this->assertTrue($organizationUtils->isStructure($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
        $this->assertFalse($organizationUtils->isStructure($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
        $this->assertFalse($organizationUtils->isStructure($organization));
    }

    /**
     * @see OrganizationUtils::isManager()
     */
    public function testIsManager(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isManager');

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
        $this->assertFalse($organizationUtils->isManager($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
        $this->assertFalse($organizationUtils->isManager($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
        $this->assertFalse($organizationUtils->isManager($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
        $this->assertFalse($organizationUtils->isManager($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
        $this->assertTrue($organizationUtils->isManager($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
        $this->assertFalse($organizationUtils->isManager($organization));
    }

    /**
     * @see OrganizationUtils::is2iosOrganization()
     */
    public function testIsOrganizationIs2ios(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('is2iosOrganization');

        $organization = $this->getMockBuilder(Organization::class)->getMock();

        $organizationUtils->expects(self::once())->method('isOrganizationIdEqualTo')->with($organization, OrganizationIdsEnum::_2IOS);

        $organizationUtils->is2iosOrganization($organization);
    }

    /**
     * @see OrganizationUtils::isCMF()
     */
    public function testIsCMF(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isCMF');

        $organization = $this->getMockBuilder(Organization::class)->getMock();

        $organizationUtils->expects(self::once())->method('isOrganizationIdEqualTo')->with($organization, OrganizationIdsEnum::CMF);

        $organizationUtils->isCMF($organization);
    }

    /**
     * @see OrganizationUtils::is2iosOrganization()
     */
    public function testIsOrganizationIdEqualTo(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isOrganizationIdEqualTo');

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getId')->willReturnOnConsecutiveCalls(123, OrganizationIdsEnum::_2IOS->value);

        $this->assertFalse($organizationUtils->isOrganizationIdEqualTo($organization, OrganizationIdsEnum::_2IOS));
        $this->assertTrue($organizationUtils->isOrganizationIdEqualTo($organization, OrganizationIdsEnum::_2IOS));
    }

    /**
     * @see OrganizationUtils::getOrganizationCurrentActivityYear()
     */
    public function testGetOrganizationCurrentActivityYear(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationCurrentActivityYear');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getMusicalDate')->willReturnOnConsecutiveCalls(
            null,  // default should be '2020-08-31'
            new \DateTime('2020-10-01')
        );

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        DatesUtils::setFakeDatetime('2020-03-01');

        $this->assertEquals(
            2019,
            $organizationUtils->getOrganizationCurrentActivityYear($organization)
        );

        DatesUtils::setFakeDatetime('2020-11-01');

        $this->assertEquals(
            2020,
            $organizationUtils->getOrganizationCurrentActivityYear($organization)
        );
    }

    /**
     * @see OrganizationUtils::getActivityPeriodsSwitchYear()
     */
    public function testGetActivityPeriodsSwitchYear(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getActivityPeriodsSwitchYear');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getMusicalDate')->willReturn(new \DateTime('2020-09-05'));

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $periods = $organizationUtils->getActivityPeriodsSwitchYear($organization, 2022);

        $this->assertEquals('2022-09-05', $periods['dateStart']);
        $this->assertEquals('2023-09-04', $periods['dateEnd']);
    }

    /**
     * @see OrganizationUtils::getActivityPeriodsSwitchYear()
     */
    public function testGetActivityPeriodsSwitchYearNoMusicalDate(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getActivityPeriodsSwitchYear');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getMusicalDate')->willReturn(null);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $periods = $organizationUtils->getActivityPeriodsSwitchYear($organization, 2022);

        $this->assertEquals('2022-09-01', $periods['dateStart']);
        $this->assertEquals('2023-08-31', $periods['dateEnd']);
    }

    /**
     * @see OrganizationUtils::getActivityYearSwitchDate()
     */
    public function testGetActivityYearSwitchDate(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getActivityYearSwitchDate');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getMusicalDate')->willReturn(new \DateTime('2020-09-05'));

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $this->assertEquals(2022, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-10')));
        $this->assertEquals(2021, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-02')));
    }

    /**
     * @see OrganizationUtils::getActivityYearSwitchDate()
     */
    public function testGetActivityYearSwitchDateNoMusicalDate(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getActivityYearSwitchDate');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getMusicalDate')->willReturn(null);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $this->assertEquals(2021, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-08-01')));
        $this->assertEquals(2022, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-02')));
    }

    /**
     * @see OrganizationUtils::getOrganizationActiveSubdomain()
     */
    public function testGetOrganizationActiveSubdomain(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationActiveSubdomain');

        $subdomain1 = $this->getMockBuilder(Subdomain::class)->getMock();
        $subdomain1->method('isActive')->willReturn(false);

        $subdomain2 = $this->getMockBuilder(Subdomain::class)->getMock();
        $subdomain2->method('isActive')->willReturn(false);

        $subdomain3 = $this->getMockBuilder(Subdomain::class)->getMock();
        $subdomain3->method('isActive')->willReturn(true);
        $subdomain3->method('getSubdomain')->willReturn('foo');

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getSubdomains')->willReturn(new ArrayCollection([$subdomain1, $subdomain2, $subdomain3]));

        $this->assertEquals('foo', $organizationUtils->getOrganizationActiveSubdomain($organization));
    }

    /**
     * @see OrganizationUtils::getOrganizationActiveSubdomain()
     */
    public function testGetOrganizationActiveSubdomainNone(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationActiveSubdomain');

        $subdomain1 = $this->getMockBuilder(Subdomain::class)->getMock();
        $subdomain1->method('isActive')->willReturn(false);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getSubdomains')->willReturn(new ArrayCollection([$subdomain1]));

        $this->assertEquals(null, $organizationUtils->getOrganizationActiveSubdomain($organization));
    }

    /**
     * @see OrganizationUtils::getOrganizationWebsite()
     */
    public function testOrganizationWebsite(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
        $parameters->method('getCustomDomain')->willReturn(null);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $organizationUtils->method('getOrganizationActiveSubdomain')->with($organization)->willReturn('foo');

        $this->assertEquals('https://foo.opentalent.fr', $organizationUtils->getOrganizationWebsite($organization));
    }

    /**
     * @see OrganizationUtils::getOrganizationWebsite()
     */
    public function testOrganizationWebsiteDeactivatedWithOther(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
        $parameters->method('getWebsite')->willReturn('foo.net');

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $this->assertEquals('https://foo.net', $organizationUtils->getOrganizationWebsite($organization));
    }

    /**
     * @see OrganizationUtils::getOrganizationWebsite()
     */
    public function testOrganizationWebsiteDeactivatedNoOther(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
        $parameters->method('getWebsite')->willReturn(null);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $this->assertEquals(null, $organizationUtils->getOrganizationWebsite($organization));
    }

    /**
     * @see OrganizationUtils::getOrganizationWebsite()
     */
    public function testOrganizationWebsiteWithCustom(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
        $parameters->method('getCustomDomain')->willReturn('bar.net');

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $this->assertEquals('https://bar.net', $organizationUtils->getOrganizationWebsite($organization));
    }

    /**
     * @see OrganizationUtils::getOrganizationWebsite()
     */
    public function testOrganizationWebsiteNoSubdomains(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
        $parameters->method('getCustomDomain')->willReturn(null);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $organizationUtils->method('getOrganizationActiveSubdomain')->with($organization)->willReturn(null);

        $this->assertEquals(null, $organizationUtils->getOrganizationWebsite($organization));
    }

    /**
     * @see OrganizationUtils::getOrganizationWebsite()
     */
    public function testOrganizationWebsiteOutOfNetSubdomain(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');

        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
        $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
        $parameters->method('getCustomDomain')->willReturn(null);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getParameters')->willReturn($parameters);

        $organizationUtils->method('getOrganizationActiveSubdomain')->with($organization)->willReturn('outofnet');

        $this->assertEquals('https://opentalent.fr', $organizationUtils->getOrganizationWebsite($organization));
    }

    /**
     * @see Utils::isLastParentAndCMF()
     */
    public function testIsLastParentAndCMF(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isLastParentAndCMF');

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $this->networkOrganizationRepository
            ->method('isLastParent')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, true],
                [$organization3, false],
                [$organization4, false],
            ]);

        $this->networkUtils
            ->method('isCMF')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->assertTrue($organizationUtils->isLastParentAndCMF($organization1));
        $this->assertFalse($organizationUtils->isLastParentAndCMF($organization2));
        $this->assertFalse($organizationUtils->isLastParentAndCMF($organization3));
        $this->assertFalse($organizationUtils->isLastParentAndCMF($organization4));
    }

    /**
     * @see Utils::isStructureAndCMF()
     */
    public function testIsStructureAndCMF(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isStructureAndCMF');

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $organizationUtils
            ->method('isStructure')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->networkUtils
            ->method('isCMF')
            ->with($organization1)
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, false],
                [$organization4, true],
            ]);

        $this->assertTrue($organizationUtils->isStructureAndCMF($organization1));
        $this->assertFalse($organizationUtils->isStructureAndCMF($organization2));
        $this->assertFalse($organizationUtils->isStructureAndCMF($organization3));
        $this->assertFalse($organizationUtils->isStructureAndCMF($organization4));
    }

    /**
     * @see Utils::isManagerAndCMF()
     */
    public function testIsManagerAndCMF(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isManagerAndCMF');

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $organizationUtils
            ->method('isManager')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->networkUtils
            ->method('isCMF')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, false],
                [$organization4, true],
            ]);

        $this->assertTrue($organizationUtils->isManagerAndCMF($organization1));
        $this->assertFalse($organizationUtils->isManagerAndCMF($organization2));
        $this->assertFalse($organizationUtils->isManagerAndCMF($organization3));
        $this->assertFalse($organizationUtils->isManagerAndCMF($organization4));
    }

    /**
     * @see Utils::isManagerAndNotLastParentAndCMF()
     */
    public function testIsManagerAndLastParentAndCMF(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isManagerAndLastParentAndCMF');

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $organizationUtils
            ->method('isManager')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $organizationUtils
            ->method('isLastParentAndCMF')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, false],
                [$organization4, true],
            ]);

        $this->assertTrue($organizationUtils->isManagerAndLastParentAndCMF($organization1));
        $this->assertFalse($organizationUtils->isManagerAndLastParentAndCMF($organization2));
        $this->assertFalse($organizationUtils->isManagerAndLastParentAndCMF($organization3));
        $this->assertFalse($organizationUtils->isManagerAndLastParentAndCMF($organization4));
    }

    /**
     * @see Utils::isManagerAndNotLastParentAndCMF()
     */
    public function testIsManagerAndNotLastParentAndCMF(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isManagerAndNotLastParentAndCMF');

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $organizationUtils
            ->method('isManager')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, true],
            ]);

        $this->networkOrganizationRepository
            ->method('isLastParent')
            ->willReturnMap([
                [$organization1, false],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->networkUtils
            ->method('isCMF')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, true],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->assertTrue($organizationUtils->isManagerAndNotLastParentAndCMF($organization1));
        $this->assertFalse($organizationUtils->isManagerAndNotLastParentAndCMF($organization2));
        $this->assertFalse($organizationUtils->isManagerAndNotLastParentAndCMF($organization3));
        $this->assertFalse($organizationUtils->isManagerAndNotLastParentAndCMF($organization4));
    }

    /**
     * @see OrganizationUtils::isSchool()
     */
    public function testIsSchool(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isSchool');

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
        $this->assertFalse($organizationUtils->isSchool($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
        $this->assertFalse($organizationUtils->isSchool($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
        $this->assertTrue($organizationUtils->isSchool($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
        $this->assertTrue($organizationUtils->isSchool($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
        $this->assertFalse($organizationUtils->isSchool($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
        $this->assertFalse($organizationUtils->isSchool($organization));
    }

    /**
     * @see OrganizationUtils::isArtist()
     */
    public function testIsArtist(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('isArtist');

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
        $this->assertTrue($organizationUtils->isArtist($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
        $this->assertTrue($organizationUtils->isArtist($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
        $this->assertFalse($organizationUtils->isArtist($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
        $this->assertFalse($organizationUtils->isArtist($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
        $this->assertFalse($organizationUtils->isArtist($organization));

        $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
        $this->assertFalse($organizationUtils->isArtist($organization));
    }

    /**
     * @see OrganizationUtils::hasModule()
     */
    public function testHasModule(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('hasModule');

        $settings = $this->getMockBuilder(Settings::class)->getMock();
        $settings->method('getModules')->willReturn(['foo', 'bar']);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getSettings')->willReturn($settings);

        $this->assertTrue($organizationUtils->hasModule($organization, 'foo'));
        $this->assertFalse($organizationUtils->hasModule($organization, 'other'));
    }

    /**
     * @see OrganizationUtils::getActiveNetworkOrganization()
     */
    public function testGetActiveNetworkOrganization(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getActiveNetworkOrganization');

        $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
        $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
        $networkOrganization3 = $this->getMockBuilder(NetworkOrganization::class)->getMock();

        $this
            ->networkUtils
            ->expects(self::exactly(2))
            ->method('isNetworkOrganizationActiveNow')
            ->willReturnMap([
                [$networkOrganization1, false],
                [$networkOrganization2, true],
                [$networkOrganization3, false],
            ]);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization
            ->method('getNetworkOrganizations')
            ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2, $networkOrganization3]));

        $result = $organizationUtils->getActiveNetworkOrganization($organization);

        $this->assertEquals($networkOrganization2, $result);
    }

    /**
     * @see OrganizationUtils::getActiveNetworkOrganization()
     */
    public function testGetActiveNetworkOrganizationNoActive(): void
    {
        $organizationUtils = $this->getOrganizationUtilsMockFor('getActiveNetworkOrganization');

        $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
        $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();

        $this
            ->networkUtils
            ->expects(self::exactly(2))
            ->method('isNetworkOrganizationActiveNow')
            ->willReturnMap([
                [$networkOrganization1, false],
                [$networkOrganization2, false],
            ]);

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization
            ->method('getNetworkOrganizations')
            ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2]));

        $result = $organizationUtils->getActiveNetworkOrganization($organization);

        $this->assertEquals(null, $result);
    }
}
