You are here

TelephoneFormatterUnitTest.test in Telephone Formatter 7

File

tests/TelephoneFormatterUnitTest.test
View source
<?php

use libphonenumber\PhoneNumberFormat;
use libphonenumber\NumberParseException;
class TelephoneFormatterUnitTest extends DrupalUnitTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Telephone Formatter Unit Tests',
      'description' => 'Test Telephone Formatter library interaction.',
      'group' => 'Telephone Formatter',
    );
  }
  public function setUp() {
    parent::setUp('telephone_formatter');
  }

  /**
   * Test formatter - library interaction.
   */
  public function testFormatterLibraryInteraction() {
    $test_country = 'NO';
    $test_value = '98765432';
    $this
      ->assertEqual('987 65 432', _telephone_formatter_get_formatted_value($test_value, PhoneNumberFormat::NATIONAL, $test_country));
    $this
      ->assertEqual('+47 987 65 432', _telephone_formatter_get_formatted_value($test_value, PhoneNumberFormat::INTERNATIONAL, $test_country));
    $this
      ->assertEqual('+4798765432', _telephone_formatter_get_formatted_value($test_value, PhoneNumberFormat::E164, $test_country));
    $this
      ->assertEqual('tel:+47-987-65-432', _telephone_formatter_get_formatted_value($test_value, PhoneNumberFormat::RFC3966, $test_country));
  }

  /**
   * Valid national number but missing region code.
   */
  public function testUnparsableNumber() {
    try {
      _telephone_formatter_get_formatted_value('98765432', PhoneNumberFormat::NATIONAL);
    } catch (NumberParseException $e) {
      $this
        ->pass('NumberParseException was thrown as expected.');
    } catch (Exception $e) {
      $this
        ->fail('NumberParseException was not thrown for a valid national number but missing region code.');
    }
  }

  /**
   * Number was successfully parsed but invalid.
   */
  public function testInvalidNumber() {
    try {
      _telephone_formatter_get_formatted_value('987654320', PhoneNumberFormat::NATIONAL, 'NO');
    } catch (InvalidArgumentException $e) {
      $this
        ->pass('InvalidArgumentException was thrown as expected.');
    } catch (Exception $e) {
      $this
        ->fail('InvalidArgumentException was not thrown for a succesfully parsed but invalid number.');
    }
  }

}

Classes