View source
<?php
namespace Drupal\Tests\currency\Unit\Entity;
use Commercie\Currency\Usage;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\currency\Entity\Currency;
use Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterInterface;
use Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManagerInterface;
use Drupal\Tests\UnitTestCase;
class CurrencyTest extends UnitTestCase {
protected $currencyAmountFormatterManager;
protected $entityTypeManager;
protected $entityTypeId;
protected $sut;
function setUp() : void {
$this->entityTypeId = $this
->randomMachineName();
$this->currencyAmountFormatterManager = $this
->createMock(AmountFormatterManagerInterface::class);
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->sut = new Currency([], $this->entityTypeId);
$this->sut
->setCurrencyAmountFormatterManager($this->currencyAmountFormatterManager);
$this->sut
->setEntityTypeManager($this->entityTypeManager);
}
public function testGetCurrencyAmountFormatterManager() {
$method = new \ReflectionMethod($this->sut, 'getCurrencyAmountFormatterManager');
$method
->setAccessible(TRUE);
$this
->assertSame($this->sut, $this->sut
->setCurrencyAmountFormatterManager($this->currencyAmountFormatterManager));
$this
->assertSame($this->currencyAmountFormatterManager, $method
->invoke($this->sut));
}
public function testEntityTypeManager() {
$method = new \ReflectionMethod($this->sut, 'entityTypeManager');
$method
->setAccessible(TRUE);
$this
->assertSame($this->sut, $this->sut
->setEntityTypeManager($this->entityTypeManager));
$this
->assertSame($this->entityTypeManager, $method
->invoke($this->sut));
}
function testGetRoundingStep() {
$rounding_step = mt_rand();
$this
->assertSame($this->sut, $this->sut
->setRoundingStep($rounding_step));
$this
->assertSame($rounding_step, $this->sut
->getRoundingStep());
}
function testGetRoundingStepBySubunits() {
$subunits = 5;
$rounding_step = '0.200000';
$this->sut
->setSubunits($subunits);
$this
->assertSame($rounding_step, $this->sut
->getRoundingStep());
}
function testGetRoundingStepUnavailable() {
$this
->assertNull($this->sut
->getRoundingStep());
}
function testFormatAmount($expected, $amount, $amount_with_currency_precision_applied) {
$amount_formatter = $this
->createMock(AmountFormatterInterface::class);
$amount_formatter
->expects($this
->atLeastOnce())
->method('formatAmount')
->with($this->sut, $amount_with_currency_precision_applied)
->willReturn($expected);
$this->currencyAmountFormatterManager
->expects($this
->atLeastOnce())
->method('getDefaultPlugin')
->willReturn($amount_formatter);
$this->sut
->setCurrencyCode('BLA');
$this->sut
->setSubunits(100);
$this
->assertSame($expected, $this->sut
->formatAmount($amount, $amount !== $amount_with_currency_precision_applied));
}
public function providerTestFormatAmount() {
return [
[
'BLA 12,345.68',
'12345.6789',
'12345.68',
],
[
'BLA 12,345.6789',
'12345.6789',
'12345.6789',
],
];
}
function testGetDecimals() {
foreach ([
1,
2,
3,
] as $decimals) {
$this->sut
->setSubunits(pow(10, $decimals));
$this
->assertSame($decimals, $this->sut
->getDecimals());
}
}
function testIsObsolete() {
$this
->assertFalse($this->sut
->isObsolete());
$usage = new Usage();
$usage
->setStart('1813-01-01')
->setEnd('2002-02-28');
$this->sut
->setUsages([
$usage,
]);
$this
->assertTrue($this->sut
->isObsolete());
$usage = new Usage();
$usage
->setStart('1813-01-01')
->setEnd(date('o') + 1 . '-02-28');
$this->sut
->setUsages([
$usage,
]);
$this
->assertFalse($this->sut
->isObsolete());
}
function testGetAlternativeSigns() {
$alternative_signs = [
'A',
'B',
];
$this
->assertSame($this->sut, $this->sut
->setAlternativeSigns($alternative_signs));
$this
->assertSame($alternative_signs, $this->sut
->getAlternativeSigns());
}
function testId() {
$currency_code = $this
->randomMachineName(3);
$this
->assertSame($this->sut, $this->sut
->setCurrencyCode($currency_code));
$this
->assertSame($currency_code, $this->sut
->id());
}
function testGetCurrencyCode() {
$currency_code = $this
->randomMachineName(3);
$this
->assertSame($this->sut, $this->sut
->setCurrencyCode($currency_code));
$this
->assertSame($currency_code, $this->sut
->getCurrencyCode());
}
function testGetCurrencyNumber() {
$currency_number = '000';
$this
->assertSame($this->sut, $this->sut
->setCurrencyNumber($currency_number));
$this
->assertSame($currency_number, $this->sut
->getCurrencyNumber());
}
function testLabel() {
$entity_type = $this
->createMock(EntityTypeInterface::class);
$entity_type
->expects($this
->atLeastOnce())
->method('getKey')
->with('label')
->willReturn('label');
$this->entityTypeManager
->expects($this
->atLeastOnce())
->method('getDefinition')
->with($this->entityTypeId)
->willReturn($entity_type);
$label = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setLabel($label));
$this
->assertSame($label, $this->sut
->label());
}
function testGetSign() {
$sign = $this
->randomMachineName(1);
$this
->assertSame($this->sut, $this->sut
->setSign($sign));
$this
->assertSame($sign, $this->sut
->getSign());
}
function testGetSubunits() {
$subunits = 73;
$this
->assertSame($this->sut, $this->sut
->setSubunits($subunits));
$this
->assertSame($subunits, $this->sut
->getSubunits());
}
function testGetUsage() {
$usage = new Usage();
$usage
->setStart('1813-01-01')
->setEnd(date('o') + 1 . '-02-28');
$this
->assertSame($this->sut, $this->sut
->setUsages([
$usage,
]));
$this
->assertSame([
$usage,
], $this->sut
->getUsages());
}
}