function TemplateProjectTestBase::assertExpectedStageEventsFired

Same name and namespace in other branches
  1. main core/modules/package_manager/tests/src/Build/TemplateProjectTestBase.php \Drupal\Tests\package_manager\Build\TemplateProjectTestBase::assertExpectedStageEventsFired()

Asserts stage events were fired in a specific order.

Parameters

string $expected_stage_class: The expected stage class for the events.

array|null $expected_events: (optional) The expected stage events that should have been fired in the order in which they should have been fired. Events can be specified more that once if they will be fired multiple times. If there are no events specified all life cycle events from PreCreateEvent to PostApplyEvent will be asserted.

int $wait: (optional) How many seconds to wait for the events to be fired. Defaults to 0.

string $message: (optional) A message to display with the assertion.

See also

\Drupal\package_manager_test_event_logger\EventSubscriber\EventLogSubscriber::logEventInfo

1 call to TemplateProjectTestBase::assertExpectedStageEventsFired()
PackageUpdateTest::testPackageUpdate in core/modules/package_manager/tests/src/Build/PackageUpdateTest.php
Tests updating packages in a stage directory.

File

core/modules/package_manager/tests/src/Build/TemplateProjectTestBase.php, line 608

Class

TemplateProjectTestBase
Base class for tests which create a test site from a core project template.

Namespace

Drupal\Tests\package_manager\Build

Code

protected function assertExpectedStageEventsFired(string $expected_stage_class, ?array $expected_events = NULL, int $wait = 0, string $message = '') : void {
  if ($expected_events === NULL) {
    $expected_events = EventLogSubscriber::getSubscribedEvents();
    // The event subscriber uses this event to ensure the log file is excluded
    // from Package Manager operations, but it's not relevant for our purposes
    // because it's not part of the stage life cycle.
    unset($expected_events[CollectPathsToExcludeEvent::class]);
    $expected_events = array_keys($expected_events);
  }
  $this->assertNotEmpty($expected_events);
  $log_file = $this->getWorkspaceDirectory() . '/project/' . EventLogSubscriber::LOG_FILE_NAME;
  $max_wait = time() + $wait;
  do {
    $this->assertFileIsReadable($log_file);
    $log_data = file_get_contents($log_file);
    $log_data = json_decode($log_data, TRUE, flags: JSON_THROW_ON_ERROR);
    // Filter out events logged by any other stage.
    $log_data = array_filter($log_data, fn(array $event): bool => $event['stage'] === $expected_stage_class);
    // If we've logged at least the expected number of events, stop waiting.
    // Break out of the loop and assert the expected events were logged.
    if (count($log_data) >= count($expected_events)) {
      break;

    }
    // Wait a bit before checking again.
    sleep(5);
  } while ($max_wait > time());
  $this->assertSame($expected_events, array_column($log_data, 'event'), $message);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.