<?php

namespace App\Tests\Unit\Service\Utils;

use App\Entity\Access\Access;
use App\Entity\Billing\BillingSetting;
use App\Entity\Billing\ResidenceArea;
use App\Entity\Education\EducationTiming;
use App\Entity\Organization\Organization;
use App\Service\Utils\EntityUtils;
use PHPUnit\Framework\TestCase;

class TestableEntityUtils extends EntityUtils {
    public function organizationDefaultValue($entity, Access $access): void { parent::organizationDefaultValue($entity, $access); }
    public function billingSettingDefaultValueDefaultValue($entity, Access $access): void { parent::billingSettingDefaultValueDefaultValue($entity, $access); }
}

class EntityUtilsTest extends TestCase
{
    /**
     * @see EntityUtils::defaultValueSettersByAccess()
     */
    public function testDefaultValueSettersByAccess(): void {
        $utils = $this->getMockBuilder(TestableEntityUtils::class)
            ->setMethodsExcept(['defaultValueSettersByAccess'])
            ->getMock();

        $entity = $this->getMockBuilder(Organization::class)->getMock();
        $access = $this->getMockBuilder(Access::class)->getMock();

        $utils->expects(self::once())->method('organizationDefaultValue')->with($entity, $access);
        $utils->expects(self::once())->method('billingSettingDefaultValueDefaultValue')->with($entity, $access);

        $utils->defaultValueSettersByAccess($entity, $access);
    }

    /**
     * @see EntityUtils::organizationDefaultValue()
     */
    public function testOrganizationDefaultValue(): void {
        $utils = $this->getMockBuilder(TestableEntityUtils::class)
            ->setMethodsExcept(['organizationDefaultValue'])
            ->getMock();

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

        $entity = new EducationTiming();  // Can't test this method with a mocked entity...

        $access = $this->getMockBuilder(Access::class)->getMock();
        $access->method('getOrganization')->willReturn($organization);

        $utils->organizationDefaultValue($entity, $access);

        $this->assertEquals($organization, $entity->getOrganization());
    }

    /**
     * @see EntityUtils::billingSettingDefaultValueDefaultValue()
     */
    public function testBillingSettingDefaultValueDefaultValue(): void {
        $utils = $this->getMockBuilder(TestableEntityUtils::class)
            ->setMethodsExcept(['billingSettingDefaultValueDefaultValue'])
            ->getMock();

        $entity = new ResidenceArea();  // Can't test this method with a mocked entity...

        $billingSetting = $this->getMockBuilder(BillingSetting::class)->getMock();

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

        $access = $this->getMockBuilder(Access::class)->getMock();
        $access->method('getOrganization')->willReturn($organization);

        $utils->billingSettingDefaultValueDefaultValue($entity, $access);

        $this->assertEquals($billingSetting, $entity->getBillingSetting());
    }

}
