You are here

class PaymentCaptureFormTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Entity\Payment\PaymentCaptureForm

@group Payment

Hierarchy

Expanded class hierarchy of PaymentCaptureFormTest

File

tests/src/Unit/Entity/Payment/PaymentCaptureFormTest.php, line 24

Namespace

Drupal\Tests\payment\Unit\Entity\Payment
View source
class PaymentCaptureFormTest extends UnitTestCase {

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

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

  /**
   * The string translator.
   *
   * @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\Entity\Payment\PaymentCaptureForm
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->entityRepository = $this
      ->createMock(EntityRepositoryInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->payment = $this
      ->createMock(PaymentInterface::class);
    $this->entityTypeBundleInfo = $this
      ->prophesize(EntityTypeBundleInfoInterface::class)
      ->reveal();
    $this->time = $this
      ->prophesize(TimeInterface::class)
      ->reveal();
    $this->sut = new PaymentCaptureForm($this->entityRepository, $this->entityTypeBundleInfo, $this->time);
    $this->sut
      ->setStringTranslation($this->stringTranslation);
    $this->sut
      ->setEntity($this->payment);
  }

  /**
   * @covers ::getConfirmText
   */
  function testGetConfirmText() {
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $this->sut
      ->getConfirmText());
  }

  /**
   * @covers ::getQuestion
   */
  function testGetQuestion() {
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $this->sut
      ->getQuestion());
  }

  /**
   * @covers ::getCancelUrl
   */
  function testGetCancelUrl() {
    $url = new Url($this
      ->randomMachineName());
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('toUrl')
      ->with('canonical')
      ->willReturn($url);
    $this
      ->assertSame($url, $this->sut
      ->getCancelUrl());
  }

  /**
   * @covers ::submitForm
   */
  function testSubmitFormWithCompletionResponse() {
    $response = $this
      ->getMockBuilder(Response::class)
      ->disableOriginalConstructor()
      ->getMock();
    $completion_response = $this
      ->createMock(ResponseInterface::class);
    $completion_response
      ->expects($this
      ->atLeastOnce())
      ->method('getResponse')
      ->willReturn($response);
    $operation_result = $this
      ->createMock(OperationResultInterface::class);
    $operation_result
      ->expects($this
      ->atLeastOnce())
      ->method('getCompletionResponse')
      ->willReturn($completion_response);
    $operation_result
      ->expects($this
      ->atLeastOnce())
      ->method('isCompleted')
      ->willReturn(FALSE);
    $payment_method = $this
      ->createMock(PaymentMethodCapturePaymentInterface::class);
    $payment_method
      ->expects($this
      ->once())
      ->method('capturePayment')
      ->willReturn($operation_result);
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('getPaymentMethod')
      ->willReturn($payment_method);
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('setResponse')
      ->with($response);
    $this->sut
      ->submitForm($form, $form_state);
  }

  /**
   * @covers ::submitForm
   */
  function testSubmitFormWithoutCompletionResponse() {
    $operation_result = $this
      ->createMock(OperationResultInterface::class);
    $operation_result
      ->expects($this
      ->atLeastOnce())
      ->method('isCompleted')
      ->willReturn(TRUE);
    $payment_method = $this
      ->createMock(PaymentMethodCapturePaymentInterface::class);
    $payment_method
      ->expects($this
      ->once())
      ->method('capturePayment')
      ->willReturn($operation_result);
    $url = new Url($this
      ->randomMachineName());
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('getPaymentMethod')
      ->willReturn($payment_method);
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('toUrl')
      ->with('canonical')
      ->willReturn($url);
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('setRedirectUrl')
      ->with($url);
    $this->sut
      ->submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentCaptureFormTest::$entityRepository protected property The entity repository.
PaymentCaptureFormTest::$entityTypeBundleInfo protected property The entity type bundle service.
PaymentCaptureFormTest::$payment protected property The payment.
PaymentCaptureFormTest::$stringTranslation protected property The string translator.
PaymentCaptureFormTest::$sut protected property The class under test.
PaymentCaptureFormTest::$time protected property The time service.
PaymentCaptureFormTest::setUp public function Overrides UnitTestCase::setUp
PaymentCaptureFormTest::testGetCancelUrl function @covers ::getCancelUrl
PaymentCaptureFormTest::testGetConfirmText function @covers ::getConfirmText
PaymentCaptureFormTest::testGetQuestion function @covers ::getQuestion
PaymentCaptureFormTest::testSubmitFormWithCompletionResponse function @covers ::submitForm
PaymentCaptureFormTest::testSubmitFormWithoutCompletionResponse function @covers ::submitForm
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.