function StageEventsTest::testAddResult

Tests adding validation results to events.

File

core/modules/package_manager/tests/src/Kernel/StageEventsTest.php, line 159

Class

StageEventsTest
Tests that the stage fires events during its lifecycle.

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testAddResult() : void {
  $stage = $this->createStage();
  $error = ValidationResult::createError([
    $this->t('Burn, baby, burn!'),
  ]);
  $warning = ValidationResult::createWarning([
    $this->t('The path ahead is scary...'),
  ]);
  $excluded_paths = $this->createMock(PathListInterface::class);
  // Status check events can accept both errors and warnings.
  $event = new StatusCheckEvent($stage, $excluded_paths);
  $event->addResult($error);
  $event->addResult($warning);
  $this->assertSame([
    $error,
    $warning,
  ], $event->getResults());
  // Other stage events will accept errors, but throw an exception if you try
  // to add a warning.
  $event = new PreCreateEvent($stage, $excluded_paths);
  $event->addResult($error);
  $this->assertSame([
    $error,
  ], $event->getResults());
  $this->expectException(\InvalidArgumentException::class);
  $this->expectExceptionMessage('Only errors are allowed.');
  $event->addResult($warning);
}

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