View source
<?php
namespace Drupal\Tests\currency\Unit\Plugin\views\field;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\currency\Entity\CurrencyInterface;
use Drupal\currency\Plugin\views\field\Currency;
use Drupal\Tests\UnitTestCase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CurrencyTest extends UnitTestCase {
protected $currencyStorage;
protected $pluginConfiguration;
protected $pluginDefinition;
protected $stringTranslation;
protected $sut;
public function setUp() : void {
$this->pluginConfiguration = [
'currency_method' => 'label',
];
$plugin_id = $this
->randomMachineName();
$this->pluginDefinition = [];
$this->currencyStorage = $this
->createMock(EntityStorageInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$this->sut = new Currency($this->pluginConfiguration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage);
}
public function testConstructWithoutMethod() {
$this
->expectException(\InvalidArgumentException::class);
$plugin_id = $this
->randomMachineName();
$this->sut = new Currency([], $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage);
}
public function testConstructWithNonExistentMethod() {
$this
->expectException(\InvalidArgumentException::class);
$configuration = [
'currency_method' => $this
->randomMachineName(),
];
$plugin_id = $this
->randomMachineName();
$this->sut = new Currency($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage);
}
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,
],
[
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
],
];
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$sut = Currency::create($container, $this->pluginConfiguration, '', $this->pluginDefinition);
$this
->assertInstanceOf(Currency::class, $sut);
}
function testRenderWithExistingCurrency() {
$currency_code = $this
->randomMachineName();
$currency_method_return_value = $this
->randomMachineName();
$field_alias = $this
->randomMachineName();
$this->sut->field_alias = $field_alias;
$result_row = new ResultRow([
$field_alias => $currency_code,
]);
$currency = $this
->createMock(CurrencyInterface::class);
$currency
->expects($this
->atLeastOnce())
->method($this->pluginConfiguration['currency_method'])
->willReturn($currency_method_return_value);
$this->currencyStorage
->expects($this
->atLeastOnce())
->method('load')
->with($currency_code)
->willReturn($currency);
$this
->assertSame($currency_method_return_value, $this->sut
->render($result_row));
}
function testRenderWithNonExistingCurrency() {
$currency_code = $this
->randomMachineName();
$field_alias = $this
->randomMachineName();
$this->sut->field_alias = $field_alias;
$result_row = new ResultRow([
$field_alias => $currency_code,
]);
$this->currencyStorage
->expects($this
->atLeastOnce())
->method('load')
->with($currency_code)
->willReturn(NULL);
$this
->assertInstanceOf(MarkupInterface::class, $this->sut
->render($result_row));
}
}