class ErrorTestController

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php \Drupal\error_test\Controller\ErrorTestController

Controller routines for error_test routes.

Hierarchy

Expanded class hierarchy of ErrorTestController

1 file declares its use of ErrorTestController
DbLogTest.php in core/modules/dblog/tests/src/Functional/DbLogTest.php

File

core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php, line 12

Namespace

Drupal\error_test\Controller
View source
class ErrorTestController extends ControllerBase {
  
  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;
  
  /**
   * Constructs a \Drupal\error_test\Controller\ErrorTestController object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('database'));
  }
  
  /**
   * Generate warnings to test the error handler.
   */
  public function generateWarnings($collect_errors = FALSE) {
    // Tell Drupal error reporter to send errors to Simpletest or not.
    define('SIMPLETEST_COLLECT_ERRORS', $collect_errors);
    // This will generate a notice.
    $notice = new \stdClass();
    $notice == 1 ? 1 : 0;
    // This will generate a warning.
    $obj = new \stdClass();
    $obj->p =& $obj;
    var_export($obj, TRUE);
    // This will generate a user error. Use & to check for double escaping.
    trigger_error("Drupal & awesome", E_USER_WARNING);
    return [];
  }
  
  /**
   * Generate fatals to test the error handler.
   */
  public function generateFatals() {
    $function = function (array $test) {
    };
    $function("test-string");
    return [];
  }
  
  /**
   * Trigger an exception to test the exception handler.
   */
  public function triggerException() {
    define('SIMPLETEST_COLLECT_ERRORS', FALSE);
    throw new \Exception("Drupal & awesome");
  }
  
  /**
   * Trigger an exception to test the PDO exception handler.
   */
  public function triggerPDOException() {
    define('SIMPLETEST_COLLECT_ERRORS', FALSE);
    $this->database
      ->select('bananas_are_awesome', 'b')
      ->fields('b')
      ->execute();
  }
  
  /**
   * Trigger an exception during rendering.
   */
  public function triggerRendererException() {
    return [
      '#type' => 'page',
      '#post_render' => [
        function () {
          throw new \Exception('This is an exception that occurs during rendering');
        },
      ],
    ];
  }

}

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