ErrorTestController.php 2.35 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @file
 * Contains \Drupal\error_test\Controller\ErrorTestController.
 */

namespace Drupal\error_test\Controller;

use Drupal\Core\Controller\ControllerBase;
10 11
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;
12 13 14 15

/**
 * Controller routines for error_test routes.
 */
16
class ErrorTestController extends ControllerBase {
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  /**
   * 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')
    );
  }
43 44 45 46 47 48 49 50 51 52 53

  /**
   * 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.
    $monkey_love = $bananas;
    // This will generate a warning.
    $awesomely_big = 1/0;
54 55
    // This will generate a user error. Use & to check for double escaping.
    trigger_error("Drupal & awesome", E_USER_WARNING);
56
    return [];
57 58
  }

59 60 61 62 63 64 65 66 67 68 69
  /**
   * Generate fatals to test the error handler.
   */
  public function generateFatals() {
    $function = function(array $test) {
    };

    $function("test-string");
    return [];
  }

70 71 72 73 74
  /**
   * Trigger an exception to test the exception handler.
   */
  public function triggerException() {
    define('SIMPLETEST_COLLECT_ERRORS', FALSE);
75
    throw new \Exception("Drupal & awesome");
76 77 78 79 80 81 82
  }

  /**
   * Trigger an exception to test the PDO exception handler.
   */
  public function triggerPDOException() {
    define('SIMPLETEST_COLLECT_ERRORS', FALSE);
83
    $this->database->query('SELECT * FROM bananas_are_awesome');
84 85
  }

86 87 88 89 90 91 92 93 94 95 96 97 98 99
  /**
   * 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');
        }
      ],
    ];
  }

100
}