class CurrencyLocalizeTest in Currency 8.3
@coversDefaultClass \Drupal\currency\Plugin\Filter\CurrencyLocalize
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\Plugin\Filter\CurrencyLocalizeTest
Expanded class hierarchy of CurrencyLocalizeTest
File
- tests/
src/ Unit/ Plugin/ Filter/ CurrencyLocalizeTest.php, line 24
Namespace
Drupal\Tests\currency\Unit\Plugin\FilterView source
class CurrencyLocalizeTest extends UnitTestCase {
/**
* The cache contexts manager.
*
* @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $cacheContextsManager;
/**
* The currency storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currencyStorage;
/**
* The input parser.
*
* @var \Commercie\Currency\InputInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $input;
/**
* The plugin definiton.
*
* @var mixed[]
*/
protected $pluginDefinition;
/**
* The string translator.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $stringTranslation;
/**
* The class under test.
*
* @var \Drupal\currency\Plugin\Filter\CurrencyLocalize
*/
protected $sut;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$configuration = [];
$plugin_id = $this
->randomMachineName();
$this->pluginDefinition = [
'cache' => TRUE,
'provider' => $this
->randomMachineName(),
];
$this->cacheContextsManager = $this
->getMockBuilder(CacheContextsManager::class)
->disableOriginalConstructor()
->getMock();
$this->cacheContextsManager
->expects($this
->any())
->method('assertValidTokens')
->willReturn(TRUE);
$this->currencyStorage = $this
->createMock(EntityStorageInterface::class);
$this->input = $this
->createMock(InputInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$container = new Container();
$container
->set('cache_contexts_manager', $this->cacheContextsManager);
\Drupal::setContainer($container);
$this->sut = new CurrencyLocalize($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage, $this->input);
}
/**
* @covers ::create
* @covers ::__construct
*/
function testCreate() {
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_manager
->expects($this
->atLeastOnce())
->method('getStorage')
->with('currency')
->willReturn($this->currencyStorage);
$container = $this
->createMock(ContainerInterface::class);
$map = [
[
'entity_type.manager',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$entity_type_manager,
],
[
'currency.input',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->input,
],
[
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
],
];
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$sut = CurrencyLocalize::create($container, [], '', $this->pluginDefinition);
$this
->assertInstanceOf(CurrencyLocalize::class, $sut);
}
/**
* @covers ::process
* @covers ::processCallback
*/
function testProcess() {
$cache_contexts = Cache::mergeContexts([
'baz',
'qux',
]);
$cache_tags = Cache::mergeTags([
'foo',
'bar',
]);
$map = [
[
'100',
TRUE,
LanguageInterface::TYPE_CONTENT,
'€100.00',
],
[
'100.7654',
TRUE,
LanguageInterface::TYPE_CONTENT,
'€100.77',
],
[
'1.99',
TRUE,
LanguageInterface::TYPE_CONTENT,
'€1.99',
],
[
'2.99',
TRUE,
LanguageInterface::TYPE_CONTENT,
'€2.99',
],
];
$currency = $this
->createMock(CurrencyInterface::class);
$currency
->expects($this
->any())
->method('formatAmount')
->willReturnMap($map);
$currency
->expects($this
->atLeastOnce())
->method('getCacheContexts')
->willReturn($cache_contexts);
$currency
->expects($this
->atLeastOnce())
->method('getCacheTags')
->willReturn($cache_tags);
$this->currencyStorage
->expects($this
->any())
->method('load')
->with('EUR')
->willReturn($currency);
$this->input
->expects($this
->any())
->method('parseAmount')
->will($this
->returnArgument(0));
$langcode = $this
->randomMachineName(2);
$tokens_valid = [
'[currency-localize:EUR:100]' => '€100.00',
'[currency-localize:EUR:100.7654]' => '€100.77',
'[currency-localize:EUR:1.99]' => '€1.99',
'[currency-localize:EUR:2.99]' => '€2.99',
];
$tokens_invalid = [
// Missing arguments.
'[currency-localize]',
'[currency-localize:]',
'[currency-localize::]',
'[currency-localize:EUR]',
// Invalid currency code.
'[currency-localize:123:456]',
// Invalid currency code and missing argument.
'[currency-localize:123]',
];
foreach ($tokens_valid as $token => $replacement) {
$result = $this->sut
->process($token, $langcode);
$this
->assertInstanceOf(FilterProcessResult::class, $result);
$this
->assertSame($replacement, $result
->getProcessedText());
$this
->assertSame($cache_contexts, $result
->getCacheContexts());
$this
->assertSame($cache_tags, $result
->getCacheTags());
}
foreach ($tokens_invalid as $token) {
$result = $this->sut
->process($token, $langcode);
$this
->assertInstanceOf(FilterProcessResult::class, $result);
$this
->assertSame($token, $result
->getProcessedText());
$this
->assertEmpty($result
->getCacheContexts());
$this
->assertEmpty($result
->getCacheTags());
}
}
/**
* @covers ::tips
*/
public function testTips() {
$tips = $this->sut
->tips();
$this
->assertInstanceOf(TranslatableMarkup::class, $tips);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrencyLocalizeTest:: |
protected | property | The cache contexts manager. | |
CurrencyLocalizeTest:: |
protected | property | The currency storage. | |
CurrencyLocalizeTest:: |
protected | property | The input parser. | |
CurrencyLocalizeTest:: |
protected | property | The plugin definiton. | |
CurrencyLocalizeTest:: |
protected | property | The string translator. | |
CurrencyLocalizeTest:: |
protected | property | The class under test. | |
CurrencyLocalizeTest:: |
public | function |
Overrides UnitTestCase:: |
|
CurrencyLocalizeTest:: |
function | @covers ::create @covers ::__construct | ||
CurrencyLocalizeTest:: |
function | @covers ::process @covers ::processCallback | ||
CurrencyLocalizeTest:: |
public | function | @covers ::tips | |
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. |