View source
<?php
namespace Drupal\Tests\payment\Unit\Controller;
use Drupal\Core\Render\RendererInterface;
use Drupal\payment\Controller\ListPaymentStatuses;
use Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface;
use Drupal\plugin\PluginOperationsProviderInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ListPaymentStatusesTest extends UnitTestCase {
protected $paymentStatusManager;
protected $renderer;
protected $stringTranslation;
protected $sut;
protected function setUp() : void {
$this->paymentStatusManager = $this
->createMock(PaymentStatusManagerInterface::class);
$this->renderer = $this
->createMock(RendererInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$this->sut = new ListPaymentStatuses($this->stringTranslation, $this->renderer, $this->paymentStatusManager);
}
function testCreate() {
$container = $this
->createMock(ContainerInterface::class);
$map = [
[
'plugin.manager.payment.status',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->paymentStatusManager,
],
[
'renderer',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->renderer,
],
[
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
],
];
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$sut = ListPaymentStatuses::create($container);
$this
->assertInstanceOf(ListPaymentStatuses::class, $sut);
}
function testListing() {
$plugin_id_a = $this
->randomMachineName();
$plugin_id_b = $this
->randomMachineName();
$definitions = [
$plugin_id_a => [
'label' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
],
$plugin_id_b => [
'label' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'parent_id' => $plugin_id_a,
],
];
$operations_a = [
'title' => $this
->randomMachineName(),
];
$operations_provider_a = $this
->createMock(PluginOperationsProviderInterface::class);
$operations_provider_a
->expects($this
->once())
->method('getOperations')
->with($plugin_id_a)
->willReturn($operations_a);
$map = [
[
$plugin_id_a,
TRUE,
$definitions[$plugin_id_a],
],
[
$plugin_id_b,
TRUE,
$definitions[$plugin_id_b],
],
];
$this->paymentStatusManager
->expects($this
->exactly(count($map)))
->method('getDefinition')
->willReturnMap($map);
$this->paymentStatusManager
->expects($this
->atLeastOnce())
->method('getDefinitions')
->willReturn($definitions);
$map = [
[
$plugin_id_a,
$operations_provider_a,
],
[
$plugin_id_b,
NULL,
],
];
$this->paymentStatusManager
->expects($this
->exactly(2))
->method('getOperationsProvider')
->willReturnMap($map);
$this->stringTranslation
->expects($this
->any())
->method('translate')
->will($this
->returnArgument(0));
$build = $this->sut
->execute();
foreach ($build['#header'] as $key => $label) {
$build['#header'][$key] = (string) $label;
}
$expected = [
'#header' => [
'Title',
'Description',
'Operations',
],
'#type' => 'table',
$plugin_id_a => [
'label' => [
'#markup' => $definitions[$plugin_id_a]['label'],
],
'description' => [
'#markup' => $definitions[$plugin_id_a]['description'],
],
'operations' => [
'#type' => 'operations',
'#links' => $operations_a,
],
],
$plugin_id_b => [
'label' => [
'#markup' => $definitions[$plugin_id_b]['label'],
],
'description' => [
'#markup' => $definitions[$plugin_id_b]['description'],
],
'operations' => [
'#type' => 'operations',
'#links' => [],
],
],
];
$this
->assertSame($expected, $build);
}
}