You are here

final class WatchdogContext in Lightning Core 8.5

Same name and namespace in other branches
  1. 8.2 tests/contexts/WatchdogContext.behat.inc \WatchdogContext
  2. 8.3 tests/contexts/WatchdogContext.behat.inc \WatchdogContext
  3. 8.4 tests/contexts/WatchdogContext.behat.inc \WatchdogContext

Tracks errors logged to the database during the scenario.

@internal This is an internal part of Lightning Core's testing system and may be changed or removed at any time without warning. It should not be extended, instantiated, or used in any way by external code! If you need to use this functionality, you should copy the relevant code into your own project.

Hierarchy

  • class \WatchdogContext extends \Drupal\DrupalExtension\Context\DrupalSubContextBase

Expanded class hierarchy of WatchdogContext

File

tests/contexts/WatchdogContext.behat.inc, line 15

View source
final class WatchdogContext extends DrupalSubContextBase {

  /**
   * Start time for the scenario.
   *
   * @var int
   */
  private $startTime;

  /**
   * Indicates if dblog was installed during the scenario.
   *
   * @var bool
   */
  private $uninstall = FALSE;

  /**
   * Checks if errors should be checked after the scenario.
   *
   * @param \Behat\Behat\Hook\Scope\ScenarioScope $scope
   *   The scenario scope.
   *
   * @return bool
   *   TRUE if errors should be checked, FALSE otherwise.
   */
  private function isDiabled(ScenarioScope $scope) {
    return in_array('errors', $scope
      ->getScenario()
      ->getTags());
  }

  /**
   * Install dblog and store the time the scenario began.
   *
   * @param \Behat\Behat\Hook\Scope\ScenarioScope $scope
   *   The scenario scope.
   *
   * @BeforeScenario ~@errors
   */
  public function setUp(ScenarioScope $scope) {
    if ($this
      ->isDiabled($scope)) {
      return;
    }
    if (!\Drupal::moduleHandler()
      ->moduleExists('dblog')) {
      $this->uninstall = \Drupal::service('module_installer')
        ->install([
        'dblog',
      ]);
    }
    $this->startTime = time();
  }

  /**
   * Check for errors since the scenario started.
   *
   * @param \Behat\Behat\Hook\Scope\ScenarioScope $scope
   *   The scenario scope.
   *
   * @AfterScenario ~@errors
   */
  public function checkWatchdog(ScenarioScope $scope) {
    if ($this
      ->isDiabled($scope)) {
      return;
    }
    $db = \Drupal::database();
    if ($db
      ->schema()
      ->tableExists('watchdog')) {
      $log = $db
        ->select('watchdog', 'w')
        ->fields('w')
        ->condition('w.type', 'php', '=')
        ->condition('w.timestamp', $this->startTime, '>=')
        ->execute()
        ->fetchAll();
      if ($log) {
        foreach ($log as $error) {

          // Make the substitutions easier to read in the log.
          $error->variables = unserialize($error->variables);
          print_r($error);
        }
        throw new \Exception('PHP errors logged to watchdog in this scenario.');
      }
    }
    if ($this->uninstall) {
      \Drupal::service('module_installer')
        ->uninstall([
        'dblog',
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WatchdogContext::$startTime private property Start time for the scenario.
WatchdogContext::$uninstall private property Indicates if dblog was installed during the scenario.
WatchdogContext::checkWatchdog public function Check for errors since the scenario started.
WatchdogContext::isDiabled private function Checks if errors should be checked after the scenario.
WatchdogContext::setUp public function Install dblog and store the time the scenario began.