You are here

class ConfigurePaymentTypeTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Controller\ConfigurePaymentType

@group Payment

Hierarchy

Expanded class hierarchy of ConfigurePaymentTypeTest

File

tests/src/Unit/Controller/ConfigurePaymentTypeTest.php, line 17

Namespace

Drupal\Tests\payment\Unit\Controller
View source
class ConfigurePaymentTypeTest extends UnitTestCase {

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $formBuilder;

  /**
   * The payment type plugin manager.
   *
   * @var \Drupal\payment\Plugin\Payment\Type\PaymentTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentTypeManager;

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $stringTranslation;

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Controller\ConfigurePaymentType
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  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);
  }

  /**
   * @covers ::execute
   */
  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);

    // Test with a bundle of a plugin with a form.
    $build = $this->sut
      ->execute($bundle_exists);
    $this
      ->assertSame($form_build, $build);

    // Test with a bundle of a plugin without a form.
    $build = $this->sut
      ->execute($bundle_exists_no_form);
    $this
      ->assertIsArray($build);

    // Test with a non-existing bundle.
    $this
      ->expectException(NotFoundHttpException::class);
    $this->sut
      ->execute($bundle_no_exists);
  }

  /**
   * @covers ::title
   */
  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));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurePaymentTypeTest::$formBuilder protected property The form builder.
ConfigurePaymentTypeTest::$paymentTypeManager protected property The payment type plugin manager.
ConfigurePaymentTypeTest::$stringTranslation protected property The string translator.
ConfigurePaymentTypeTest::$sut protected property The class under test.
ConfigurePaymentTypeTest::setUp protected function Overrides UnitTestCase::setUp
ConfigurePaymentTypeTest::testCreate function @covers ::create @covers ::__construct
ConfigurePaymentTypeTest::testExecute public function @covers ::execute
ConfigurePaymentTypeTest::testTitle public function @covers ::title
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.