PhoneNumberExtensionTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\Twig;
  4. use App\Service\Twig\PhoneNumberExtension;
  5. use libphonenumber\PhoneNumber;
  6. use libphonenumber\PhoneNumberFormat;
  7. use libphonenumber\PhoneNumberUtil;
  8. use PHPUnit\Framework\MockObject\MockObject;
  9. use PHPUnit\Framework\TestCase;
  10. use Twig\TwigFilter;
  11. class PhoneNumberExtensionTest extends TestCase
  12. {
  13. private PhoneNumberUtil|MockObject $phoneNumberUtil;
  14. public function setUp(): void
  15. {
  16. $this->phoneNumberUtil = $this->getMockBuilder(PhoneNumberUtil::class)
  17. ->disableOriginalConstructor()
  18. ->getMock();
  19. }
  20. private function getPhoneNumberExtensionMockFor(string $methodName): PhoneNumberExtension|MockObject
  21. {
  22. return $this->getMockBuilder(PhoneNumberExtension::class)
  23. ->setConstructorArgs([$this->phoneNumberUtil])
  24. ->setMethodsExcept([$methodName])
  25. ->getMock();
  26. }
  27. /**
  28. * @see PhoneNumberExtension::getFilters()
  29. */
  30. public function testGetFilters(): void
  31. {
  32. $extension = $this->getPhoneNumberExtensionMockFor('getFilters');
  33. $filters = $extension->getFilters();
  34. $this->assertIsArray($filters);
  35. $this->assertCount(2, $filters);
  36. // Test first filter (phone_international)
  37. $this->assertInstanceOf(TwigFilter::class, $filters[0]);
  38. $this->assertSame('phone_international', $filters[0]->getName());
  39. $callable = $filters[0]->getCallable();
  40. $this->assertIsArray($callable);
  41. $this->assertSame($extension, $callable[0]);
  42. $this->assertSame('formatPhoneInternational', $callable[1]);
  43. // Test second filter (phone_national)
  44. $this->assertInstanceOf(TwigFilter::class, $filters[1]);
  45. $this->assertSame('phone_national', $filters[1]->getName());
  46. $callable = $filters[1]->getCallable();
  47. $this->assertIsArray($callable);
  48. $this->assertSame($extension, $callable[0]);
  49. $this->assertSame('formatPhoneNational', $callable[1]);
  50. }
  51. /**
  52. * @see PhoneNumberExtension::formatPhoneInternational()
  53. */
  54. public function testFormatPhoneInternational(): void
  55. {
  56. $extension = $this->getPhoneNumberExtensionMockFor('formatPhoneInternational');
  57. $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
  58. $expectedResult = '+33 1 23 45 67 89';
  59. $this->phoneNumberUtil->expects(self::once())
  60. ->method('format')
  61. ->with($phoneNumber, PhoneNumberFormat::INTERNATIONAL)
  62. ->willReturn($expectedResult);
  63. $result = $extension->formatPhoneInternational($phoneNumber);
  64. $this->assertSame($expectedResult, $result);
  65. }
  66. /**
  67. * @see PhoneNumberExtension::formatPhoneInternational()
  68. */
  69. public function testFormatPhoneInternationalWithNull(): void
  70. {
  71. $extension = $this->getPhoneNumberExtensionMockFor('formatPhoneInternational');
  72. $this->phoneNumberUtil->expects(self::never())
  73. ->method('format');
  74. $result = $extension->formatPhoneInternational(null);
  75. $this->assertSame('', $result);
  76. }
  77. /**
  78. * @see PhoneNumberExtension::formatPhoneNational()
  79. */
  80. public function testFormatPhoneNational(): void
  81. {
  82. $extension = $this->getPhoneNumberExtensionMockFor('formatPhoneNational');
  83. $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
  84. $expectedResult = '01 23 45 67 89';
  85. $this->phoneNumberUtil->expects(self::once())
  86. ->method('format')
  87. ->with($phoneNumber, PhoneNumberFormat::NATIONAL)
  88. ->willReturn($expectedResult);
  89. $result = $extension->formatPhoneNational($phoneNumber);
  90. $this->assertSame($expectedResult, $result);
  91. }
  92. /**
  93. * @see PhoneNumberExtension::formatPhoneNational()
  94. */
  95. public function testFormatPhoneNationalWithNull(): void
  96. {
  97. $extension = $this->getPhoneNumberExtensionMockFor('formatPhoneNational');
  98. $this->phoneNumberUtil->expects(self::never())
  99. ->method('format');
  100. $result = $extension->formatPhoneNational(null);
  101. $this->assertSame('', $result);
  102. }
  103. }