class BasicTest in Currency 8.3
@coversDefaultClass \Drupal\currency\Plugin\Currency\AmountFormatter\Basic
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\Plugin\Currency\AmountFormatter\BasicTest
Expanded class hierarchy of BasicTest
File
- tests/
src/ Unit/ Plugin/ Currency/ AmountFormatter/ BasicTest.php, line 18
Namespace
Drupal\Tests\currency\Unit\Plugin\Currency\AmountFormatterView source
class BasicTest extends UnitTestCase {
/**
* The locale resolver.
*
* @var \Drupal\currency\LocaleResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $localeResolver;
/**
* The string translator.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stringTranslation;
/**
* The class under test.
*
* @var \Drupal\currency\Plugin\Currency\AmountFormatter\Basic
*/
protected $sut;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$configuration = array();
$plugin_id = $this
->randomMachineName();
$plugin_definition = array();
$this->localeResolver = $this
->createMock(LocaleResolverInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$this->sut = new Basic($configuration, $plugin_id, $plugin_definition, $this->stringTranslation, $this->localeResolver);
}
/**
* @covers ::create
* @covers ::__construct
*/
function testCreate() {
$container = $this
->createMock(ContainerInterface::class);
$map = array(
array(
'currency.locale_resolver',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->localeResolver,
),
array(
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
),
);
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$configuration = array();
$plugin_id = $this
->randomMachineName();
$plugin_definition = array();
$sut = Basic::create($container, $configuration, $plugin_id, $plugin_definition);
$this
->assertInstanceOf(Basic::class, $sut);
}
/**
* @covers ::formatAmount
*/
function testFormatAmount() {
$decimal_separator = '@';
$grouping_separator = '%';
$currency_locale = $this
->createMock(CurrencyLocaleInterface::class);
$currency_locale
->expects($this
->any())
->method('getDecimalSeparator')
->willReturn($decimal_separator);
$currency_locale
->expects($this
->any())
->method('getGroupingSeparator')
->willReturn($grouping_separator);
$this->localeResolver
->expects($this
->any())
->method('resolveCurrencyLocale')
->willReturn($currency_locale);
// The formatter must not alter the decimals.
$amount = '987654.321';
$currency_sign = '₴';
$currency_code = 'UAH';
$currency_decimals = 2;
$currency = $this
->createMock(CurrencyInterface::class);
$currency
->expects($this
->any())
->method('getCurrencyCode')
->willReturn($currency_code);
$currency
->expects($this
->any())
->method('getDecimals')
->willReturn($currency_decimals);
$currency
->expects($this
->any())
->method('getSign')
->willReturn($currency_sign);
$translation = 'UAH 987%654@321';
$arguments = array(
'@currency_code' => 'UAH',
'@currency_sign' => '₴',
'@amount' => '987%654@321',
);
$formatted_amount = $this->sut
->formatAmount($currency, $amount);
$this
->assertInstanceOf(TranslatableMarkup::class, $formatted_amount);
$this
->assertSame('@currency_code @amount', $formatted_amount
->getUntranslatedString());
$this
->assertSame($arguments, $formatted_amount
->getArguments());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BasicTest:: |
protected | property | The locale resolver. | |
BasicTest:: |
protected | property | The string translator. | |
BasicTest:: |
protected | property | The class under test. | |
BasicTest:: |
public | function |
Overrides UnitTestCase:: |
|
BasicTest:: |
function | @covers ::create @covers ::__construct | ||
BasicTest:: |
function | @covers ::formatAmount | ||
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. |