You are here

class LocaleResolverTest in Currency 8.3

@coversDefaultClass \Drupal\currency\LocaleResolver

@group Currency

Hierarchy

Expanded class hierarchy of LocaleResolverTest

File

tests/src/Unit/LocaleResolverTest.php, line 24

Namespace

Drupal\Tests\currency\Unit
View source
class LocaleResolverTest extends UnitTestCase {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
   * The currency currency locale storage used for testing
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currencyLocaleStorage;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * The event dispatcher.
   *
   * @var \Drupal\currency\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $eventDispatcher;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $languageManager;

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

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $this->currencyLocaleStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('currency_locale')
      ->willReturn($this->currencyLocaleStorage);
    $this->eventDispatcher = $this
      ->createMock(EventDispatcherInterface::class);
    $this->languageManager = $this
      ->createMock(LanguageManagerInterface::class);
    $this->sut = new LocaleResolver($this->entityTypeManager, $this->languageManager, $this->configFactory, $this->eventDispatcher);
  }

  /**
   * @covers ::resolveCurrencyLocale
   */
  function testResolveCurrencyLocaleWithRequestCountry() {
    $this
      ->prepareLanguageManager();
    $request_country_code = 'IN';
    $this->eventDispatcher
      ->expects($this
      ->atLeastOnce())
      ->method('resolveCountryCode')
      ->willReturn($request_country_code);
    $currency_locale = $this
      ->createMock(CurrencyLocaleInterface::class);
    $this->currencyLocaleStorage
      ->expects($this
      ->any())
      ->method('load')
      ->with($this->languageManager
      ->getCurrentLanguage(Language::TYPE_CONTENT)
      ->getId() . '_' . $request_country_code)
      ->willReturn($currency_locale);

    // Test loading the fallback locale.
    $this
      ->assertSame($currency_locale, $this->sut
      ->resolveCurrencyLocale());
  }

  /**
   * @covers ::resolveCurrencyLocale
   */
  function testResolveCurrencyLocaleWithSiteDefaultCountry() {
    $this
      ->prepareLanguageManager();
    $site_default_country = 'IN';
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->any())
      ->method('get')
      ->with('country.default')
      ->willReturn($site_default_country);
    $this->configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('system.date')
      ->willReturn($config);
    $currency_locale = $this
      ->createMock(CurrencyLocaleInterface::class);
    $this->currencyLocaleStorage
      ->expects($this
      ->any())
      ->method('load')
      ->with($this->languageManager
      ->getCurrentLanguage(Language::TYPE_CONTENT)
      ->getId() . '_' . $site_default_country)
      ->willReturn($currency_locale);

    // Test loading the fallback locale.
    $this
      ->assertSame($currency_locale, $this->sut
      ->resolveCurrencyLocale());
  }

  /**
   * @covers ::resolveCurrencyLocale
   */
  function testResolveCurrencyLocaleFallback() {
    $this
      ->prepareLanguageManager();
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->any())
      ->method('get')
      ->with('country.default')
      ->willReturn(NULL);
    $this->configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('system.date')
      ->willReturn($config);
    $currency_locale = $this
      ->createMock(CurrencyLocaleInterface::class);
    $this->currencyLocaleStorage
      ->expects($this
      ->any())
      ->method('load')
      ->with(LocaleResolverInterface::DEFAULT_LOCALE)
      ->willReturn($currency_locale);

    // Test loading the fallback locale.
    $this
      ->assertSame($currency_locale, $this->sut
      ->resolveCurrencyLocale());
  }

  /**
   * @covers ::resolveCurrencyLocale
   */
  function testResolveCurrencyLocaleMissingFallback() {
    $this
      ->expectException(RuntimeException::class);
    $this
      ->prepareLanguageManager();
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->any())
      ->method('get')
      ->with('country.default')
      ->willReturn(NULL);
    $this->configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('system.date')
      ->willReturn($config);
    $this->currencyLocaleStorage
      ->expects($this
      ->any())
      ->method('load')
      ->with(LocaleResolverInterface::DEFAULT_LOCALE)
      ->willReturn(NULL);

    // Test loading the fallback locale.
    $this->sut
      ->resolveCurrencyLocale();
  }

  /**
   * Prepares the language manager for testing.
   */
  protected function prepareLanguageManager() {
    $language_code = $this
      ->randomMachineName(2);
    $language = $this
      ->createMock(LanguageInterface::class);
    $language
      ->expects($this
      ->atLeastOnce())
      ->method('getId')
      ->willReturn($language_code);
    $this->languageManager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->with(Language::TYPE_CONTENT)
      ->willReturn($language);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LocaleResolverTest::$configFactory protected property The config factory.
LocaleResolverTest::$currencyLocaleStorage protected property The currency currency locale storage used for testing
LocaleResolverTest::$entityTypeManager protected property The entity type manager.
LocaleResolverTest::$eventDispatcher protected property The event dispatcher.
LocaleResolverTest::$languageManager protected property The language manager.
LocaleResolverTest::$sut protected property The class under test.
LocaleResolverTest::prepareLanguageManager protected function Prepares the language manager for testing.
LocaleResolverTest::setUp public function Overrides UnitTestCase::setUp
LocaleResolverTest::testResolveCurrencyLocaleFallback function @covers ::resolveCurrencyLocale
LocaleResolverTest::testResolveCurrencyLocaleMissingFallback function @covers ::resolveCurrencyLocale
LocaleResolverTest::testResolveCurrencyLocaleWithRequestCountry function @covers ::resolveCurrencyLocale
LocaleResolverTest::testResolveCurrencyLocaleWithSiteDefaultCountry function @covers ::resolveCurrencyLocale
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.