View source
<?php
namespace Drupal\Tests\payment\Unit\Controller;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\payment\Controller\ConfigurePaymentType;
use Drupal\payment\Plugin\Payment\Type\PaymentTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ConfigurePaymentTypeTest extends UnitTestCase {
protected $formBuilder;
protected $paymentTypeManager;
protected $stringTranslation;
protected $sut;
protected function setUp() : void {
$this->formBuilder = $this
->createMock(FormBuilderInterface::class);
$this->paymentTypeManager = $this
->createMock(PaymentTypeManagerInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$this->sut = new ConfigurePaymentType($this->formBuilder, $this->paymentTypeManager, $this->stringTranslation);
}
function testCreate() {
$container = $this
->createMock(ContainerInterface::class);
$map = [
[
'form_builder',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->formBuilder,
],
[
'plugin.manager.payment.type',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->paymentTypeManager,
],
[
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
],
];
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$sut = ConfigurePaymentType::create($container);
$this
->assertInstanceOf(ConfigurePaymentType::class, $sut);
}
public function testExecute() {
$bundle_exists = $this
->randomMachineName();
$bundle_exists_definition = [
'configuration_form' => $this
->randomMachineName(),
];
$bundle_exists_no_form = $this
->randomMachineName();
$bundle_exists_no_form_definition = [];
$bundle_no_exists = $this
->randomMachineName();
$bundle_no_exists_definition = NULL;
$form_build = [
'#type' => $this
->randomMachineName(),
];
$this->formBuilder
->expects($this
->once())
->method('getForm')
->with($bundle_exists_definition['configuration_form'])
->willReturn($form_build);
$map = [
[
$bundle_exists,
FALSE,
$bundle_exists_definition,
],
[
$bundle_exists_no_form,
FALSE,
$bundle_exists_no_form_definition,
],
[
$bundle_no_exists,
FALSE,
$bundle_no_exists_definition,
],
];
$this->paymentTypeManager
->expects($this
->any())
->method('getDefinition')
->willReturnMap($map);
$build = $this->sut
->execute($bundle_exists);
$this
->assertSame($form_build, $build);
$build = $this->sut
->execute($bundle_exists_no_form);
$this
->assertIsArray($build);
$this
->expectException(NotFoundHttpException::class);
$this->sut
->execute($bundle_no_exists);
}
public function testTitle() {
$plugin_id = $this
->randomMachineName();
$label = $this
->randomMachineName();
$definition = [
'label' => $label,
];
$this->paymentTypeManager
->expects($this
->once())
->method('getDefinition')
->with($plugin_id)
->willReturn($definition);
$this
->assertSame($label, $this->sut
->title($plugin_id));
}
}