class LocaleResolverTest in Currency 8.3
@coversDefaultClass \Drupal\currency\LocaleResolver
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\LocaleResolverTest
Expanded class hierarchy of LocaleResolverTest
File
- tests/
src/ Unit/ LocaleResolverTest.php, line 24
Namespace
Drupal\Tests\currency\UnitView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LocaleResolverTest:: |
protected | property | The config factory. | |
LocaleResolverTest:: |
protected | property | The currency currency locale storage used for testing | |
LocaleResolverTest:: |
protected | property | The entity type manager. | |
LocaleResolverTest:: |
protected | property | The event dispatcher. | |
LocaleResolverTest:: |
protected | property | The language manager. | |
LocaleResolverTest:: |
protected | property | The class under test. | |
LocaleResolverTest:: |
protected | function | Prepares the language manager for testing. | |
LocaleResolverTest:: |
public | function |
Overrides UnitTestCase:: |
|
LocaleResolverTest:: |
function | @covers ::resolveCurrencyLocale | ||
LocaleResolverTest:: |
function | @covers ::resolveCurrencyLocale | ||
LocaleResolverTest:: |
function | @covers ::resolveCurrencyLocale | ||
LocaleResolverTest:: |
function | @covers ::resolveCurrencyLocale | ||
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |