You are here

class CurrencyLocaleTest in Currency 8.3

Same name in this branch
  1. 8.3 tests/src/Kernel/CurrencyLocaleTest.php \Drupal\Tests\currency\Kernel\CurrencyLocaleTest
  2. 8.3 tests/src/Unit/Entity/CurrencyLocaleTest.php \Drupal\Tests\currency\Unit\Entity\CurrencyLocaleTest

@coversDefaultClass \Drupal\currency\Entity\CurrencyLocale

@group Currency

Hierarchy

Expanded class hierarchy of CurrencyLocaleTest

File

tests/src/Unit/Entity/CurrencyLocaleTest.php, line 16

Namespace

Drupal\Tests\currency\Unit\Entity
View source
class CurrencyLocaleTest extends UnitTestCase {

  /**
   * The country manager.
   *
   * @var \Drupal\Core\Locale\CountryManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $countryManager;

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $stringTranslation;

  /**
   * The class under test.
   *
   * @var \Drupal\currency\Entity\CurrencyLocale
   */
  protected $sut;

  /**
   * {@inheritdoc}
   *
   * @covers ::setCountryManager
   */
  function setUp() : void {
    $this->countryManager = $this
      ->createMock(CountryManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new CurrencyLocale([], $this
      ->randomMachineName());
    $this->sut
      ->setCountryManager($this->countryManager);
    $this->sut
      ->setStringTranslation($this->stringTranslation);
  }

  /**
   * @covers ::setDecimalSeparator
   * @covers ::getDecimalSeparator
   */
  function testGetDecimalSeparator() {
    $separator = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setDecimalSeparator($separator));
    $this
      ->assertSame($separator, $this->sut
      ->getDecimalSeparator());
  }

  /**
   * @covers ::setGroupingSeparator
   * @covers ::getGroupingSeparator
   */
  function testGetGroupingSeparator() {
    $separator = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setGroupingSeparator($separator));
    $this
      ->assertSame($separator, $this->sut
      ->getGroupingSeparator());
  }

  /**
   * @covers ::setLocale
   * @covers ::getLocale
   */
  function testGetLocale() {
    $language_code = $this
      ->randomMachineName();
    $country_code = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setLocale($language_code, $country_code));
    $this
      ->assertSame(strtolower($language_code) . '_' . strtoupper($country_code), $this->sut
      ->getLocale());
  }

  /**
   * @covers ::id
   *
   * @depends testGetLocale
   */
  function testId() {
    $language_code = $this
      ->randomMachineName();
    $country_code = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setLocale($language_code, $country_code));
    $this
      ->assertSame(strtolower($language_code) . '_' . strtoupper($country_code), $this->sut
      ->id());
  }

  /**
   * @covers ::getLanguageCode
   *
   * @depends testGetLocale
   */
  function testGetLanguageCode() {
    $language_code = $this
      ->randomMachineName();
    $country_code = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setLocale($language_code, $country_code));
    $this
      ->assertSame(strtolower($language_code), $this->sut
      ->getLanguageCode());
  }

  /**
   * @covers ::getCountryCode
   *
   * @depends testGetLocale
   */
  function testGetCountryCode() {
    $language_code = $this
      ->randomMachineName();
    $country_code = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setLocale($language_code, $country_code));
    $this
      ->assertSame(strtoupper($country_code), $this->sut
      ->getCountryCode());
  }

  /**
   * @covers ::setPattern
   * @covers ::getPattern
   */
  function testGetPattern() {
    $pattern = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setPattern($pattern));
    $this
      ->assertSame($pattern, $this->sut
      ->getPattern());
  }

  /**
   * @covers ::label
   * @covers ::getCountryManager
   *
   * @depends testGetLocale
   */
  function testLabel() {
    $languages = LanguageManager::getStandardLanguageList();
    $language_code = array_rand($languages);
    $country_code_a = strtoupper($this
      ->randomMachineName());
    $country_code_b = strtoupper($this
      ->randomMachineName());
    $country_code_c = strtoupper($this
      ->randomMachineName());
    $country_list = [
      $country_code_a => $this
        ->randomMachineName(),
      $country_code_b => $this
        ->randomMachineName(),
      $country_code_c => $this
        ->randomMachineName(),
    ];
    $this->countryManager
      ->expects($this
      ->atLeastOnce())
      ->method('getList')
      ->willReturn($country_list);
    $this->sut
      ->setLocale($language_code, $country_code_b);
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $this->sut
      ->label());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyLocaleTest::$countryManager protected property The country manager.
CurrencyLocaleTest::$stringTranslation protected property The string translator.
CurrencyLocaleTest::$sut protected property The class under test.
CurrencyLocaleTest::setUp function @covers ::setCountryManager Overrides UnitTestCase::setUp
CurrencyLocaleTest::testGetCountryCode function @covers ::getCountryCode
CurrencyLocaleTest::testGetDecimalSeparator function @covers ::setDecimalSeparator @covers ::getDecimalSeparator
CurrencyLocaleTest::testGetGroupingSeparator function @covers ::setGroupingSeparator @covers ::getGroupingSeparator
CurrencyLocaleTest::testGetLanguageCode function @covers ::getLanguageCode
CurrencyLocaleTest::testGetLocale function @covers ::setLocale @covers ::getLocale
CurrencyLocaleTest::testGetPattern function @covers ::setPattern @covers ::getPattern
CurrencyLocaleTest::testId function @covers ::id
CurrencyLocaleTest::testLabel function @covers ::label @covers ::getCountryManager
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.