You are here

public function PaymentReferenceBaseTest::testValueCallbackWithoutDefaultValue in Payment 8.2

@covers ::valueCallback

File

tests/src/Unit/Element/PaymentReferenceBaseTest.php, line 254

Class

PaymentReferenceBaseTest
@coversDefaultClass \Drupal\payment\Element\PaymentReferenceBase

Namespace

Drupal\Tests\payment\Unit\Element

Code

public function testValueCallbackWithoutDefaultValue() {
  $queue_category_id = $this
    ->randomMachineName();
  $queue_owner_id = $this
    ->randomMachineName();
  $payment_id = mt_rand();
  $element = array(
    '#default_value' => NULL,
    '#queue_category_id' => $queue_category_id,
    '#queue_owner_id' => $queue_owner_id,
    '#type' => $this
      ->randomMachineName(),
  );
  $input = $this
    ->randomMachineName();
  $form_state = $this
    ->createMock(FormStateInterface::class);
  $this->sut = $this
    ->getMockBuilder(PaymentReferenceBase::class)
    ->disableOriginalConstructor()
    ->getMockForAbstractClass();
  $this->sut
    ->expects($this
    ->atLeastOnce())
    ->method('getPaymentQueue')
    ->willReturn($this->paymentQueue);

  // We cannot mock ElementInfoManagerInterface, because it does not extend
  // PluginManagerInterface.
  $element_info_manager = $this
    ->createMock(PluginManagerInterface::class);
  $element_info_manager
    ->expects($this
    ->once())
    ->method('createInstance')
    ->with($element['#type'])
    ->willReturn($this->sut);
  $container = $this
    ->createMock(ContainerInterface::class);
  $container
    ->expects($this
    ->once())
    ->method('get')
    ->with('plugin.manager.element_info')
    ->willReturn($element_info_manager);
  \Drupal::setContainer($container);
  $this->paymentQueue
    ->expects($this
    ->once())
    ->method('loadPaymentIds')
    ->with($queue_category_id, $queue_owner_id)
    ->willReturn(array(
    $payment_id,
  ));
  $element_sut = $this->sut;
  $this
    ->assertSame($payment_id, $element_sut::valueCallback($element, $input, $form_state));
}