You are here

class PaymentReferenceConfigurationFormTest in Payment 8.2

@coversDefaultClass \Drupal\payment_reference\Plugin\Payment\Type\PaymentReferenceConfigurationForm

@group Payment Reference Field

Hierarchy

Expanded class hierarchy of PaymentReferenceConfigurationFormTest

File

modules/payment_reference/tests/src/Unit/Plugin/Payment/Type/PaymentReferenceConfigurationFormTest.php, line 21

Namespace

Drupal\Tests\payment_reference\Unit\Plugin\Payment\Type
View source
class PaymentReferenceConfigurationFormTest extends UnitTestCase {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $configFactory;

  /**
   * The configuration the config factory returns.
   *
   * @see self::__construct
   *
   * @var array
   */
  protected $configFactoryConfiguration = [];

  /**
   * The payment method manager.
   *
   * @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentMethodManager;

  /**
   * The plugin selector.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $pluginSelector;

  /**
   * The plugin selector manager.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $pluginSelectorManager;

  /**
   * The plugin selector plugin type.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $pluginSelectorType;

  /**
   * The selected plugin selector.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $selectedPluginSelector;

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

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $messenger;

  /**
   * The class under test.
   *
   * @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReferenceConfigurationForm
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->configFactoryConfiguration = array(
      'payment_reference.payment_type' => array(
        'limit_allowed_plugins' => TRUE,
        'allowed_plugin_ids' => array(
          $this
            ->randomMachineName(),
        ),
        'plugin_selector_id' => $this
          ->randomMachineName(),
      ),
    );
    $this->configFactory = $this
      ->getConfigFactoryStub($this->configFactoryConfiguration);
    $this->paymentMethodManager = $this
      ->createMock(PaymentMethodManagerInterface::class);
    $this->pluginSelector = $this
      ->createMock(PluginSelectorInterface::class);
    $this->pluginSelectorManager = $this
      ->createMock(PluginSelectorManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->pluginSelectorType = $this
      ->prophesize(PluginTypeInterface::class);
    $this->pluginSelectorType
      ->getPluginManager()
      ->willReturn($this->pluginSelectorManager);
    $this->selectedPluginSelector = $this
      ->createMock(PluginSelectorInterface::class);
    $this->messenger = $this
      ->createMock(MessengerInterface::class);
    $this->sut = new PaymentReferenceConfigurationForm($this->configFactory, $this->stringTranslation, $this->paymentMethodManager, $this->pluginSelectorType
      ->reveal());
    $this->sut
      ->setMessenger($this->messenger);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $plugin_type_manager = $this
      ->prophesize(PluginTypeManagerInterface::class);
    $plugin_type_manager
      ->getPluginType('plugin_selector')
      ->willReturn($this->pluginSelectorType
      ->reveal());
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'config.factory',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->configFactory,
      ),
      array(
        'plugin.manager.payment.method',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->paymentMethodManager,
      ),
      [
        'plugin.plugin_type_manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $plugin_type_manager
          ->reveal(),
      ],
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = PaymentReferenceConfigurationForm::create($container);
    $this
      ->assertInstanceOf(PaymentReferenceConfigurationForm::class, $sut);
  }

  /**
   * @covers ::getFormId
   */
  public function testGetFormId() {
    $this
      ->assertIsString($this->sut
      ->getFormId());
  }

  /**
   * @covers ::buildForm
   * @covers ::getPluginSelector
   */
  public function testBuildForm() {
    $form = [];
    $form_state = new FormState();
    $map = [
      [
        'payment_radios',
        [],
        $this->pluginSelector,
      ],
      [
        $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'],
        [],
        $this->selectedPluginSelector,
      ],
    ];
    $this->pluginSelectorManager
      ->expects($this
      ->atLeast(count($map)))
      ->method('createInstance')
      ->willReturnMap($map);
    $this->pluginSelector
      ->expects($this
      ->once())
      ->method('buildSelectorForm')
      ->with([], $form_state)
      ->willReturn($this->pluginSelector);
    $this->paymentMethodManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn([]);
    $build = $this->sut
      ->buildForm($form, $form_state);
    $this
      ->assertIsArray($build);
  }

