You are here

class PaymentStatusFormTest in Payment 8.2

Same name in this branch
  1. 8.2 tests/src/Unit/Entity/PaymentStatus/PaymentStatusFormTest.php \Drupal\Tests\payment\Unit\Entity\PaymentStatus\PaymentStatusFormTest
  2. 8.2 tests/src/Unit/Entity/Payment/PaymentStatusFormTest.php \Drupal\Tests\payment\Unit\Entity\Payment\PaymentStatusFormTest

@coversDefaultClass \Drupal\payment\Entity\Payment\PaymentStatusForm

@group Payment

Hierarchy

Expanded class hierarchy of PaymentStatusFormTest

File

tests/src/Unit/Entity/Payment/PaymentStatusFormTest.php, line 35

Namespace

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

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $currentUser;

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

  /**
   * The payment status plugin manager.
   *
   * @var \Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentStatusManager;

  /**
   * 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 payment status plugin type.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $paymentStatusPluginType;

  /**
   * 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 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;

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

  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $urlGenerator;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->currentUser = $this
      ->createMock(AccountInterface::class);
    $this->paymentStatusManager = $this
      ->createMock(PaymentStatusManagerInterface::class);
    $this->pluginSelector = $this
      ->createMock(PluginSelectorInterface::class);
    $this->pluginSelectorManager = $this
      ->createMock(PluginSelectorManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->paymentStatusPluginType = $this
      ->prophesize(PluginTypeInterface::class);
    $this->urlGenerator = $this
      ->createMock(UrlGeneratorInterface::class);
    $this->payment = $this
      ->createMock(PaymentInterface::class);
    $this->entityRepository = $this
      ->createMock(EntityRepositoryInterface::class);
    $this->entityTypeBundleInfo = $this
      ->prophesize(EntityTypeBundleInfoInterface::class)
      ->reveal();
    $this->time = $this
      ->prophesize(TimeInterface::class)
      ->reveal();
    $this->formDisplay = $this
      ->prophesize(EntityFormDisplayInterface::class)
      ->reveal();
    $this->sut = new PaymentStatusForm($this->entityRepository, $this->entityTypeBundleInfo, $this->time);
    $this->sut
      ->setStringTranslation($this->stringTranslation);
    $this->sut
      ->setEntity($this->payment);
    $this->sut
      ->setPaymentStatusPluginType($this->paymentStatusPluginType
      ->reveal());
    $this->sut
      ->setPluginSelectorManager($this->pluginSelectorManager);
    $this->sut
      ->setCurrentUser($this->currentUser);
  }

  /**
   * @covers ::form
   * @covers ::getPluginSelector
   */
  public function testForm() {
    $form = [];
    $form_state = new FormState();
    $settable_payment_status_ids = array(
      $this
        ->randomMachineName(),
    );
    $language = $this
      ->createMock(LanguageInterface::class);
    $payment_method = $this
      ->createMock(PaymentStatusFormUnitTestDummyPaymentMethodUpdateStatusInterface::class);
    $payment_method
      ->expects($this
      ->once())
      ->method('getSettablePaymentStatuses')
      ->with($this->currentUser)
      ->willReturn($settable_payment_status_ids);
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('getPaymentMethod')
      ->willReturn($payment_method);
    $this->payment
      ->expects($this
      ->any())
      ->method('language')
      ->willReturn($language);
    $entity_type = $this
      ->createMock(EntityTypeInterface::class);
    $this->payment
      ->expects($this
      ->any())
      ->method('getEntityType')
      ->willReturn($entity_type);
    $this->pluginSelectorManager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->willReturn($this->pluginSelector);
    $plugin_selector_form = [
      'foo' => $this
        ->randomMachineName(),
    ];
    $form = [
      'langcode' => [],
    ];
    $form_state = new FormState();
    $this->sut
      ->setFormDisplay($this->formDisplay, $form_state);
    $this->pluginSelector
      ->expects($this
      ->atLeastOnce())
      ->method('buildSelectorForm')
      ->with([], $form_state)
      ->willReturn($plugin_selector_form);
    $payment_status_manager = $this
      ->prophesize(PaymentStatusManagerInterface::class);
    $this->paymentStatusPluginType
      ->getPluginManager()
      ->willReturn($payment_status_manager
      ->reveal());
    $build = $this->sut
      ->form($form, $form_state);
    $this
      ->assertIsArray($build);
    $this
      ->assertArrayHasKey('payment_status', $build);
    $this
      ->assertSame($plugin_selector_form, $build['payment_status']);

    // Build the form again to make sure the plugin selector is only created
    // once.
    $this->sut
      ->form($form, $form_state);
  }

  /**
   * @covers ::validateForm
   * @covers ::getPluginSelector
   */
  public function testValidateForm() {
    $form = [
      'payment_status' => [
        'foo' => $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')
      ->willReturn($this->pluginSelector);
    $this->pluginSelector
      ->expects($this
      ->atLeastOnce())
      ->method('validateSelectorForm')
      ->with($form['payment_status'], $form_state);
    $payment_status_manager = $this
      ->prophesize(PaymentStatusManagerInterface::class);
    $this->paymentStatusPluginType
      ->getPluginManager()
      ->willReturn($payment_status_manager
      ->reveal());
    $this->sut
      ->validateForm($form, $form_state);
  }

  /**
   * @covers ::submitForm
   * @covers ::getPluginSelector
   */
  public function testSubmitForm() {
    $form = [
      'payment_status' => [
        'foo' => $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_status = $this
      ->createMock(PaymentStatusInterface::class);
    $this->pluginSelectorManager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->willReturn($this->pluginSelector);
    $this->pluginSelector
      ->expects($this
      ->atLeastOnce())
      ->method('getSelectedPlugin')
      ->willReturn($payment_status);
    $this->pluginSelector
      ->expects($this
      ->atLeastOnce())
      ->method('submitSelectorForm')
      ->with($form['payment_status'], $form_state);
    $url = new Url($this
      ->randomMachineName());
    $this->payment
      ->expects($this
      ->once())
      ->method('setPaymentStatus')
      ->with($payment_status);
    $this->payment
      ->expects($this
      ->once())
      ->method('save');
    $this->payment
      ->expects($this
      ->once())
      ->method('toUrl')
      ->with('canonical')
      ->willReturn($url);
    $payment_status_manager = $this
      ->prophesize(PaymentStatusManagerInterface::class);
    $this->paymentStatusPluginType
      ->getPluginManager()
      ->willReturn($payment_status_manager
      ->reveal());
    $this->sut
      ->submitForm($form, $form_state);
    $this
      ->assertSame($url, $form_state
      ->getRedirect());
  }

  /**
   * @covers ::actions
   */
  public function testActions() {
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $method = new \ReflectionMethod($this->sut, 'actions');
    $method
      ->setAccessible(TRUE);
    $actions = $method
      ->invokeArgs($this->sut, array(
      $form,
      $form_state,
    ));
    $this
      ->assertIsArray($actions);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentStatusFormTest::$currentUser protected property The current user.
PaymentStatusFormTest::$entityRepository protected property The entity repository.
PaymentStatusFormTest::$entityTypeBundleInfo protected property The entity type bundle service.
PaymentStatusFormTest::$formDisplay protected property The form display.
PaymentStatusFormTest::$payment protected property The payment.
PaymentStatusFormTest::$paymentStatusManager protected property The payment status plugin manager.
PaymentStatusFormTest::$paymentStatusPluginType protected property The payment status plugin type.
PaymentStatusFormTest::$pluginSelector protected property The plugin selector.
PaymentStatusFormTest::$pluginSelectorManager protected property The plugin selector manager.
PaymentStatusFormTest::$stringTranslation protected property The string translator.
PaymentStatusFormTest::$sut protected property The class under test.
PaymentStatusFormTest::$time protected property The time service.
PaymentStatusFormTest::$urlGenerator protected property The URL generator.
PaymentStatusFormTest::setUp public function Overrides UnitTestCase::setUp
PaymentStatusFormTest::testActions public function @covers ::actions
PaymentStatusFormTest::testForm public function @covers ::form @covers ::getPluginSelector
PaymentStatusFormTest::testSubmitForm public function @covers ::submitForm @covers ::getPluginSelector
PaymentStatusFormTest::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.