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\PaymentStatus\PaymentStatusForm

@group Payment

Hierarchy

Expanded class hierarchy of PaymentStatusFormTest

File

tests/src/Unit/Entity/PaymentStatus/PaymentStatusFormTest.php, line 26

Namespace

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

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

  /**
   * The payment method configuration manager.
   *
   * @var \Drupal\payment\Plugin\Payment\MethodConfiguration\PaymentMethodConfigurationManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentStatusManager;

  /**
   * The payment status storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentStatusStorage;

  /**
   * 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 messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $messenger;

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

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->paymentStatusManager = $this
      ->createMock(PaymentStatusManagerInterface::class);
    $this->paymentStatusStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->paymentStatus = $this
      ->createMock(PaymentStatusInterface::class);
    $this->pluginSelectorManager = $this
      ->createMock(PluginSelectorManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->paymentStatusPluginType = $this
      ->prophesize(PluginTypeInterface::class);
    $this->messenger = $this
      ->createMock(MessengerInterface::class);
    $this->sut = new PaymentStatusForm($this->stringTranslation, $this->paymentStatusStorage, $this->pluginSelectorManager, $this->paymentStatusPluginType
      ->reveal());
    $this->sut
      ->setEntity($this->paymentStatus);
    $this->sut
      ->setMessenger($this->messenger);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $entity_type_manager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('payment_status')
      ->willReturn($this->paymentStatusStorage);
    $plugin_type_manager = $this
      ->prophesize(PluginTypeManagerInterface::class);
    $plugin_type_manager
      ->getPluginType('payment_status')
      ->willReturn($this->paymentStatusPluginType);
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'entity_type.manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $entity_type_manager,
      ),
      array(
        'plugin.manager.plugin.plugin_selector',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->pluginSelectorManager,
      ),
      array(
        'plugin.plugin_type_manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $plugin_type_manager
          ->reveal(),
      ),
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $form = PaymentStatusForm::create($container);
    $this
      ->assertInstanceOf(PaymentStatusForm::class, $form);
  }

  /**
   * @covers ::form
   */
  public function testForm() {
    $label = $this
      ->randomMachineName();
    $id = $this
      ->randomMachineName();
    $is_new = FALSE;
    $parent_id = $this
      ->randomMachineName();
    $description = $this
      ->randomMachineName();
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $language = $this
      ->createMock(LanguageInterface::class);
    $parent_selector_form = [
      '#foo' => $this
        ->randomMachineName(),
    ];
    $parent_selector = $this
      ->createMock(PluginSelectorInterface::class);
    $parent_selector
      ->expects($this
      ->atLeastOnce())
      ->method('buildSelectorForm')
      ->with([], $form_state)
      ->willReturn($parent_selector_form);
    $this->pluginSelectorManager
      ->expects($this
      ->atLeastOnce())
      ->method('createInstance')
      ->willReturn($parent_selector);
    $this->paymentStatus
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn($id);
    $this->paymentStatus
      ->expects($this
      ->any())
      ->method('getDescription')
      ->willReturn($description);
    $this->paymentStatus
      ->expects($this
      ->any())
      ->method('getParentId')
      ->willReturn($parent_id);
    $this->paymentStatus
      ->expects($this
      ->any())
      ->method('isNew')
      ->willReturn($is_new);
    $this->paymentStatus
      ->expects($this
      ->any())
      ->method('label')
      ->willReturn($label);
    $this->paymentStatus
      ->expects($this
      ->any())
      ->method('language')
      ->willReturn($language);
    $build = $this->sut
      ->form([], $form_state);
    unset($build['#process']);
    unset($build['langcode']);
    $expected_build['label'] = [
      '#type' => 'textfield',
      '#default_value' => $label,
      '#maxlength' => 255,
      '#required' => TRUE,
    ];
    unset($build['label']['#title']);
    $expected_build['id'] = [
      '#default_value' => $id,
      '#disabled' => !$is_new,
      '#machine_name' => array(
        'source' => array(
          'label',
        ),
        'exists' => array(
          $this->sut,
          'PaymentStatusIdExists',
        ),
      ),
      '#maxlength' => 255,
      '#type' => 'machine_name',
      '#required' => TRUE,
    ];
    unset($build['id']['#title']);
    $expected_build['parent_id'] = $parent_selector_form;
    $expected_build['description'] = [
      '#type' => 'textarea',
      '#default_value' => $description,
      '#maxlength' => 255,
    ];
    unset($build['description']['#title']);
    $expected_build['#after_build'] = [
      '::afterBuild',
    ];
    $this
      ->assertSame($expected_build, $build);
  }

  /**
   * @covers ::copyFormValuesToEntity
   */
  public function testCopyFormValuesToEntity() {
    $description = $this
      ->randomMachineName();
    $id = $this
      ->randomMachineName();
    $label = $this
      ->randomMachineName();
    $parent_id = $this
      ->randomMachineName();
    $this->paymentStatus
      ->expects($this
      ->once())
      ->method('setDescription')
      ->with($description);
    $this->paymentStatus
      ->expects($this
      ->once())
      ->method('setId')
      ->with($id);
    $this->paymentStatus
      ->expects($this
      ->once())
      ->method('setLabel')
      ->with($label);
    $this->paymentStatus
      ->expects($this
      ->once())
      ->method('setParentId')
      ->with($parent_id);
    $parent_status = $this
      ->createMock(PluginSelectorInterface::class);
    $parent_status
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginId')
      ->willReturn($parent_id);
    $parent_selector = $this
      ->createMock(PluginSelectorInterface::class);
    $parent_selector
      ->expects($this
      ->atLeastOnce())
      ->method('getSelectedPlugin')
      ->willReturn($parent_status);
    $this->pluginSelectorManager
      ->expects($this
      ->atLeastOnce())
      ->method('createInstance')
      ->willReturn($parent_selector);
    $form = [];
    $form_state = new FormState();
    $form_state
      ->setValue('description', $description);
    $form_state
      ->setValue('id', $id);
    $form_state
      ->setValue('label', $label);
    $form_state
      ->setValue('parent_id', $parent_id);
    $method = new \ReflectionMethod($this->sut, 'copyFormValuesToEntity');
    $method
      ->setAccessible(TRUE);
    $method
      ->invokeArgs($this->sut, array(
      $this->paymentStatus,
      $form,
      $form_state,
    ));
  }

  /**
   * @covers ::paymentStatusIdExists
   */
  public function testPaymentStatusIdExists() {
    $method = new \ReflectionMethod($this->sut, 'paymentStatusIdExists');
    $method
      ->setAccessible(TRUE);
    $payment_method_configuration_id = $this
      ->randomMachineName();
    $this->paymentStatusStorage
      ->expects($this
      ->at(0))
      ->method('load')
      ->with($payment_method_configuration_id)
      ->willReturn($this->paymentStatus);
    $this->paymentStatusStorage
      ->expects($this
      ->at(1))
      ->method('load')
      ->with($payment_method_configuration_id)
      ->willReturn(NULL);
    $this
      ->assertTrue($method
      ->invoke($this->sut, $payment_method_configuration_id));
    $this
      ->assertFalse($method
      ->invoke($this->sut, $payment_method_configuration_id));
  }

  /**
   * @covers ::save
   */
  public function testSave() {
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->once())
      ->method('setRedirect')
      ->with('entity.payment_status.collection');

    /** @var \Drupal\payment\Entity\PaymentStatus\PaymentStatusForm|\PHPUnit\Framework\MockObject\MockObject $form */
    $form = $this
      ->getMockBuilder(PaymentStatusForm::class)
      ->setConstructorArgs(array(
      $this->stringTranslation,
      $this->paymentStatusStorage,
      $this->pluginSelectorManager,
      $this->paymentStatusPluginType
        ->reveal(),
    ))
      ->setMethods(array(
      'copyFormValuesToEntity',
    ))
      ->getMock();
    $form
      ->setEntity($this->paymentStatus);
    $form
      ->setMessenger($this->messenger);
    $this->paymentStatus
      ->expects($this
      ->once())
      ->method('save');
    $form
      ->save([], $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentStatusFormTest::$messenger protected property The messenger.
PaymentStatusFormTest::$paymentStatus protected property The payment status.
PaymentStatusFormTest::$paymentStatusManager protected property The payment method configuration manager.
PaymentStatusFormTest::$paymentStatusPluginType protected property The payment status plugin type.
PaymentStatusFormTest::$paymentStatusStorage protected property The payment status storage.
PaymentStatusFormTest::$pluginSelectorManager protected property The plugin selector manager.
PaymentStatusFormTest::$stringTranslation protected property The string translator.
PaymentStatusFormTest::$sut protected property The form under test.
PaymentStatusFormTest::setUp public function Overrides UnitTestCase::setUp
PaymentStatusFormTest::testCopyFormValuesToEntity public function @covers ::copyFormValuesToEntity
PaymentStatusFormTest::testCreate function @covers ::create @covers ::__construct
PaymentStatusFormTest::testForm public function @covers ::form
PaymentStatusFormTest::testPaymentStatusIdExists public function @covers ::paymentStatusIdExists
PaymentStatusFormTest::testSave public function @covers ::save
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.