summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-08-26 23:29:29 (GMT)
committerAlex Pott2015-08-26 23:29:29 (GMT)
commit5bb2889d50d4a30ca43bf793affea51d88952c0b (patch)
tree4c6806a3b9395b67000dbb1a7f4d68b9e1585ed6
parentf29f1e212967ce4aa5f879a30d828565bb44089c (diff)
Issue #2536560 by Aki Tendo, stefan.r, damiankloip: Runtime Assertion unit and functional testing
-rw-r--r--core/core.api.php2
-rw-r--r--core/lib/Drupal/Component/Assertion/Handle.php75
-rw-r--r--core/lib/Drupal/Core/DrupalKernel.php6
-rw-r--r--core/modules/simpletest/src/Tests/SimpleTestTest.php23
-rw-r--r--core/tests/bootstrap.php7
-rw-r--r--sites/example.settings.local.php3
6 files changed, 107 insertions, 9 deletions
diff --git a/core/core.api.php b/core/core.api.php
index baba9a2..e59bd2c 100644
--- a/core/core.api.php
+++ b/core/core.api.php
@@ -1004,7 +1004,7 @@
* verified with standard control structures at all times, not just checked in
* development environments with assert() statements on.
*
- * When runtime assertions fail in PHP 7 an \AssertionException is thrown.
+ * When runtime assertions fail in PHP 7 an \AssertionError is thrown.
* Drupal uses an assertion callback to do the same in PHP 5.x so that unit
* tests involving runtime assertions will work uniformly across both versions.
*
diff --git a/core/lib/Drupal/Component/Assertion/Handle.php b/core/lib/Drupal/Component/Assertion/Handle.php
new file mode 100644
index 0000000..3912156
--- /dev/null
+++ b/core/lib/Drupal/Component/Assertion/Handle.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\Component\Assertion\Handle.
+ *
+ * For PHP 5 this contains \AssertionError as well.
+ */
+
+namespace {
+
+if (!class_exists('AssertionError', FALSE)) {
+
+ /**
+ * Emulates PHP 7 AssertionError as closely as possible.
+ *
+ * We force this class to exist at the root namespace for PHP 5.
+ * This class exists natively in PHP 7. Note that in PHP 7 it extends from
+ * Error, not Exception, but that isn't possible for PHP 5 - all exceptions
+ * must extend from exception.
+ */
+ 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;
+ }
+
+ }
+}
+
+}
+
+namespace Drupal\Component\Assertion {
+
+/**
+ * Handler for runtime assertion failures.
+ *
+ * This class allows PHP 5.x to throw exceptions on runtime assertion fails
+ * in the same manner as PHP 7, and sets the ASSERT_EXCEPTION flag to TRUE
+ * for the PHP 7 runtime.
+ *
+ * @ingroup php_assert
+ */
+class Handle {
+
+ /**
+ * 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, 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 8ae5924..2ddc3ee 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -923,6 +923,12 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
// Simpletest's internal browser.
define('DRUPAL_TEST_IN_CHILD_SITE', TRUE);
+ // Web tests are to be conducted with runtime assertions active.
+ assert_options(ASSERT_ACTIVE, TRUE);
+ // 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.
ini_set('log_errors', 1);
ini_set('error_log', DRUPAL_ROOT . '/sites/simpletest/' . substr($test_prefix, 10) . '/error.log');
diff --git a/core/modules/simpletest/src/Tests/SimpleTestTest.php b/core/modules/simpletest/src/Tests/SimpleTestTest.php
index 1f97845..c3758a7 100644
--- a/core/modules/simpletest/src/Tests/SimpleTestTest.php
+++ b/core/modules/simpletest/src/Tests/SimpleTestTest.php
@@ -164,7 +164,16 @@ EOD;
$site_path = $this->container->get('site.path');
file_put_contents($key_file, $private_key);
- // This causes the first of the fifteen passes asserted in
+ // Check to see if runtime assertions are indeed on, if successful this
+ // will be the first of sixteen passes asserted in confirmStubResults()
+ try {
+ assert(FALSE, 'Lorem Ipsum');
+ $this->fail('Runtime assertions are not working.');
+ }
+ catch (\AssertionError $e) {
+ $this->assertEqual($e->getMessage(), 'Lorem Ipsum', 'Runtime assertions Enabled and running.');
+ }
+ // This causes the second of the sixteen passes asserted in
// confirmStubResults().
$this->pass($this->passMessage);
@@ -175,7 +184,7 @@ EOD;
// confirmStubResults().
$this->fail($this->failMessage);
- // This causes the second to fourth of the fifteen passes asserted in
+ // This causes the third to fifth of the sixteen passes asserted in
// confirmStubResults().
$user = $this->drupalCreateUser(array($this->validPermission), 'SimpleTestTest');
@@ -183,15 +192,15 @@ EOD;
$this->drupalCreateUser(array($this->invalidPermission));
// Test logging in as a user.
- // This causes the fifth to ninth of the fifteen passes asserted in
+ // This causes the sixth to tenth of the sixteen passes asserted in
// confirmStubResults().
$this->drupalLogin($user);
- // This causes the tenth of the fifteen passes asserted in
+ // This causes the eleventh of the sixteen passes asserted in
// confirmStubResults().
$this->pass(t('Test ID is @id.', array('@id' => $this->testId)));
- // These cause the eleventh to fourteenth of the fifteen passes asserted in
+ // These cause the twelfth to fifteenth of the sixteen passes asserted in
// confirmStubResults().
$this->assertTrue(file_exists($site_path . '/settings.testing.php'));
// Check the settings.testing.php file got included.
@@ -206,7 +215,7 @@ EOD;
// Generates a warning inside a PHP function.
array_key_exists(NULL, NULL);
- // This causes the fifteenth of the fifteen passes asserted in
+ // This causes the sixteenth of the sixteen passes asserted in
// confirmStubResults().
$this->assertNothing();
@@ -250,7 +259,7 @@ EOD;
$this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
- $this->assertEqual('15 passes, 3 fails, 2 exceptions, 3 debug messages', $this->childTestResults['summary']);
+ $this->assertEqual('16 passes, 3 fails, 2 exceptions, 3 debug messages', $this->childTestResults['summary']);
$this->testIds[] = $test_id = $this->getTestIdFromResults();
$this->assertTrue($test_id, 'Found test ID in results.');
diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php
index 8934525..31ac4cd 100644
--- a/core/tests/bootstrap.php
+++ b/core/tests/bootstrap.php
@@ -108,3 +108,10 @@ setlocale(LC_ALL, 'C');
// and DST). This choice is made to prevent timezone related regressions and
// reduce the fragility of the testing system in general.
date_default_timezone_set('Australia/Sydney');
+
+// 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();
+
diff --git a/sites/example.settings.local.php b/sites/example.settings.local.php
index 34b4e19..23090a4 100644
--- a/sites/example.settings.local.php
+++ b/sites/example.settings.local.php
@@ -27,7 +27,8 @@
*
* @see https://wiki.php.net/rfc/expectations
*/
-assert_options(ASSERT_ACTIVE, 1);
+assert_options(ASSERT_ACTIVE, TRUE);
+\Drupal\Component\Assertion\Handle::register();
/**
* Enable local development services.