ErrorTestController.php 2.3 KB
Newer Older
1
2
3
4
5
<?php

namespace Drupal\error_test\Controller;

use Drupal\Core\Controller\ControllerBase;
6
7
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
10
11

/**
 * Controller routines for error_test routes.
 */
12
class ErrorTestController extends ControllerBase {
13

14
15
16
  /**
   * The database connection.
   *
17
   * @var \Drupal\Core\Database\Connection
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   */
  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')
    );
  }
39
40
41
42
43
44
45
46
47
48

  /**
   * 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.
49
    $awesomely_big = 1 / 0;
50
51
    // This will generate a user error. Use & to check for double escaping.
    trigger_error("Drupal & awesome", E_USER_WARNING);
52
    return [];
53
54
  }

55
56
57
58
  /**
   * Generate fatals to test the error handler.
   */
  public function generateFatals() {
59
    $function = function (array $test) {
60
61
62
63
64
65
    };

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

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

  /**
   * Trigger an exception to test the PDO exception handler.
   */
  public function triggerPDOException() {
    define('SIMPLETEST_COLLECT_ERRORS', FALSE);
79
80
81
    $this->database->select('bananas_are_awesome', 'b')
      ->fields('b')
      ->execute();
82
83
  }

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

98
}