You are here

abstract class AbstractFieldSettingsSummaryAlterEventTestCase in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/field_event_dispatcher/tests/src/Unit/Field/AbstractFieldSettingsSummaryAlterEventTestCase.php \Drupal\Tests\field_event_dispatcher\Unit\Field\AbstractFieldSettingsSummaryAlterEventTestCase

Class AbstractFieldSettingsSummaryAlterEventTestCase.

@group field_event_dispatcher

Hierarchy

Expanded class hierarchy of AbstractFieldSettingsSummaryAlterEventTestCase

File

modules/field_event_dispatcher/tests/src/Unit/Field/AbstractFieldSettingsSummaryAlterEventTestCase.php, line 17

Namespace

Drupal\Tests\field_event_dispatcher\Unit\Field
View source
abstract class AbstractFieldSettingsSummaryAlterEventTestCase extends TestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * Sets up the test.
   */
  public function setUp() : void {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    Drupal::setContainer($builder);
  }

  /**
   * Get the name of the procedural hook function for this test.
   *
   * @return string
   *   The name of the procedural hook function for this test.
   */
  protected abstract function getProceduralHookName() : string;

  /**
   * Get the name of the event to test.
   *
   * @return string
   *   The name of the event to test.
   */
  protected abstract function getEventName() : string;

  /**
   * Get the test context array.
   *
   * @param \Drupal\Core\Field\BaseFieldDefinition $fieldDefinition
   *   The field defition used in the test.
   *
   * @return array
   *   The test context array.
   */
  protected abstract function getTestContext(BaseFieldDefinition $fieldDefinition) : array;

  /**
   * AbstractFieldSettingsSummaryAlterEventTestCase adding summary test.
   *
   * This tests adding an additional summary.
   */
  public function testAddSummary() : void {
    $summary = $expectedSummary = [];
    $eventName = $this
      ->getEventName();
    $this->manager
      ->setEventCallbacks([
      $eventName => static function (AbstractFieldSettingsSummaryFormEvent $event) {
        $event
          ->getSummary()[] = 'Test';
      },
    ]);

    // Run the procedural hook which should trigger the above handler.
    $hook = $this
      ->getProceduralHookName();
    $hook($summary, []);

    /** @var \Drupal\field_event_dispatcher\Event\Field\AbstractFieldSettingsSummaryFormEvent $event */
    $event = $this->manager
      ->getRegisteredEvent($eventName);
    self::assertSame($summary, $event
      ->getSummary());
    $expectedSummary[] = 'Test';
    self::assertSame($expectedSummary, $summary);
  }

  /**
   * AbstractFieldSettingsSummaryAlterEventTestCase context test.
   *
   * This tests that the context parameter returns expected values.
   */
  public function testContext() : void {
    $fieldDefinition = new BaseFieldDefinition();
    $context = $this
      ->getTestContext($fieldDefinition);
    $context['field_definition'] = $fieldDefinition;
    $expectedContext = $context;
    $summary = [];

    // Run the procedural hook which should trigger the event.
    $hook = $this
      ->getProceduralHookName();
    $hook($summary, $context);

    /** @var \Drupal\field_event_dispatcher\Event\Field\AbstractFieldSettingsSummaryFormEvent $event */
    $event = $this->manager
      ->getRegisteredEvent($this
      ->getEventName());
    self::assertSame($expectedContext, $event
      ->getContext());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractFieldSettingsSummaryAlterEventTestCase::$manager private property The manager.
AbstractFieldSettingsSummaryAlterEventTestCase::getEventName abstract protected function Get the name of the event to test. 2
AbstractFieldSettingsSummaryAlterEventTestCase::getProceduralHookName abstract protected function Get the name of the procedural hook function for this test. 2
AbstractFieldSettingsSummaryAlterEventTestCase::getTestContext abstract protected function Get the test context array. 2
AbstractFieldSettingsSummaryAlterEventTestCase::setUp public function Sets up the test.
AbstractFieldSettingsSummaryAlterEventTestCase::testAddSummary public function AbstractFieldSettingsSummaryAlterEventTestCase adding summary test.
AbstractFieldSettingsSummaryAlterEventTestCase::testContext public function AbstractFieldSettingsSummaryAlterEventTestCase context test.