From 97775e78a586595a08428de44d6afc6e749496d8 Mon Sep 17 00:00:00 2001 From: Dave Long <dave@longwaveconsulting.com> Date: Mon, 7 Oct 2024 12:52:32 +0100 Subject: [PATCH] Issue #3477366 by andypost: Fix closures in tests for PHP 8.4 --- .../Functional/System/ErrorHandlerTest.php | 11 +++++++-- .../Bootstrap/UncaughtExceptionTest.php | 23 ++++++++++++++----- .../Core/Utility/CallableResolverTest.php | 4 ++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php b/core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php index 9bc4cab3cf1d..82ba0c8896a1 100644 --- a/core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php +++ b/core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php @@ -127,9 +127,14 @@ public function testExceptionHandler(): void { '%line' => 56, '%file' => $this->getModulePath('error_test') . '/error_test.module', ]; + $select = \Drupal::database()->select('bananas_are_awesome', 'b')->fields('b'); + $message = \Drupal::database()->prepareStatement((string) $select, [])->getQueryString(); + $message = str_replace(["\r", "\n"], ' ', $message); $error_pdo_exception = [ '%type' => 'DatabaseExceptionWrapper', - '@message' => 'SELECT "b".* FROM {bananas_are_awesome} "b"', + '@message' => PHP_VERSION_ID >= 80400 ? + $message : + 'SELECT "b".* FROM {bananas_are_awesome} "b"', '%function' => 'Drupal\error_test\Controller\ErrorTestController->triggerPDOException()', '%line' => 64, '%file' => $this->getModulePath('error_test') . '/error_test.module', @@ -137,7 +142,9 @@ public function testExceptionHandler(): void { $error_renderer_exception = [ '%type' => 'Exception', '@message' => 'This is an exception that occurs during rendering', - '%function' => 'Drupal\error_test\Controller\ErrorTestController->Drupal\error_test\Controller\{closure}()', + '%function' => PHP_VERSION_ID >= 80400 ? + 'Drupal\error_test\Controller\ErrorTestController->{closure:Drupal\error_test\Controller\ErrorTestController::triggerRendererException():104}()' : + 'Drupal\error_test\Controller\ErrorTestController->Drupal\error_test\Controller\{closure}()', '%line' => 82, '%file' => $this->getModulePath('error_test') . '/error_test.module', ]; diff --git a/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php b/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php index 302489162108..80fd287751c5 100644 --- a/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php +++ b/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php @@ -92,11 +92,20 @@ public function testUncaughtException(): void { * Tests displaying an uncaught fatal error. */ public function testUncaughtFatalError(): void { - $fatal_error = [ - '%type' => 'TypeError', - '@message' => 'Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}(): Argument #1 ($test) must be of type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 67', - '%function' => 'Drupal\error_test\Controller\ErrorTestController->Drupal\error_test\Controller\{closure}()', - ]; + if (PHP_VERSION_ID >= 80400) { + $fatal_error = [ + '%type' => 'TypeError', + '@message' => 'Drupal\error_test\Controller\ErrorTestController::{closure:Drupal\error_test\Controller\ErrorTestController::generateFatalErrors():64}(): Argument #1 ($test) must be of type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 67', + '%function' => 'Drupal\error_test\Controller\ErrorTestController->{closure:Drupal\error_test\Controller\ErrorTestController::generateFatalErrors():64}()', + ]; + } + else { + $fatal_error = [ + '%type' => 'TypeError', + '@message' => 'Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}(): Argument #1 ($test) must be of type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 67', + '%function' => 'Drupal\error_test\Controller\ErrorTestController->Drupal\error_test\Controller\{closure}()', + ]; + } $this->drupalGet('error-test/generate-fatal-errors'); $this->assertSession()->statusCodeEquals(500); $message = new FormattableMarkup('%type: @message in %function (line ', $fatal_error); @@ -155,7 +164,9 @@ public function testErrorContainer(): void { $this->writeSettings($settings); \Drupal::service('kernel')->invalidateContainer(); - $this->expectedExceptionMessage = 'Drupal\FunctionalTests\Bootstrap\ErrorContainer::Drupal\FunctionalTests\Bootstrap\{closure}(): Argument #1 ($container) must be of type Drupal\FunctionalTests\Bootstrap\ErrorContainer'; + $this->expectedExceptionMessage = PHP_VERSION_ID >= 80400 ? + 'Drupal\FunctionalTests\Bootstrap\ErrorContainer::{closure:Drupal\FunctionalTests\Bootstrap\ErrorContainer::get():21}(): Argument #1 ($container) must be of type Drupal\FunctionalTests\Bootstrap\ErrorContainer' : + 'Drupal\FunctionalTests\Bootstrap\ErrorContainer::Drupal\FunctionalTests\Bootstrap\{closure}(): Argument #1 ($container) must be of type Drupal\FunctionalTests\Bootstrap\ErrorContainer'; $this->drupalGet(''); $this->assertSession()->statusCodeEquals(500); diff --git a/core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php b/core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php index ed94cd09ff4c..0a5b45102b42 100644 --- a/core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php +++ b/core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php @@ -47,6 +47,8 @@ public function testCallbackResolver(): void { function ($suffix) { return __METHOD__ . '+' . $suffix; }, + PHP_VERSION_ID >= 80400 ? + '{closure:Drupal\Tests\Core\Utility\CallableResolverTest::testCallbackResolver():47}' : 'Drupal\Tests\Core\Utility\{closure}', ], 'First-class callable function' => [ @@ -59,6 +61,8 @@ function ($suffix) { ], 'Arrow function' => [ fn($suffix) => __METHOD__ . '+' . $suffix, + PHP_VERSION_ID >= 80400 ? + '{closure:Drupal\Tests\Core\Utility\CallableResolverTest::testCallbackResolver():63}' : 'Drupal\Tests\Core\Utility\{closure}', ], 'Static function' => [ -- GitLab