You are here

public function PaymentReferenceBaseTest::testPay in Payment 8.2

@covers ::pay

@dataProvider providerTestPay

File

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

Class

PaymentReferenceBaseTest
@coversDefaultClass \Drupal\payment\Element\PaymentReferenceBase

Namespace

Drupal\Tests\payment\Unit\Element

Code

public function testPay($has_completed, $is_xml_http_request) {
  $configuration = [];
  $plugin_id = $this
    ->randomMachineName();
  $this->pluginDefinition = [];
  $this->sut = $this
    ->getMockBuilder(PaymentReferenceBase::class)
    ->setConstructorArgs(array(
    $configuration,
    $plugin_id,
    $this->pluginDefinition,
    $this->requestStack,
    $this->paymentStorage,
    $this->stringTranslation,
    $this->dateFormatter,
    $this->linkGenerator,
    $this->renderer,
    $this->currentUser,
    $this->pluginSelectorManager,
    $this->paymentMethodType,
    new Random(),
  ))
    ->setMethods(array(
    'getEntityFormDisplay',
    'getPluginSelector',
  ))
    ->getMockForAbstractClass();
  $this->sut
    ->setMessenger($this->messenger);
  $payment_method = $this
    ->createMock(PaymentMethodInterface::class);
  $url = new Url($this
    ->randomMachineName());
  $result = $this
    ->createMock(OperationResultInterface::class);
  $result
    ->expects($this
    ->atLeastOnce())
    ->method('isCompleted')
    ->willReturn($has_completed);
  $payment = $this
    ->createMock(PaymentInterface::class);
  $payment
    ->expects($this
    ->atLeastOnce())
    ->method('createDuplicate')
    ->willReturnSelf();
  $payment
    ->expects($this
    ->atLeastOnce())
    ->method('execute')
    ->willReturn($result);
  $payment
    ->expects($this
    ->atLeastOnce())
    ->method('setPaymentMethod')
    ->with($payment_method);
  $payment
    ->expects(!$has_completed && !$is_xml_http_request ? $this
    ->atLeastOnce() : $this
    ->never())
    ->method('toUrl')
    ->with('complete')
    ->willReturn($url);
  $form = array(
    'foo' => array(
      'bar' => array(
        '#prototype_payment' => $payment,
        'container' => array(
          '#id' => $this
            ->randomMachineName(),
          'payment_form' => array(
            'pay' => array(
              '#array_parents' => array(
                'foo',
                'bar',
                'container',
                'payment_form',
                'pay',
              ),
            ),
            'payment_method' => array(
              '#foo' => $this
                ->randomMachineName(),
            ),
          ),
        ),
      ),
    ),
  );
  $form_state = new FormState();
  $form_state
    ->setTriggeringElement($form['foo']['bar']['container']['payment_form']['pay']);
  $request = $this
    ->getMockBuilder(Request::class)
    ->disableOriginalConstructor()
    ->getMock();
  $request
    ->expects($has_completed ? $this
    ->never() : $this
    ->atLeastOnce())
    ->method('isXmlHttpRequest')
    ->willReturn($is_xml_http_request);
  $this->requestStack
    ->expects($has_completed ? $this
    ->never() : $this
    ->atLeastOnce())
    ->method('getCurrentRequest')
    ->willReturn($request);
  $plugin_selector = $this
    ->createMock(PluginSelectorInterface::class);
  $plugin_selector
    ->expects($this
    ->atLeastOnce())
    ->method('getSelectedPlugin')
    ->willReturn($payment_method);
  $plugin_selector
    ->expects($this
    ->once())
    ->method('submitSelectorForm')
    ->with($form['foo']['bar']['container']['payment_form']['payment_method'], $form_state);
  $entity_form_display = $this
    ->createMock(EntityFormDisplayInterface::class);
  $entity_form_display
    ->expects($this
    ->once())
    ->method('extractFormValues')
    ->with($payment, $form['foo']['bar']['container']['payment_form'], $form_state);
  $this->sut
    ->expects($this
    ->atLeastOnce())
    ->method('getEntityFormDisplay')
    ->willReturn($entity_form_display);
  $this->sut
    ->expects($this
    ->atLeastOnce())
    ->method('getPluginSelector')
    ->willReturn($plugin_selector);
  $this->sut
    ->pay($form, $form_state);
  $this
    ->assertTrue($form_state
    ->isRebuilding());
}