You are here

class PaymentFormTest in Payment 8.2

Same name in this branch
  1. 8.2 modules/payment_form/tests/src/Unit/Entity/Payment/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Entity\Payment\PaymentFormTest
  2. 8.2 modules/payment_form/tests/src/Unit/Plugin/Payment/Type/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Plugin\Payment\Type\PaymentFormTest
  3. 8.2 modules/payment_form/tests/src/Unit/Plugin/Field/FieldFormatter/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Plugin\Field\FieldFormatter\PaymentFormTest
  4. 8.2 modules/payment_form/tests/src/Unit/Plugin/Field/FieldWidget/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Plugin\Field\FieldWidget\PaymentFormTest
  5. 8.2 modules/payment_form/tests/src/Unit/Plugin/Field/FieldType/PaymentFormTest.php \Drupal\Tests\payment_form\Unit\Plugin\Field\FieldType\PaymentFormTest

@coversDefaultClass \Drupal\payment_form\Plugin\Field\FieldWidget\PaymentForm

@group Payment Form Field

Hierarchy

Expanded class hierarchy of PaymentFormTest

File

modules/payment_form/tests/src/Unit/Plugin/Field/FieldWidget/PaymentFormTest.php, line 21

Namespace

Drupal\Tests\payment_form\Unit\Plugin\Field\FieldWidget
View source
class PaymentFormTest extends UnitTestCase {

  /**
   * The field definition.
   *
   * @var \Drupal\Core\Field\FieldDefinitionInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $fieldDefinition;

  /**
   * The payment line item manager.
   *
   * @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentLineItemManager;

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $stringTranslation;

  /**
   * The class under test.
   *
   * @var \Drupal\payment_form\Plugin\Field\FieldWidget\PaymentForm
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $plugin_id = $this
      ->randomMachineName();
    $plugin_definition = [];
    $this->fieldDefinition = $this
      ->createMock(FieldDefinitionInterface::class);
    $settings = [];
    $third_party_settings = [];
    $this->paymentLineItemManager = $this
      ->createMock(PaymentLineItemManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new PaymentForm($plugin_id, $plugin_definition, $this->fieldDefinition, $settings, $third_party_settings, $this->stringTranslation, $this->paymentLineItemManager);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'plugin.manager.payment.line_item',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->paymentLineItemManager,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $field_definition = $this
      ->createMock(FieldDefinitionInterface::class);
    $configuration = [
      'field_definition' => $field_definition,
      'settings' => [],
      'third_party_settings' => [],
    ];
    $plugin_definition = [];
    $plugin_id = $this
      ->randomMachineName();
    $sut = PaymentForm::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(PaymentForm::class, $sut);
  }

  /**
   * @covers ::settingsSummary
   */
  public function testSettingsSummaryWithOneLineItem() {
    $line_items_data = [
      [
        'plugin_id' => $this
          ->randomMachineName(),
        'plugin_configuration' => [],
      ],
    ];
    $this->sut
      ->setSetting('line_items', $line_items_data);
    $this->stringTranslation
      ->expects($this
      ->any())
      ->method('formatPlural')
      ->with(1);
    $this
      ->assertInstanceOf(PluralTranslatableMarkup::class, $this->sut
      ->settingsSummary()[0]);
  }

  /**
   * @covers ::settingsSummary
   */
  public function testSettingsSummaryWithMultipleLineItems() {
    $line_items_data = [
      [
        'plugin_id' => $this
          ->randomMachineName(),
        'plugin_configuration' => [],
      ],
      [
        'plugin_id' => $this
          ->randomMachineName(),
        'plugin_configuration' => [],
      ],
    ];
    $this->sut
      ->setSetting('line_items', $line_items_data);
    $this->stringTranslation
      ->expects($this
      ->any())
      ->method('formatPlural')
      ->with(2);
    $this
      ->assertInstanceOf(PluralTranslatableMarkup::class, $this->sut
      ->settingsSummary()[0]);
  }

  /**
   * @covers ::formElement
   */
  public function testFormElement() {
    $items = $this
      ->getMockBuilder(FieldItemList::class)
      ->disableOriginalConstructor()
      ->getMock();
    $delta = 0;
    $element = [];
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $this
      ->assertIsArray($this->sut
      ->formElement($items, $delta, $element, $form, $form_state));
  }

  /**
   * @covers ::formElementProcess
   */
  public function testFormElementProcess() {
    $field_storage_definition = $this
      ->createMock(FieldStorageDefinitionInterface::class);
    $this->fieldDefinition
      ->expects($this
      ->atLeastOnce())
      ->method('getFieldStorageDefinition')
      ->willReturn($field_storage_definition);
    $iterator = new \ArrayIterator([
      (object) [
        'plugin_configuration' => [],
        'plugin_id' => $this
          ->randomMachineName(),
      ],
    ]);
    $items = $this
      ->getMockBuilder(FieldItemList::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'getIterator',
    ])
      ->getMock();
    $items
      ->expects($this
      ->once())
      ->method('getIterator')
      ->willReturn($iterator);
    $element = [
      '#array_parents' => [
        'line_items',
      ],
      '#items' => $items,
    ];
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $element = $this->sut
      ->formElementProcess($element, $form_state, $form);
    $this
      ->assertIsArray($element);
    $this
      ->arrayHasKey('array_parents', $element);
    $this
      ->arrayHasKey('line_items', $element);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentFormTest::$fieldDefinition protected property The field definition.
PaymentFormTest::$paymentLineItemManager protected property The payment line item manager.
PaymentFormTest::$stringTranslation protected property The string translator.
PaymentFormTest::$sut protected property The class under test.
PaymentFormTest::setUp protected function Overrides UnitTestCase::setUp
PaymentFormTest::testCreate function @covers ::create @covers ::__construct
PaymentFormTest::testFormElement public function @covers ::formElement
PaymentFormTest::testFormElementProcess public function @covers ::formElementProcess
PaymentFormTest::testSettingsSummaryWithMultipleLineItems public function @covers ::settingsSummary
PaymentFormTest::testSettingsSummaryWithOneLineItem public function @covers ::settingsSummary
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.