diff -u b/core/includes/bootstrap.inc b/core/includes/bootstrap.inc --- b/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -1134,23 +1134,35 @@ } /** - * Unify PHP 5 and 7's assertion handling. + * Registers the assertion handle. + * + * This assertion handle unifies assertion handling for PHP 5 and PHP 7. */ function drupal_register_assert_handle() { - // PHP 7 - just make sure assert failures are thrown. + if (!assert_options(ASSERT_ACTIVE)) { + return; + } + + // In PHP 7, just make sure assert failures are thrown. if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) { assert_options(ASSERT_EXCEPTION, 1); } - // PHP 5 - we must establish an AssertionError to throw and a assert callback - // handle to throw it. + // In PHP 5, and establish an assert callback handle that will throw AssertionErrors. elseif (assert_options(ASSERT_CALLBACK) === NULL) { - class AssertionError extends Exception {} + class AssertionError extends Exception { + public function __construct($message, $code = 0, Exception $previous = NULL, $file, $line) { + parent::__construct($message, $code, $previous); + // Preserve the filename and line number of the assertion failure. + $this->file = $file; + $this->line = $line; + } + } assert_options(ASSERT_CALLBACK, function($file, $line, $code, $message = '') { if (empty($message)) { - $message = "Assertion Failure in {$file} at {$line}. Failed asserting {$code}"; + $message = $code; } - throw new AssertionError($message); + throw new AssertionError($message, 0, NULL, $file, $line); }); } } diff -u b/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php --- b/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -862,6 +862,10 @@ // Simpletest's internal browser. define('DRUPAL_TEST_IN_CHILD_SITE', TRUE); + // Enable runtime assertions. + assert_options(ASSERT_ACTIVE, 1); + drupal_register_assert_handle(); + // Log fatal errors to the test site directory. ini_set('log_errors', 1); ini_set('error_log', DRUPAL_ROOT . '/sites/simpletest/' . substr($test_prefix, 10) . '/error.log'); @@ -870,10 +874,6 @@ // Simpletest's internal browser. define('DRUPAL_TEST_IN_CHILD_SITE', TRUE); - // Enable runtime assertions. - assert_options(ASSERT_ACTIVE, 1); - drupal_register_assert_handle(); - // Log fatal errors to the test site directory. ini_set('log_errors', 1); ini_set('error_log', DRUPAL_ROOT . '/sites/simpletest/' . substr($test_prefix, 10) . '/error.log'); diff -u b/core/modules/simpletest/src/Tests/SimpleTestTest.php b/core/modules/simpletest/src/Tests/SimpleTestTest.php --- b/core/modules/simpletest/src/Tests/SimpleTestTest.php +++ b/core/modules/simpletest/src/Tests/SimpleTestTest.php @@ -168,9 +168,17 @@ // will be the first of sixteen passes asserted in confirmStubResults() try { assert(FALSE); - $this->fail('Runtime Assertions are not working.'); + $this->fail('Runtime assertions are not working.'); } catch (\AssertionError $e) { - $this->pass('Runtime Assertions Enabled and running.'); + $this->pass('Runtime assertions enabled and running.'); + $this->assertEqual($e->getMessage, 'FALSE'); + } + + // Check to see that assertion messsages get passed along. + try { + assert(FALSE, 'Success!'); + } catch (\AssertionError $e) { + $this->assertEqual($e->getMessage, 'Success!'); } // This causes the second of the sixteen passes asserted in @@ -196,7 +204,7 @@ // confirmStubResults(). $this->drupalLogin($user); - // This causes the twelth of the sixteen passes asserted in + // This causes the twelfth of the sixteen passes asserted in // confirmStubResults(). $this->pass(t('Test ID is @id.', array('@id' => $this->testId))); diff -u b/core/tests/bootstrap.php b/core/tests/bootstrap.php --- b/core/tests/bootstrap.php +++ b/core/tests/bootstrap.php @@ -93,16 +93,3 @@ -// PHP 7 - just make sure assert failures are thrown. -if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) { - assert_options(ASSERT_EXCEPTION, 1); -} -// PHP 5 - we must establish an AssertionError to throw and a assert callback -// handle to throw it. -elseif (assert_options(ASSERT_CALLBACK) === NULL) { - class AssertionError extends Exception {} - - assert_options(ASSERT_CALLBACK, function($file, $line, $code, $message = '') { - if (empty($message)) { - $message = "Assertion Failure in {$file} at {$line}. Failed asserting {$code}"; - } - throw new AssertionError($message); - }); -} +// Register the assertion handle. +require_once __DIR__ . '/../includes/bootstrap.inc'; +drupal_register_assert_handle(); reverted: --- b/sites/example.settings.local.php +++ a/sites/example.settings.local.php @@ -28,7 +28,6 @@ * @see https://wiki.php.net/rfc/expectations */ assert_options(ASSERT_ACTIVE, 1); -drupal_register_assert_handle(); /** * Enable local development services.