View source
<?php
namespace Drupal\Tests\payment_reference\Unit\Element;
use Drupal\Component\Utility\Random;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodManagerInterface;
use Drupal\payment\QueueInterface;
use Drupal\payment_reference\Element\PaymentReference;
use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface;
use Drupal\plugin\PluginType\PluginTypeInterface;
use Drupal\plugin\PluginType\PluginTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class PaymentReferenceTest extends UnitTestCase {
protected $currentUser;
protected $dateFormatter;
protected $linkGenerator;
protected $paymentMethodManager;
protected $paymentMethodType;
protected $paymentQueue;
protected $paymentStorage;
protected $pluginSelectorManager;
protected $renderer;
protected $requestStack;
protected $stringTranslation;
protected $sut;
protected $urlGenerator;
public function setUp() : void {
$this->currentUser = $this
->createMock(AccountInterface::class);
$this->dateFormatter = $this
->getMockBuilder(DateFormatter::class)
->disableOriginalConstructor()
->getMock();
$this->linkGenerator = $this
->createMock(LinkGeneratorInterface::class);
$this->paymentMethodManager = $this
->createMock(PaymentMethodManagerInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$this->paymentMethodType = $this
->prophesize(PluginTypeInterface::class);
$this->paymentQueue = $this
->createMock(QueueInterface::class);
$this->paymentStorage = $this
->createMock(EntityStorageInterface::class);
$this->pluginSelectorManager = $this
->createMock(PluginSelectorManagerInterface::class);
$this->renderer = $this
->createMock(RendererInterface::class);
$this->requestStack = $this
->getMockBuilder(RequestStack::class)
->disableOriginalConstructor()
->getMock();
$this->urlGenerator = $this
->createMock(UrlGeneratorInterface::class);
$configuration = [];
$plugin_id = $this
->randomMachineName();
$plugin_definition = [];
$this->sut = new PaymentReference($configuration, $plugin_id, $plugin_definition, $this->requestStack, $this->paymentStorage, $this->stringTranslation, $this->dateFormatter, $this->linkGenerator, $this->renderer, $this->currentUser, $this->pluginSelectorManager, $this->paymentMethodType
->reveal(), new Random(), $this->paymentQueue);
}
function testCreate() {
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_manager
->expects($this
->once())
->method('getStorage')
->with('payment')
->willReturn($this->paymentStorage);
$plugin_type_manager = $this
->prophesize(PluginTypeManagerInterface::class);
$plugin_type_manager
->getPluginType('payment_method')
->willReturn($this->paymentMethodType
->reveal());
$container = $this
->createMock(ContainerInterface::class);
$map = array(
array(
'current_user',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->currentUser,
),
array(
'date.formatter',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->dateFormatter,
),
array(
'entity_type.manager',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$entity_type_manager,
),
array(
'link_generator',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->linkGenerator,
),
array(
'payment_reference.queue',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->paymentQueue,
),
array(
'plugin.manager.plugin.plugin_selector',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->pluginSelectorManager,
),
array(
'plugin.plugin_type_manager',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$plugin_type_manager
->reveal(),
),
array(
'renderer',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->renderer,
),
array(
'request_stack',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->requestStack,
),
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 = PaymentReference::create($container, $configuration, $plugin_id, $plugin_definition);
$this
->assertInstanceOf(PaymentReference::class, $sut);
}
public function testGetPaymentQueue() {
$method = new \ReflectionMethod($this->sut, 'getPaymentQueue');
$method
->setAccessible(TRUE);
$this
->assertSame($this->paymentQueue, $method
->invoke($this->sut));
}
}