From 207d339b6d12a0d03317a17bddd4145407883db5 Mon Sep 17 00:00:00 2001 From: mmorris Date: Thu, 13 Aug 2015 10:43:08 -0400 Subject: [PATCH] Additional tweaks, phpcs pass. --- core/lib/Drupal/Component/Assertion/Handle.php | 27 ++++++++++++++++++++++---- core/lib/Drupal/Core/DrupalKernel.php | 5 +++-- core/tests/bootstrap.php | 6 +++++- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/core/lib/Drupal/Component/Assertion/Handle.php b/core/lib/Drupal/Component/Assertion/Handle.php index 08131e6..019f320 100644 --- a/core/lib/Drupal/Component/Assertion/Handle.php +++ b/core/lib/Drupal/Component/Assertion/Handle.php @@ -9,8 +9,22 @@ // Error, not Exception, but that isn't possible for PHP 5 - all exceptions // must extend from exception. namespace { - if(!class_exists('AssertionError', FALSE)) { - class AssertionError extends Exception {} + if (!class_exists('AssertionError', FALSE)) { + /** + * Emulates PHP 7 AssertionError as closely as possible. + */ + class AssertionError extends Exception { + /** + * {@inheritdoc} + */ + public function __construct($message = '', $code = 0, Exception $previous = NULL, $file = '', $line = 0) { + parent::__construct($message, $code, $previous); + // Preserve the filename and line number of the assertion failure. + $this->file = $file; + $this->line = $line; + } + + } } } @@ -26,18 +40,23 @@ class AssertionError extends Exception {} */ class Handle { /** - * Registers an assertion handler in PHP 5, sets exception throwing in PHP 7. + * Registers uniform assertion handling. */ public static function register() { + // Since we're using exceptions, turn error warnings off. + assert_options(ASSERT_WARNING, FALSE); + if (version_compare(PHP_VERSION, '7.0.0-dev') < 0) { + // PHP 5 - create a handler to throw the exception directly. assert_options(ASSERT_CALLBACK, function($file, $line, $code, $message) { if (empty($message)) { $message = $code; } - throw new \AssertionError($message); + throw new \AssertionError($message, 0, NULL, $file, $line); }); } else { + // PHP 7 - just turn exception throwing on. assert_options(ASSERT_EXCEPTION, TRUE); } } diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 89c299b..464e649 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -848,9 +848,10 @@ public static function bootEnvironment() { // Simpletest's internal browser. define('DRUPAL_TEST_IN_CHILD_SITE', TRUE); - // Turn assertions on, sync PHP 5 & 7's handling of them. + // Webtests are to be conducted with runtime assertions active. assert_options(ASSERT_ACTIVE, TRUE); - // No use statement for this, we don't use this class outside testing. + // Now synchronize PHP 5 and 7's handling of assertions as much as + // possible. \Drupal\Component\Assertion\Handle::register(); // Log fatal errors to the test site directory. diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php index 9fc2b11..20e8b09 100644 --- a/core/tests/bootstrap.php +++ b/core/tests/bootstrap.php @@ -93,5 +93,9 @@ function drupal_phpunit_register_extension_dirs(Composer\Autoload\ClassLoader $l // reduce the fragility of the testing system in general. date_default_timezone_set('Australia/Sydney'); -// Register the default assertion handler. +// Runtime assertions. PHPUnit follows the php.ini assert.active setting for +// runtime assertions. By default this setting is on. Here we make a call to +// make PHP 5 and 7 handle assertion failures the same way, but this call does +// not turn runtime assertions on if they weren't on already. \Drupal\Component\Assertion\Handle::register(); + -- 1.8.4.2