class PaymentLineItemsDisplayTest in Payment 8.2
@coversDefaultClass \Drupal\payment\Element\PaymentLineItemsDisplay
@group Payment
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\payment\Unit\Element\PaymentLineItemsDisplayTest
Expanded class hierarchy of PaymentLineItemsDisplayTest
File
- tests/
src/ Unit/ Element/ PaymentLineItemsDisplayTest.php, line 21
Namespace
Drupal\Tests\payment\Unit\ElementView source
class PaymentLineItemsDisplayTest extends UnitTestCase {
/**
* The currency storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $currencyStorage;
/**
* The string translator.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $stringTranslation;
/**
* The class under test.
*
* @var \Drupal\payment\Element\PaymentLineItemsDisplay
*/
protected $sut;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$this->currencyStorage = $this
->createMock(EntityStorageInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$configuration = [];
$plugin_id = $this
->randomMachineName();
$plugin_definition = [];
$this->sut = new PaymentLineItemsDisplay($configuration, $plugin_id, $plugin_definition, $this->stringTranslation, $this->currencyStorage);
}
/**
* @covers ::__construct
* @covers ::create
*/
function testCreate() {
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_manager
->expects($this
->once())
->method('getStorage')
->with('currency')
->willReturn($this->currencyStorage);
$container = $this
->createMock(ContainerInterface::class);
$map = array(
array(
'entity_type.manager',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$entity_type_manager,
),
array(
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
),
);
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$configuration = [];
$plugin_id = $this
->randomMachineName();
$plugin_definition = [];
$sut = PaymentLineItemsDisplay::create($container, $configuration, $plugin_id, $plugin_definition);
$this
->assertInstanceOf(PaymentLineItemsDisplay::class, $sut);
}
/**
* @covers ::getInfo
*/
public function testGetInfo() {
$info = $this->sut
->getInfo();
$this
->assertIsArray($info);
foreach ($info['#pre_render'] as $callback) {
$this
->assertTrue(is_callable($callback));
}
}
/**
* @covers ::preRender
*/
public function testPreRender() {
$payment_line_item_amount = mt_rand();
$payment_line_item_total_amount = mt_rand();
$payment_line_item_currency_code = $this
->randomMachineName();
$payment_line_item = $this
->createMock(PaymentLineItemInterface::class);
$payment_line_item
->expects($this
->atLeastOnce())
->method('getAmount')
->willReturn($payment_line_item_amount);
$payment_line_item
->expects($this
->atLeastOnce())
->method('getCurrencyCode')
->willReturn($payment_line_item_currency_code);
$payment_line_item
->expects($this
->atLeastOnce())
->method('getTotalAmount')
->willReturn($payment_line_item_total_amount);
$payment_line_items_currency_code = $this
->randomMachineName();
$payment_line_items = $this
->createMock(LineItemCollectionInterface::class);
$payment_line_items
->expects($this
->atLeastOnce())
->method('getCurrencyCode')
->willReturn($payment_line_items_currency_code);
$payment_line_items
->expects($this
->atLeastOnce())
->method('getLineItems')
->willReturn([
$payment_line_item,
]);
$payment_line_item_currency = $this
->createMock(CurrencyInterface::class);
$payment_line_item_currency
->expects($this
->exactly(2))
->method('formatAmount');
$payment_line_items_currency = $this
->createMock(CurrencyInterface::class);
$payment_line_items_currency
->expects($this
->once())
->method('formatAmount');
$map = array(
array(
$payment_line_item_currency_code,
$payment_line_item_currency,
),
array(
$payment_line_items_currency_code,
$payment_line_items_currency,
),
);
$this->currencyStorage
->expects($this
->atLeast(count($map)))
->method('load')
->willReturnMap($map);
$element = array(
'#payment_line_items' => $payment_line_items,
);
$build = $this->sut
->preRender($element);
$this
->assertSame('table', $build['table']['#type']);
}
/**
* @covers ::preRender
*/
public function testPreRenderWithoutPaymentLineItems() {
$this
->expectException(\InvalidArgumentException::class);
$element = [
'#payment' => $this
->createMock(PaymentInterface::class),
];
$this->sut
->preRender($element);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PaymentLineItemsDisplayTest:: |
protected | property | The currency storage. | |
PaymentLineItemsDisplayTest:: |
protected | property | The string translator. | |
PaymentLineItemsDisplayTest:: |
protected | property | The class under test. | |
PaymentLineItemsDisplayTest:: |
public | function |
Overrides UnitTestCase:: |
|
PaymentLineItemsDisplayTest:: |
function | @covers ::__construct @covers ::create | ||
PaymentLineItemsDisplayTest:: |
public | function | @covers ::getInfo | |
PaymentLineItemsDisplayTest:: |
public | function | @covers ::preRender | |
PaymentLineItemsDisplayTest:: |
public | function | @covers ::preRender | |
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. |