  /**
   * @covers ::validateForm
   * @covers ::getPluginSelector
   */
  public function testValidateForm() {
    $form = [
      'plugin_selector' => [
        'foo' => $this
          ->randomMachineName(),
      ],
    ];
    $form_state = new FormState();
    $form_state
      ->setValues([
      'plugin_selector_id' => $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'],
      'allowed_plugin_ids' => $this->configFactoryConfiguration['payment_reference.payment_type']['allowed_plugin_ids'],
      'limit_allowed_plugins' => $this->configFactoryConfiguration['payment_reference.payment_type']['limit_allowed_plugins'],
    ]);
    $map = [
      [
        'payment_radios',
        [],
        $this->pluginSelector,
      ],
      [
        $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'],
        [],
        $this->selectedPluginSelector,
      ],
    ];
    $this->pluginSelectorManager
      ->expects($this
      ->atLeast(count($map)))
      ->method('createInstance')
      ->willReturnMap($map);
    $this->pluginSelector
      ->expects($this
      ->once())
      ->method('validateSelectorForm')
      ->with($form['plugin_selector'], $form_state);
    $this->sut
      ->validateForm($form, $form_state);
  }

  /**
   * @covers ::submitForm
   * @covers ::getPluginSelector
   */
  public function testSubmitForm() {
    $form = [
      'plugin_selector' => [
        'foo' => $this
          ->randomMachineName(),
      ],
    ];
    $form_state = new FormState();
    $form_state
      ->setValues([
      'plugin_selector_id' => $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'],
      'allowed_plugin_ids' => $this->configFactoryConfiguration['payment_reference.payment_type']['allowed_plugin_ids'],
      'limit_allowed_plugins' => $this->configFactoryConfiguration['payment_reference.payment_type']['limit_allowed_plugins'],
    ]);
    $map = [
      [
        'payment_radios',
        [],
        $this->pluginSelector,
      ],
      [
        $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'],
        [],
        $this->selectedPluginSelector,
      ],
    ];
    $this->pluginSelectorManager
      ->expects($this
      ->atLeast(count($map)))
      ->method('createInstance')
      ->willReturnMap($map);
    $this->pluginSelector
      ->expects($this
      ->once())
      ->method('submitSelectorForm')
      ->with($form['plugin_selector'], $form_state);
    $this->pluginSelector
      ->expects($this
      ->once())
      ->method('getSelectedPlugin')
      ->willReturn($this->selectedPluginSelector);
    $this->sut
      ->submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentReferenceConfigurationFormTest::$configFactory protected property The config factory.
PaymentReferenceConfigurationFormTest::$configFactoryConfiguration protected property The configuration the config factory returns.
PaymentReferenceConfigurationFormTest::$messenger protected property The messenger.
PaymentReferenceConfigurationFormTest::$paymentMethodManager protected property The payment method manager.
PaymentReferenceConfigurationFormTest::$pluginSelector protected property The plugin selector.
PaymentReferenceConfigurationFormTest::$pluginSelectorManager protected property The plugin selector manager.
PaymentReferenceConfigurationFormTest::$pluginSelectorType protected property The plugin selector plugin type.
PaymentReferenceConfigurationFormTest::$selectedPluginSelector protected property The selected plugin selector.
PaymentReferenceConfigurationFormTest::$stringTranslation protected property The string translator.
PaymentReferenceConfigurationFormTest::$sut protected property The class under test.
PaymentReferenceConfigurationFormTest::setUp public function Overrides UnitTestCase::setUp
PaymentReferenceConfigurationFormTest::testBuildForm public function @covers ::buildForm @covers ::getPluginSelector
PaymentReferenceConfigurationFormTest::testCreate function @covers ::create @covers ::__construct
PaymentReferenceConfigurationFormTest::testGetFormId public function @covers ::getFormId
PaymentReferenceConfigurationFormTest::testSubmitForm public function @covers ::submitForm @covers ::getPluginSelector
PaymentReferenceConfigurationFormTest::testValidateForm public function @covers ::validateForm @covers ::getPluginSelector
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.