function StageConflictTest::testDestroyDuringApply

Tests destroying a stage while applying it.

@dataProvider providerDestroyDuringApply

Parameters

string $event_class: The event class for which to attempt to destroy the stage.

bool $force: Whether the stage should be force destroyed.

int $time_offset: How many simulated seconds should have elapsed between the PreApplyEvent being dispatched and the attempt to destroy the stage.

string|null $expected_exception_message: The expected exception message string if an exception is expected, or NULL if no exception message was expected.

File

core/modules/package_manager/tests/src/Kernel/StageConflictTest.php, line 125

Class

StageConflictTest
@coversDefaultClass \Drupal\package_manager\SandboxManagerBase[[api-linebreak]] @covers \Drupal\package_manager\PackageManagerUninstallValidator[[api-linebreak]] @group package_manager @group #slow @internal

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testDestroyDuringApply(string $event_class, bool $force, int $time_offset, ?string $expected_exception_message) : void {
  $listener = function (SandboxEvent $event) use ($force, $time_offset) : void {
    // Simulate that a certain amount of time has passed since we started
    // applying staged changes. After a point, it should be possible to
    // destroy the stage even if it hasn't finished.
    TestTime::$offset = $time_offset;
    // No real-life event subscriber should try to destroy the stage while
    // handling another event. The only reason we're doing it here is to
    // simulate an attempt to destroy the stage while it's being applied, for
    // testing purposes.
    $event->sandboxManager
      ->destroy($force);
    LoggingCommitter::setException(PreconditionException::class, $this->createMock(PreconditionInterface::class), $this->createComposeStagerMessage('Stage directory does not exist'));
  };
  $this->addEventTestListener($listener, $event_class, 0);
  $stage = $this->createStage();
  $stage->create();
  $stage->require([
    'ext-json:*',
  ]);
  if ($expected_exception_message) {
    $this->expectException(SandboxException::class);
    $this->expectExceptionMessage($expected_exception_message);
  }
  $stage->apply();
  // If the stage was successfully destroyed by the event handler (i.e., the
  // stage has been applying for too long and is therefore considered stale),
  // the postApply() method should fail because the stage is not claimed.
  if ($stage->isAvailable()) {
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Stage must be claimed before performing any operations on it.');
  }
  $stage->postApply();
}

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