You are here

public function PaymentReferenceTest::testFormElement in Payment 8.2

@covers ::formElement

File

modules/payment_reference/tests/src/Unit/Plugin/Field/FieldWidget/PaymentReferenceTest.php, line 119

Class

PaymentReferenceTest
@coversDefaultClass \Drupal\payment_reference\Plugin\Field\FieldWidget\PaymentReference

Namespace

Drupal\Tests\payment_reference\Unit\Plugin\Field\FieldWidget

Code

public function testFormElement() {
  $entity_type_id = $this
    ->randomMachineName();
  $bundle = $this
    ->randomMachineName();
  $field_name = $this
    ->randomMachineName();
  $user_id = mt_rand();
  $required = TRUE;
  $entity = $this
    ->createMock(EntityInterface::class);
  $entity
    ->expects($this
    ->atLeastOnce())
    ->method('bundle')
    ->willReturn($bundle);
  $entity
    ->expects($this
    ->atLeastOnce())
    ->method('getEntityTypeId')
    ->willReturn($entity_type_id);
  $this->fieldDefinition
    ->expects($this
    ->once())
    ->method('getName')
    ->willReturn($field_name);
  $this->fieldDefinition
    ->expects($this
    ->once())
    ->method('isRequired')
    ->willReturn($required);
  $payment = $this
    ->createMock(PaymentInterface::class);
  $this->paymentFactory
    ->expects($this
    ->once())
    ->method('createPayment')
    ->with($this->fieldDefinition)
    ->willReturn($payment);
  $this->currentUser
    ->expects($this
    ->exactly(1))
    ->method('id')
    ->willReturn($user_id);
  $items = $this
    ->getMockBuilder(FieldItemList::class)
    ->disableOriginalConstructor()
    ->getMock();
  $items
    ->expects($this
    ->atLeastOnce())
    ->method('getEntity')
    ->willReturn($entity);
  $form = [];
  $form_state = $this
    ->createMock(FormStateInterface::class);
  $build = $this->sut
    ->formElement($items, 3, [], $form, $form_state);
  $expected_build = array(
    'target_id' => array(
      '#default_value' => NULL,
      '#limit_allowed_plugin_ids' => $this->configFactoryConfiguration['payment_reference.payment_type']['allowed_plugin_ids'],
      '#plugin_selector_id' => $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'],
      '#prototype_payment' => $payment,
      '#queue_category_id' => $entity_type_id . '.' . $bundle . '.' . $field_name,
      '#queue_owner_id' => (int) $user_id,
      '#required' => $required,
      '#type' => 'payment_reference',
    ),
  );
  $this
    ->assertSame($expected_build, $build);
}