class ConfigImporterTest in Currency 8.3
@coversDefaultClass \Drupal\currency\ConfigImporter
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\ConfigImporterTest
Expanded class hierarchy of ConfigImporterTest
File
- tests/
src/ Unit/ ConfigImporterTest.php, line 23
Namespace
Drupal\Tests\currency\UnitView source
class ConfigImporterTest extends UnitTestCase {
/**
* The config storage.
*
* @var \Drupal\Core\Config\StorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configStorage;
/**
* The currency entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currencyStorage;
/**
* The currency locale entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currencyLocaleStorage;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The class under test.
*
* @var \Drupal\currency\ConfigImporter
*/
protected $sut;
/**
* The typed config manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $typedConfigManager;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$this->configStorage = $this
->createMock(StorageInterface::class);
$this->currencyStorage = $this
->createMock(EntityStorageInterface::class);
$this->currencyLocaleStorage = $this
->createMock(EntityStorageInterface::class);
$this->moduleHandler = $this
->createMock(ModuleHandlerInterface::class);
$this->typedConfigManager = $this
->createMock(TypedConfigManagerInterface::class);
$map = [
[
'currency',
$this->currencyStorage,
],
[
'currency_locale',
$this->currencyLocaleStorage,
],
];
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->entityTypeManager
->expects($this
->atLeastOnce())
->method('getStorage')
->willReturnMap($map);
$this->sut = new ConfigImporter($this->moduleHandler, $this->typedConfigManager, $this->entityTypeManager);
}
/**
* @covers ::__construct
*/
public function testConstruct() {
$this->sut = new ConfigImporter($this->moduleHandler, $this->typedConfigManager, $this->entityTypeManager);
}
/**
* @covers ::getConfigStorage
* @covers ::setConfigStorage
*/
public function testGetConfigStorage() {
$method_get = new \ReflectionMethod($this->sut, 'getConfigStorage');
$method_get
->setAccessible(TRUE);
FileCacheFactory::setPrefix('prefix');
$extension = $this
->getMockBuilder(Extension::class)
->disableOriginalConstructor()
->getMock();
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('getModule')
->willReturn($extension);
$this
->assertInstanceof(StorageInterface::class, $method_get
->invoke($this->sut));
$config_storage = $this
->createMock(StorageInterface::class);
$this->sut
->setConfigStorage($config_storage);
$this
->assertSame($config_storage, $method_get
->invoke($this->sut));
}
/**
* @covers ::getImportableCurrencies
* @covers ::createCurrencyFromRepository
*/
public function testGetImportableCurrencies() {
$resource_repository = new ResourceRepository();
$resource_repository_currency_codes = $resource_repository
->listCurrencies();
// Fake the importable and existing currencies, by taking two mutually
// exclusive, randomized samples of the currencies available in the
// repository.
shuffle($resource_repository_currency_codes);
list($expected_importable_currency_codes, $stored_currency_codes) = array_chunk($resource_repository_currency_codes, ceil(count($resource_repository_currency_codes) / 2));
$currency = $this
->createMock(CurrencyInterface::class);
$stored_currencies = [];
foreach ($stored_currency_codes as $stored_currency_code) {
$stored_currencies[$stored_currency_code] = $this
->createMock(CurrencyInterface::class);
}
$this->currencyStorage
->expects($this
->atLeastOnce())
->method('create')
->willReturn($currency);
$this->currencyStorage
->expects($this
->atLeastOnce())
->method('loadMultiple')
->with(NULL)
->willReturn($stored_currencies);
$importable_currencies = $this->sut
->getImportableCurrencies();
sort($expected_importable_currency_codes);
$importable_currency_codes = array_keys($importable_currencies);
sort($importable_currency_codes);
$this
->assertSame($expected_importable_currency_codes, $importable_currency_codes);
}
/**
* @covers ::getImportableCurrencyLocales
*/
public function testGetImportableCurrencyLocales() {
$locale_a = $this
->randomMachineName();
$currency_locale_a = $this
->createMock(CurrencyLocaleInterface::class);
$locale_b = $this
->randomMachineName();
$currency_locale_b = $this
->createMock(CurrencyLocaleInterface::class);
$locale_c = $this
->randomMachineName();
$currency_locale_data_c = [
'id' => $locale_c,
];
$currency_locale_c = $this
->createMock(CurrencyLocaleInterface::class);
$stored_currencies = [
$locale_a => $currency_locale_a,
$locale_b => $currency_locale_b,
];
$this->currencyLocaleStorage
->expects($this
->atLeastOnce())
->method('create')
->with($currency_locale_data_c)
->willReturn($currency_locale_c);
$this->currencyLocaleStorage
->expects($this
->atLeastOnce())
->method('loadMultiple')
->with(NULL)
->willReturn($stored_currencies);
$prefix = 'currency.currency_locale.';
$this->configStorage
->expects($this
->atLeastOnce())
->method('listAll')
->with($prefix)
->willReturn([
$prefix . $locale_b,
$prefix . $locale_c,
]);
$this->configStorage
->expects($this
->once())
->method('read')
->with($prefix . $locale_c)
->willReturn($currency_locale_data_c);
$this->sut
->setConfigStorage($this->configStorage);
$importable_currencies = $this->sut
->getImportableCurrencyLocales();
$this
->assertSame([
$currency_locale_c,
], $importable_currencies);
}
/**
* @covers ::importCurrency
* @covers ::createCurrencyFromRepository
*/
public function testImportCurrency() {
$resource_repository = new ResourceRepository();
$currency_codes = $resource_repository
->listCurrencies();
$currency_code = $currency_codes[array_rand($currency_codes)];
$currency = $this
->createMock(CurrencyInterface::class);
$this->currencyStorage
->expects($this
->atLeastOnce())
->method('create')
->with()
->willReturn($currency);
$this->sut
->setConfigStorage($this->configStorage);
$this
->assertSame($currency, $this->sut
->importCurrency($currency_code));
}
/**
* @covers ::importCurrency
*/
public function testImportCurrencyWithExistingCurrency() {
$currency_code = $this
->randomMachineName();
$currency = $this
->createMock(CurrencyInterface::class);
$this->currencyStorage
->expects($this
->never())
->method('create');
$this->currencyStorage
->expects($this
->once())
->method('load')
->with($currency_code)
->willReturn($currency);
$this->configStorage
->expects($this
->never())
->method('read');
$this->sut
->setConfigStorage($this->configStorage);
$this
->assertFalse($this->sut
->importCurrency($currency_code));
}
/**
* @covers ::importCurrencyLocale
*/
public function testImportCurrencyLocale() {
$locale = $this
->randomMachineName();
$currency_locale_data = [
'id' => $locale,
];
$currency_locale = $this
->createMock(CurrencyLocaleInterface::class);
$this->currencyLocaleStorage
->expects($this
->atLeastOnce())
->method('create')
->with($currency_locale_data)
->willReturn($currency_locale);
$this->configStorage
->expects($this
->once())
->method('read')
->with('currency.currency_locale.' . $locale)
->willReturn($currency_locale_data);
$this->sut
->setConfigStorage($this->configStorage);
$this
->assertSame($currency_locale, $this->sut
->importCurrencyLocale($locale));
}
/**
* @covers ::importCurrencyLocale
*/
public function testImportCurrencyLocaleWithExistingCurrency() {
$locale = $this
->randomMachineName();
$currency_locale = $this
->createMock(CurrencyLocaleInterface::class);
$this->currencyLocaleStorage
->expects($this
->never())
->method('create');
$this->currencyLocaleStorage
->expects($this
->once())
->method('load')
->with($locale)
->willReturn($currency_locale);
$this->configStorage
->expects($this
->never())
->method('read');
$this->sut
->setConfigStorage($this->configStorage);
$this
->assertFalse($this->sut
->importCurrencyLocale($locale));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigImporterTest:: |
protected | property | The config storage. | |
ConfigImporterTest:: |
protected | property | The currency locale entity storage. | |
ConfigImporterTest:: |
protected | property | The currency entity storage. | |
ConfigImporterTest:: |
protected | property | The entity type manager. | |
ConfigImporterTest:: |
protected | property | The module handler. | |
ConfigImporterTest:: |
protected | property | The class under test. | |
ConfigImporterTest:: |
protected | property | The typed config manager. | |
ConfigImporterTest:: |
public | function |
Overrides UnitTestCase:: |
|
ConfigImporterTest:: |
public | function | @covers ::__construct | |
ConfigImporterTest:: |
public | function | @covers ::getConfigStorage @covers ::setConfigStorage | |
ConfigImporterTest:: |
public | function | @covers ::getImportableCurrencies @covers ::createCurrencyFromRepository | |
ConfigImporterTest:: |
public | function | @covers ::getImportableCurrencyLocales | |
ConfigImporterTest:: |
public | function | @covers ::importCurrency @covers ::createCurrencyFromRepository | |
ConfigImporterTest:: |
public | function | @covers ::importCurrencyLocale | |
ConfigImporterTest:: |
public | function | @covers ::importCurrencyLocale | |
ConfigImporterTest:: |
public | function | @covers ::importCurrency | |
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. |