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); } }