| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- declare(strict_types=1);
- namespace App\Tests\Unit\Service\Twig;
- use App\Service\Twig\PhoneNumberExtension;
- use libphonenumber\PhoneNumber;
- use libphonenumber\PhoneNumberFormat;
- use libphonenumber\PhoneNumberUtil;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- use Twig\TwigFilter;
- class PhoneNumberExtensionTest extends TestCase
- {
- private PhoneNumberUtil|MockObject $phoneNumberUtil;
- public function setUp(): void
- {
- $this->phoneNumberUtil = $this->getMockBuilder(PhoneNumberUtil::class)
- ->disableOriginalConstructor()
- ->getMock();
- }
- private function getPhoneNumberExtensionMockFor(string $methodName): PhoneNumberExtension|MockObject
- {
- return $this->getMockBuilder(PhoneNumberExtension::class)
- ->setConstructorArgs([$this->phoneNumberUtil])
- ->setMethodsExcept([$methodName])
- ->getMock();
- }
- /**
- * @see PhoneNumberExtension::getFilters()
- */
- public function testGetFilters(): void
- {
- $extension = $this->getPhoneNumberExtensionMockFor('getFilters');
- $filters = $extension->getFilters();
- $this->assertIsArray($filters);
- $this->assertCount(2, $filters);
- // Test first filter (phone_international)
- $this->assertInstanceOf(TwigFilter::class, $filters[0]);
- $this->assertSame('phone_international', $filters[0]->getName());
- $callable = $filters[0]->getCallable();
- $this->assertIsArray($callable);
- $this->assertSame($extension, $callable[0]);
- $this->assertSame('formatPhoneInternational', $callable[1]);
- // Test second filter (phone_national)
- $this->assertInstanceOf(TwigFilter::class, $filters[1]);
- $this->assertSame('phone_national', $filters[1]->getName());
- $callable = $filters[1]->getCallable();
- $this->assertIsArray($callable);
- $this->assertSame($extension, $callable[0]);
- $this->assertSame('formatPhoneNational', $callable[1]);
- }
- /**
- * @see PhoneNumberExtension::formatPhoneInternational()
- */
- public function testFormatPhoneInternational(): void
- {
- $extension = $this->getPhoneNumberExtensionMockFor('formatPhoneInternational');
- $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
- $expectedResult = '+33 1 23 45 67 89';
- $this->phoneNumberUtil->expects(self::once())
- ->method('format')
- ->with($phoneNumber, PhoneNumberFormat::INTERNATIONAL)
- ->willReturn($expectedResult);
- $result = $extension->formatPhoneInternational($phoneNumber);
- $this->assertSame($expectedResult, $result);
- }
- /**
- * @see PhoneNumberExtension::formatPhoneInternational()
- */
- public function testFormatPhoneInternationalWithNull(): void
- {
- $extension = $this->getPhoneNumberExtensionMockFor('formatPhoneInternational');
- $this->phoneNumberUtil->expects(self::never())
- ->method('format');
- $result = $extension->formatPhoneInternational(null);
- $this->assertSame('', $result);
- }
- /**
- * @see PhoneNumberExtension::formatPhoneNational()
- */
- public function testFormatPhoneNational(): void
- {
- $extension = $this->getPhoneNumberExtensionMockFor('formatPhoneNational');
- $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
- $expectedResult = '01 23 45 67 89';
- $this->phoneNumberUtil->expects(self::once())
- ->method('format')
- ->with($phoneNumber, PhoneNumberFormat::NATIONAL)
- ->willReturn($expectedResult);
- $result = $extension->formatPhoneNational($phoneNumber);
- $this->assertSame($expectedResult, $result);
- }
- /**
- * @see PhoneNumberExtension::formatPhoneNational()
- */
- public function testFormatPhoneNationalWithNull(): void
- {
- $extension = $this->getPhoneNumberExtensionMockFor('formatPhoneNational');
- $this->phoneNumberUtil->expects(self::never())
- ->method('format');
- $result = $extension->formatPhoneNational(null);
- $this->assertSame('', $result);
- }
- }
|