You are here

public function PaymentLineItemsInputTest::testDeleteSubmit in Payment 8.2

@covers ::deleteSubmit

File

tests/src/Unit/Element/PaymentLineItemsInputTest.php, line 457

Class

PaymentLineItemsInputTest
@coversDefaultClass \Drupal\payment\Element\PaymentLineItemsInput

Namespace

Drupal\Tests\payment\Unit\Element

Code

public function testDeleteSubmit() {
  $line_item_name = $this
    ->randomMachineName();
  $root_element_name = $this
    ->randomMachineName();
  $line_item_a = $this
    ->createMock(PaymentLineItemInterface::class);
  $line_item_a
    ->expects($this
    ->once())
    ->method('getName')
    ->willReturn($this
    ->randomMachineName());
  $line_item_b = $this
    ->createMock(PaymentLineItemInterface::class);
  $line_item_b
    ->expects($this
    ->once())
    ->method('getName')
    ->willReturn($line_item_name);
  $line_item_c = $this
    ->createMock(PaymentLineItemInterface::class);
  $line_item_c
    ->expects($this
    ->once())
    ->method('getName')
    ->willReturn($this
    ->randomMachineName());
  $form_build = array(
    'foo' => array(
      '#name' => $root_element_name,
      '#parents' => array(
        $this
          ->randomMachineName(),
        $this
          ->randomMachineName(),
      ),
      'line_items' => array(
        $line_item_name => array(
          'delete' => array(
            '#array_parents' => array(
              'foo',
              'line_items',
              $line_item_name,
              'delete',
            ),
            '#parents' => [],
          ),
        ),
      ),
    ),
  );
  $form_build['foo']['line_items'][$line_item_name]['delete']['#name'] = 'delete_' . implode('-', $form_build['foo']['#parents']);
  $form_state = new FormState();
  $form_state
    ->set('payment.element.payment_line_items_input.configured.' . $root_element_name, array(
    $line_item_a,
    $line_item_b,
    $line_item_c,
  ));
  $form_state
    ->setTriggeringElement($form_build['foo']['line_items'][$line_item_name]['delete']);
  $this->sut
    ->deleteSubmit($form_build, $form_state);
  $this
    ->assertTrue($form_state
    ->isRebuilding());
  $element = $this->sut;
  $line_items = $element::getLineItems($form_build['foo'], $form_state);
  $this
    ->assertTrue(in_array($line_item_a, $line_items, TRUE));
  $this
    ->assertFalse(in_array($line_item_b, $line_items, TRUE));
  $this
    ->assertTrue(in_array($line_item_c, $line_items, TRUE));
}