You are here

public function FormEventTest::testWidgetFormAlterEvent in Hook Event Dispatcher 8

Test WidgetFormAlterEvent.

File

tests/src/Unit/Form/FormEventTest.php, line 109

Class

FormEventTest
Class FormEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Form

Code

public function testWidgetFormAlterEvent() {
  $element = [
    'widget' => 'element',
  ];
  $formState = $this
    ->createMock(FormStateInterface::class);
  $items = $this
    ->createMock(FieldItemListInterface::class);
  $definition = $this
    ->createMock(FieldDefinitionInterface::class);
  $widgetType = 'widget_type';
  $definition
    ->method('getType')
    ->willReturn($widgetType);
  $items
    ->method('getFieldDefinition')
    ->willReturn($definition);
  $context = [
    'items' => $items,
  ];
  $this->manager
    ->setMaxEventCount(2);
  hook_event_dispatcher_field_widget_form_alter($element, $formState, $context);

  /** @var \Drupal\hook_event_dispatcher\Event\Form\WidgetFormAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::WIDGET_FORM_ALTER);
  self::assertEquals($element, $event
    ->getElement());
  $this
    ->assertEquals($formState, $event
    ->getFormState());
  $this
    ->assertEquals($context, $event
    ->getContext());
  $newElement = [
    'NewElement',
  ];
  $event
    ->setElement($newElement);
  $this
    ->assertEquals($newElement, $event
    ->getElement());
}