<?php

namespace App\Tests\Unit\Service\Core;

use App\Entity\Core\AddressPostal;
use App\Service\Core\AddressPostalUtils;
use PHPUnit\Framework\TestCase;

class TestableAddressPostalUtils extends AddressPostalUtils {
    // there's a bug when SUT has only one method,
    // it can't be mocked, so we add one method here in the meantime
    public function foo(): void {}
}

class AddressPostalUtilsTest extends TestCase
{
    /**
     * @see AddressPostalUtils::getFullStreetAddress()
     */
    public function testGetFullStreetAddress(): void {
        $addressPostalUtils = $this->getMockBuilder(TestableAddressPostalUtils::class)
            ->setMethodsExcept(['getFullStreetAddress'])
            ->getMock();

        $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
        $addressPostal->method('getStreetAddress')->willReturn('Abc');
        $addressPostal->method('getStreetAddressSecond')->willReturn('Def  ');
        $addressPostal->method('getStreetAddressThird')->willReturn('  Ghi ');

        $this->assertEqualsCanonicalizing(
            "Abc\nDef\nGhi",
            $addressPostalUtils->getFullStreetAddress($addressPostal)
        );

        $this->assertEqualsCanonicalizing(
            "Abc Def Ghi",
            $addressPostalUtils->getFullStreetAddress($addressPostal, ' ')
        );
    }

    /**
     * @see AddressPostalUtils::getFullStreetAddress()
     */
    public function testGetFullStreetAddressWithMissing(): void {
        $addressPostalUtils = $this->getMockBuilder(TestableAddressPostalUtils::class)
            ->setMethodsExcept(['getFullStreetAddress'])
            ->getMock();

        $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
        $addressPostal->method('getStreetAddress')->willReturn('Abc');
        $addressPostal->method('getStreetAddressSecond')->willReturn('');
        $addressPostal->method('getStreetAddressThird')->willReturn('  Def');

        $this->assertEqualsCanonicalizing(
            "Abc\nDef",
            $addressPostalUtils->getFullStreetAddress($addressPostal)
        );
    }

}
