You are here

public function PaymentReferenceBaseTest::testAjaxPay in Payment 8.2

@covers ::ajaxPay

@dataProvider providerTestAjaxPay

File

tests/src/Unit/Element/PaymentReferenceBaseTest.php, line 413

Class

PaymentReferenceBaseTest
@coversDefaultClass \Drupal\payment\Element\PaymentReferenceBase

Namespace

Drupal\Tests\payment\Unit\Element

Code

public function testAjaxPay($is_completed, $number_of_commands) {
  $payment = $this
    ->createMock(PaymentInterface::class);
  $result = $this
    ->createMock(OperationResultInterface::class);
  $result
    ->expects($this
    ->atLeastOnce())
    ->method('isCompleted')
    ->willReturn($is_completed);
  $payment_method = $this
    ->createMock(PaymentMethodInterface::class);
  $payment_method
    ->expects($this
    ->any())
    ->method('getPayment')
    ->willReturn($payment);
  $payment_method
    ->expects($this
    ->atLeastOnce())
    ->method('getPaymentExecutionResult')
    ->willReturn($result);
  $payment_method
    ->expects($this
    ->any())
    ->method('getPluginDefinition')
    ->willReturn([
    'label' => 'Example',
  ]);
  $plugin_selector_plugin_id = $this
    ->randomMachineName();
  $plugin_selector = $this
    ->createMock(PluginSelectorInterface::class);
  $plugin_selector
    ->expects($this
    ->atLeastOnce())
    ->method('getSelectedPlugin')
    ->willReturn($payment_method);
  $form = array(
    'foo' => array(
      'bar' => array(
        '#limit_allowed_plugin_ids' => [],
        '#name' => $this
          ->randomMachineName(),
        '#plugin_selector_id' => $plugin_selector_plugin_id,
        '#prototype_payment' => $payment,
        '#required' => TRUE,
        'container' => array(
          '#id' => $this
            ->randomMachineName(),
          'payment_form' => array(
            'pay' => array(
              '#array_parents' => array(
                'foo',
                'bar',
                'container',
                'payment_form',
                'pay',
              ),
            ),
          ),
        ),
      ),
    ),
  );
  $form_state = new FormState();
  $form_state
    ->set('payment_reference.element.payment_reference.plugin_selector.' . $form['foo']['bar']['#name'], $plugin_selector);
  $form_state
    ->setTriggeringElement($form['foo']['bar']['container']['payment_form']['pay']);
  $response = $this->sut
    ->ajaxPay($form, $form_state);
  $this
    ->assertInstanceOf(AjaxResponse::class, $response);
  $this
    ->assertCount($number_of_commands, $response
    ->getCommands());
}