You are here

class TestChecker1 in Automatic Updates 8.2

A test readiness checker.

Hierarchy

  • class \Drupal\automatic_updates_test\ReadinessChecker\TestChecker1 implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of TestChecker1

4 files declare their use of TestChecker1
ReadinessValidationManagerTest.php in tests/src/Kernel/ReadinessValidation/ReadinessValidationManagerTest.php
ReadinessValidationTest.php in tests/src/Functional/ReadinessValidationTest.php
TestChecker2.php in tests/modules/automatic_updates_test2/src/ReadinessChecker/TestChecker2.php
UpdaterFormTest.php in tests/src/Functional/UpdaterFormTest.php
1 string reference to 'TestChecker1'
automatic_updates_test.services.yml in tests/modules/automatic_updates_test/automatic_updates_test.services.yml
tests/modules/automatic_updates_test/automatic_updates_test.services.yml
1 service uses TestChecker1
automatic_updates_test.checker in tests/modules/automatic_updates_test/automatic_updates_test.services.yml
Drupal\automatic_updates_test\ReadinessChecker\TestChecker1

File

tests/modules/automatic_updates_test/src/ReadinessChecker/TestChecker1.php, line 13

Namespace

Drupal\automatic_updates_test\ReadinessChecker
View source
class TestChecker1 implements EventSubscriberInterface {

  /**
   * The key to use store the test results.
   */
  protected const STATE_KEY = 'automatic_updates_test.checker_results';

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Creates a TestChecker object.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * Sets messages for this readiness checker.
   *
   * This method is static to enable setting the expected messages before the
   * test module is enabled.
   *
   * @param \Drupal\automatic_updates\Validation\ValidationResult[]|\Throwable|null $checker_results
   *   The test validation results, or an exception to throw, or NULL to delete
   *   stored results.
   * @param string $event_name
   *   (optional )The event name. Defaults to
   *   AutomaticUpdatesEvents::READINESS_CHECK.
   */
  public static function setTestResult($checker_results, string $event_name = AutomaticUpdatesEvents::READINESS_CHECK) : void {
    $key = static::STATE_KEY . ".{$event_name}";
    if (isset($checker_results)) {
      \Drupal::state()
        ->set($key, $checker_results);
    }
    else {
      \Drupal::state()
        ->delete($key);
    }
  }

  /**
   * Adds test result to an update event from a state setting.
   *
   * @param \Drupal\automatic_updates\Event\UpdateEvent $event
   *   The update event.
   * @param string $state_key
   *   The state key.
   */
  protected function addResults(UpdateEvent $event, string $state_key) : void {
    $results = $this->state
      ->get($state_key, []);
    if ($results instanceof \Throwable) {
      throw $results;
    }
    foreach ($results as $result) {
      $event
        ->addValidationResult($result);
    }
  }

  /**
   * Adds test results for the readiness check event.
   *
   * @param \Drupal\automatic_updates\Event\UpdateEvent $event
   *   The update event.
   */
  public function runPreChecks(UpdateEvent $event) : void {
    $this
      ->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::READINESS_CHECK);
  }

  /**
   * Adds test results for the pre-commit event.
   *
   * @param \Drupal\automatic_updates\Event\UpdateEvent $event
   *   The update event.
   */
  public function runPreCommitChecks(UpdateEvent $event) : void {
    $this
      ->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::PRE_COMMIT);
  }

  /**
   * Adds test results for the pre-start event.
   *
   * @param \Drupal\automatic_updates\Event\UpdateEvent $event
   *   The update event.
   */
  public function runStartChecks(UpdateEvent $event) : void {
    $this
      ->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::PRE_START);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $priority = defined('AUTOMATIC_UPDATES_TEST_SET_PRIORITY') ? AUTOMATIC_UPDATES_TEST_SET_PRIORITY : 5;
    $events[AutomaticUpdatesEvents::READINESS_CHECK][] = [
      'runPreChecks',
      $priority,
    ];
    $events[AutomaticUpdatesEvents::PRE_START][] = [
      'runStartChecks',
      $priority,
    ];
    $events[AutomaticUpdatesEvents::PRE_COMMIT][] = [
      'runPreCommitChecks',
      $priority,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TestChecker1::$state protected property The state service.
TestChecker1::addResults protected function Adds test result to an update event from a state setting.
TestChecker1::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. 1
TestChecker1::runPreChecks public function Adds test results for the readiness check event.
TestChecker1::runPreCommitChecks public function Adds test results for the pre-commit event.
TestChecker1::runStartChecks public function Adds test results for the pre-start event.
TestChecker1::setTestResult public static function Sets messages for this readiness checker.
TestChecker1::STATE_KEY protected constant The key to use store the test results. 1
TestChecker1::__construct public function Creates a TestChecker object.