You are here

class FormatterTest in Telephone Formatter 8

Formatter test.

@coversDefaultClass Drupal\telephone_formatter\Formatter

@group Telephone

Hierarchy

Expanded class hierarchy of FormatterTest

File

tests/src/Unit/FormatterTest.php, line 16

Namespace

Drupal\Tests\telephone_formatter\Unit
View source
class FormatterTest extends UnitTestCase {

  /**
   * Formatter service.
   *
   * @var \Drupal\telephone_formatter\FormatterInterface
   */
  protected $formatterService;

  /**
   * Language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->languageManager = $this
      ->createMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
    $this->formatterService = new Formatter($this->languageManager);
  }

  /**
   * Test formatter service.
   *
   * ::covers format.
   */
  public function testFormatterService() {
    $test_country = 'NO';
    $test_value = '98765432';
    $this
      ->assertEquals('987 65 432', $this->formatterService
      ->format($test_value, PhoneNumberFormat::NATIONAL, $test_country));
    $this
      ->assertEquals('+47 987 65 432', $this->formatterService
      ->format($test_value, PhoneNumberFormat::INTERNATIONAL, $test_country));
    $this
      ->assertEquals('+4798765432', $this->formatterService
      ->format($test_value, PhoneNumberFormat::E164, $test_country));
    $this
      ->assertEquals('tel:+47-987-65-432', $this->formatterService
      ->format($test_value, PhoneNumberFormat::RFC3966, $test_country));
  }

  /**
   * Valid national number but missing region code.
   *
   * ::covers format.
   *
   * @expectedException \libphonenumber\NumberParseException
   */
  public function testUnparsableNumber() {
    $this->formatterService
      ->format('98765432', PhoneNumberFormat::NATIONAL);
  }

  /**
   * Number was successfully parsed but invalid.
   *
   * ::covers format.
   *
   * @expectedException \InvalidArgumentException
   * @expectedExceptionMessage Number is invalid.
   */
  public function testInvalidNumber() {
    $this->formatterService
      ->format('987654320', PhoneNumberFormat::NATIONAL, 'NO');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormatterTest::$formatterService protected property Formatter service.
FormatterTest::$languageManager protected property Language manager.
FormatterTest::setUp public function Overrides UnitTestCase::setUp
FormatterTest::testFormatterService public function Test formatter service.
FormatterTest::testInvalidNumber public function Number was successfully parsed but invalid.
FormatterTest::testUnparsableNumber public function Valid national number but missing region code.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.