class CurrencyTest in Currency 8.3
Same name in this branch
- 8.3 tests/src/Kernel/CurrencyTest.php \Drupal\Tests\currency\Kernel\CurrencyTest
- 8.3 tests/src/Unit/Entity/CurrencyTest.php \Drupal\Tests\currency\Unit\Entity\CurrencyTest
- 8.3 tests/src/Unit/Plugin/views/filter/CurrencyTest.php \Drupal\Tests\currency\Unit\Plugin\views\filter\CurrencyTest
- 8.3 tests/src/Unit/Plugin/views/field/CurrencyTest.php \Drupal\Tests\currency\Unit\Plugin\views\field\CurrencyTest
@coversDefaultClass \Drupal\currency\Plugin\views\field\Currency
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\Plugin\views\field\CurrencyTest
Expanded class hierarchy of CurrencyTest
File
- tests/
src/ Unit/ Plugin/ views/ field/ CurrencyTest.php, line 19
Namespace
Drupal\Tests\currency\Unit\Plugin\views\fieldView source
class CurrencyTest extends UnitTestCase {
/**
* The currency storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currencyStorage;
/**
* The plugin configuration.
*
* @var mixed[]
*/
protected $pluginConfiguration;
/**
* 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\views\field\Currency
*/
protected $sut;
/**
* {@inheritdoc}
*/
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);
}
/**
* @covers ::__construct
*/
public function testConstructWithoutMethod() {
$this
->expectException(\InvalidArgumentException::class);
$plugin_id = $this
->randomMachineName();
$this->sut = new Currency([], $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage);
}
/**
* @covers ::__construct
*/
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);
}
/**
* @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,
],
[
'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);
}
/**
* @covers ::render
*/
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));
}
/**
* @covers ::render
*/
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));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrencyTest:: |
protected | property | The currency storage. | |
CurrencyTest:: |
protected | property | The plugin configuration. | |
CurrencyTest:: |
protected | property | The plugin definiton. | |
CurrencyTest:: |
protected | property | The string translator. | |
CurrencyTest:: |
protected | property | The class under test. | |
CurrencyTest:: |
public | function |
Overrides UnitTestCase:: |
|
CurrencyTest:: |
public | function | @covers ::__construct | |
CurrencyTest:: |
public | function | @covers ::__construct | |
CurrencyTest:: |
function | @covers ::create @covers ::__construct | ||
CurrencyTest:: |
function | @covers ::render | ||
CurrencyTest:: |
function | @covers ::render | ||
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. |