You are here

class PaymentFormTest in Payment 8.2

Same name in this branch
  1. 8.2 modules/payment_form/tests/src/Unit/Entity/Payment/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Entity\Payment\PaymentFormTest
  2. 8.2 modules/payment_form/tests/src/Unit/Plugin/Payment/Type/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Plugin\Payment\Type\PaymentFormTest
  3. 8.2 modules/payment_form/tests/src/Unit/Plugin/Field/FieldFormatter/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Plugin\Field\FieldFormatter\PaymentFormTest
  4. 8.2 modules/payment_form/tests/src/Unit/Plugin/Field/FieldWidget/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Plugin\Field\FieldWidget\PaymentFormTest
  5. 8.2 modules/payment_form/tests/src/Unit/Plugin/Field/FieldType/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Plugin\Field\FieldType\PaymentFormTest

@coversDefaultClass \Drupal\payment_form\Entity\Payment\PaymentForm

@group Payment Form Field

Hierarchy

Expanded class hierarchy of PaymentFormTest

File

modules/payment_form/tests/src/Unit/Entity/Payment/PaymentFormTest.php, line 35

Namespace

Drupal\Tests\payment_form\Unit\Entity\Payment
View source
class PaymentFormTest 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 current user.
   *
   * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $currentUser;

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityRepository;

  /**
   * The form display.
   *
   * @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $formDisplay;

  /**
   * A payment entity.
   *
   * @var \Drupal\payment\Entity\Payment|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $payment;

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

  /**
   * The payment method plugin type.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $paymentMethodType;

  /**
   * 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 string translation.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $stringTranslation;

  /**
   * The entity type bundle service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeBundleInfo;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $time;

  /**
   * The class under test.
   *
   * @var \Drupal\payment_form\Entity\Payment\PaymentForm
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->currentUser = $this
      ->createMock(AccountInterface::class);
    $this->entityRepository = $this
      ->createMock(EntityRepositoryInterface::class);
    $this->formDisplay = $this
      ->createMock(EntityFormDisplayInterface::class);
    $this->payment = $this
      ->createMock(PaymentInterface::class);
    $this->paymentMethodManager = $this
      ->createMock(PaymentMethodManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->paymentMethodType = $this
      ->prophesize(PluginTypeInterface::class);
    $this->paymentMethodType
      ->getPluginManager()
      ->willReturn($this->paymentMethodManager);
    $this->pluginSelector = $this
      ->createMock(PluginSelectorInterface::class);
    $this->pluginSelectorManager = $this
      ->createMock(PluginSelectorManagerInterface::class);
    $this->configFactoryConfiguration = [
      'payment_form.payment_type' => [
        'limit_allowed_plugins' => TRUE,
        'allowed_plugin_ids' => [
          $this
            ->randomMachineName(),
        ],
        'plugin_selector_id' => $this
          ->randomMachineName(),
      ],
    ];
    $this->configFactory = $this
      ->getConfigFactoryStub($this->configFactoryConfiguration);
    $this->entityTypeBundleInfo = $this
      ->prophesize(EntityTypeBundleInfoInterface::class)
      ->reveal();
    $this->time = $this
      ->prophesize(TimeInterface::class)
      ->reveal();
    $this->sut = new PaymentForm($this->entityRepository, $this->entityTypeBundleInfo, $this->time);
    $this->sut
      ->setStringTranslation($this->stringTranslation);
    $this->sut
      ->setConfigFactory($this->configFactory);
    $this->sut
      ->setEntity($this->payment);
    $this->sut
      ->setPluginSelectorManager($this->pluginSelectorManager);
    $this->sut
      ->setPaymentMethodPluginType($this->paymentMethodType
      ->reveal());
    $this->sut
      ->setCurrentUser($this->currentUser);
  }

  /**
   * @covers ::form
   * @covers ::getPluginSelector
   * @covers ::getPaymentMethodManager
   */
  public function testForm() {
    $plugin_selector_build = [
      '#type' => $this
        ->randomMachineName(),
    ];
    $this->pluginSelector
      ->expects($this
      ->atLeastOnce())
      ->method('buildSelectorForm')
      ->willReturn($plugin_selector_build);
    $this->pluginSelectorManager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($this->configFactoryConfiguration['payment_form.payment_type']['plugin_selector_id'])
      ->willReturn($this->pluginSelector);
    $payment_type = $this
      ->createMock(PaymentTypeInterface::class);
    $this->payment
      ->expects($this
      ->any())
      ->method('getPaymentType')
      ->willReturn($payment_type);
    $entity_type = $this
      ->createMock(EntityTypeInterface::class);
    $this->payment
      ->expects($this
      ->any())
      ->method('getEntityType')
      ->willReturn($entity_type);
    $form = [
      'langcode' => [],
    ];
    $form_state = new FormState();
    $this->sut
      ->setFormDisplay($this->formDisplay, $form_state);
    $build = $this->sut
      ->form($form, $form_state);

    // Build the form a second time to make sure the plugin selector is only
    // instantiated once.
    $this->sut
      ->form($form, $form_state);
    $this
      ->assertIsArray($build);
    $this
      ->assertArrayHasKey('line_items', $build);
    $this
      ->assertSame($this->payment, $build['line_items']['#payment_line_items']);
    $this
      ->assertArrayHasKey('payment_method', $build);
    $this
      ->assertSame($plugin_selector_build, $build['payment_method']);
  }

  /**
   * @covers ::validateForm
   * @covers ::getPluginSelector
   * @covers ::getPaymentMethodManager
   */
  public function testValidateForm() {
    $form = [
      'payment_method' => [
        '#type' => $this
          ->randomMachineName(),
      ],
    ];
    $form_state = new FormState();
    $form_display = $this
      ->prophesize(EntityFormDisplayInterface::class);
    $form_display
      ->extractFormValues(Argument::any(), Argument::any(), Argument::any())
      ->shouldBeCalled();
    $form_display
      ->flagWidgetsErrorsFromViolations(Argument::any(), Argument::any(), Argument::any())
      ->shouldBeCalled();
    $form_display
      ->getComponents()
      ->willReturn([]);
    $form_state
      ->set('form_display', $form_display
      ->reveal());
    $entity_type = new EntityType([
      'id' => 'payment',
    ]);
    $this->payment
      ->expects($this
      ->any())
      ->method('getEntityType')
      ->willReturn($entity_type);
    $this->payment
      ->expects($this
      ->any())
      ->method('validate')
      ->willReturn(new EntityConstraintViolationList($this->payment));
    $this->payment
      ->expects($this
      ->any())
      ->method('getFieldDefinitions')
      ->willReturn([]);
    $this->pluginSelectorManager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($this->configFactoryConfiguration['payment_form.payment_type']['plugin_selector_id'])
      ->willReturn($this->pluginSelector);
    $this->pluginSelector
      ->expects($this
      ->once())
      ->method('validateSelectorForm')
      ->with($form['payment_method'], $form_state);
    $this->sut
      ->validateForm($form, $form_state);
  }

  /**
   * @covers ::submitForm
   * @covers ::getPluginSelector
   * @covers ::getPaymentMethodManager
   */
  public function testSubmitForm() {
    $symfony_response = $this
      ->prophesize(Response::class);
    $completion_response = $this
      ->prophesize(ResponseInterface::class);
    $completion_response
      ->getResponse()
      ->willReturn($symfony_response
      ->reveal());
    $result = $this
      ->createMock(OperationResultInterface::class);
    $result
      ->expects($this
      ->atLeastOnce())
      ->method('getCompletionResponse')
      ->willReturn($completion_response
      ->reveal());
    $form = [
      'payment_method' => [
        '#type' => $this
          ->randomMachineName(),
      ],
    ];
    $form_state = new FormState();
    $form_display = $this
      ->prophesize(EntityFormDisplayInterface::class);
    $form_display
      ->extractFormValues(Argument::any(), $form, $form_state);
    $form_state
      ->set('form_display', $form_display
      ->reveal());
    $entity_type = new EntityType([
      'id' => 'payment',
    ]);
    $this->payment
      ->expects($this
      ->any())
      ->method('getEntityType')
      ->willReturn($entity_type);
    $payment_method = $this
      ->createMock(PaymentMethodInterface::class);
    $this->pluginSelectorManager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($this->configFactoryConfiguration['payment_form.payment_type']['plugin_selector_id'])
      ->willReturn($this->pluginSelector);
    $this->pluginSelector
      ->expects($this
      ->atLeastOnce())
      ->method('getSelectedPlugin')
      ->willReturn($payment_method);
    $this->pluginSelector
      ->expects($this
      ->atLeastOnce())
      ->method('submitSelectorForm')
      ->with($form['payment_method'], $form_state);
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('setPaymentMethod')
      ->with($payment_method);
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('save');
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('execute')
      ->willReturn($result);
    $this->sut
      ->submitForm($form, $form_state);
    $this
      ->assertSame($symfony_response
      ->reveal(), $form_state
      ->getResponse());
  }

  /**
   * @covers ::actions
   * @covers ::getPaymentMethodManager
   */
  public function testActionsWithAvailablePlugins() {
    $form = [];
    $form_state = new FormState();
    $form_state
      ->set('plugin_selector', $this->pluginSelector);
    $plugin_id_a = reset($this->configFactoryConfiguration['payment_form.payment_type']['allowed_plugin_ids']);
    $plugin_id_b = $this
      ->randomMachineName();
    $plugin_a = $this
      ->createMock(PaymentMethodInterface::class);
    $plugin_a
      ->expects($this
      ->atLeastOnce())
      ->method('executePaymentAccess')
      ->with($this->currentUser)
      ->willReturn(AccessResult::allowed());
    $plugin_definitions = [
      $plugin_id_a => [
        'id' => $plugin_id_a,
      ],
      $plugin_id_b => [
        'id' => $plugin_id_b,
      ],
    ];
    $this->paymentMethodManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn($plugin_definitions);
    $this->paymentMethodManager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($plugin_id_a)
      ->willReturn($plugin_a);
    $this->configFactory = $this
      ->getConfigFactoryStub($this->configFactoryConfiguration);
    $method = new \ReflectionMethod($this->sut, 'actions');
    $method
      ->setAccessible(TRUE);
    $actions = $method
      ->invokeArgs($this->sut, [
      $form,
      $form_state,
    ]);
    $this
      ->assertFalse($actions['submit']['#disabled']);
  }

  /**
   * @covers ::actions
   * @covers ::getPaymentMethodManager
   */
  public function testActionsWithoutAvailablePlugins() {
    $form = [];
    $form_state = new FormState();
    $form_state
      ->set('plugin_selector', $this->pluginSelector);
    $this->paymentMethodManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn([]);
    $method = new \ReflectionMethod($this->sut, 'actions');
    $method
      ->setAccessible(TRUE);
    $actions = $method
      ->invokeArgs($this->sut, [
      $form,
      $form_state,
    ]);
    $this
      ->assertTrue($actions['submit']['#disabled']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentFormTest::$configFactory protected property The config factory.
PaymentFormTest::$configFactoryConfiguration protected property The configuration the config factory returns.
PaymentFormTest::$currentUser protected property The current user.
PaymentFormTest::$entityRepository protected property The entity repository.
PaymentFormTest::$entityTypeBundleInfo protected property The entity type bundle service.
PaymentFormTest::$formDisplay protected property The form display.
PaymentFormTest::$payment protected property A payment entity.
PaymentFormTest::$paymentMethodManager protected property The payment method manager.
PaymentFormTest::$paymentMethodType protected property The payment method plugin type.
PaymentFormTest::$pluginSelector protected property The plugin selector.
PaymentFormTest::$pluginSelectorManager protected property The plugin selector manager.
PaymentFormTest::$stringTranslation protected property The string translation.
PaymentFormTest::$sut protected property The class under test.
PaymentFormTest::$time protected property The time service.
PaymentFormTest::setUp protected function Overrides UnitTestCase::setUp
PaymentFormTest::testActionsWithAvailablePlugins public function @covers ::actions @covers ::getPaymentMethodManager
PaymentFormTest::testActionsWithoutAvailablePlugins public function @covers ::actions @covers ::getPaymentMethodManager
PaymentFormTest::testForm public function @covers ::form @covers ::getPluginSelector @covers ::getPaymentMethodManager
PaymentFormTest::testSubmitForm public function @covers ::submitForm @covers ::getPluginSelector @covers ::getPaymentMethodManager
PaymentFormTest::testValidateForm public function @covers ::validateForm @covers ::getPluginSelector @covers ::getPaymentMethodManager
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.