From b59e63e9904a70013621f4c824ab3f98cd3add8e Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 5 Apr 2024 13:21:21 +0100
Subject: [PATCH] Issue #3427177 by longwave, quietone, dineshkumarbollu,
 samit.310@gmail.com, mondrake, alexpott: Replace calls to ::expectError*()
 from Drupal\Tests\Core\Render\ElementTest

---
 core/.phpstan-baseline.php                          | 12 ------------
 core/lib/Drupal/Core/Render/Element.php             | 11 ++---------
 core/tests/Drupal/Tests/Core/Render/ElementTest.php |  4 ++--
 3 files changed, 4 insertions(+), 23 deletions(-)

diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php
index 1f1aae4f6595..bd693e8a5126 100644
--- a/core/.phpstan-baseline.php
+++ b/core/.phpstan-baseline.php
@@ -2245,18 +2245,6 @@
 	'count' => 1,
 	'path' => __DIR__ . '/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Call to deprecated method expectError\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\:
-https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
-	'count' => 1,
-	'path' => __DIR__ . '/tests/Drupal/Tests/Core/Render/ElementTest.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Call to deprecated method expectErrorMessage\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\:
-https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
-	'count' => 1,
-	'path' => __DIR__ . '/tests/Drupal/Tests/Core/Render/ElementTest.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Call to deprecated method expectWarning\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\:
 https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
diff --git a/core/lib/Drupal/Core/Render/Element.php b/core/lib/Drupal/Core/Render/Element.php
index ae329e8aee5b..f8f61919c00a 100644
--- a/core/lib/Drupal/Core/Render/Element.php
+++ b/core/lib/Drupal/Core/Render/Element.php
@@ -2,7 +2,6 @@
 
 namespace Drupal\Core\Render;
 
-use Drupal\Component\Render\FormattableMarkup;
 use Drupal\Core\Access\AccessResultInterface;
 
 /**
@@ -92,16 +91,10 @@ public static function children(array &$elements, $sort = FALSE) {
           // the insertion order.
           $child_weights[$key] = floor($weight * 1000) + $i / $count;
         }
-        // Only trigger an error if the value is not null.
+        // Only trigger an exception if the value is not null.
         // @see https://www.drupal.org/node/1283892
         elseif (isset($value)) {
-          trigger_error(new FormattableMarkup(
-            '"@key" is an invalid render array key. Value should be an array but got a @type',
-            [
-              '@key' => $key,
-              '@type' => gettype($value),
-            ]
-          ), E_USER_ERROR);
+          throw new \InvalidArgumentException(sprintf('"%s" is an invalid render array key. Value should be an array but got a %s.', $key, gettype($value)));
         }
       }
       $i++;
diff --git a/core/tests/Drupal/Tests/Core/Render/ElementTest.php b/core/tests/Drupal/Tests/Core/Render/ElementTest.php
index bc1b0217c6de..eba2dd726845 100644
--- a/core/tests/Drupal/Tests/Core/Render/ElementTest.php
+++ b/core/tests/Drupal/Tests/Core/Render/ElementTest.php
@@ -108,8 +108,8 @@ public function testInvalidChildren() {
     $element = [
       'foo' => 'bar',
     ];
-    $this->expectError();
-    $this->expectErrorMessage('"foo" is an invalid render array key. Value should be an array but got a string');
+    $this->expectException(\InvalidArgumentException::class);
+    $this->expectExceptionMessage('"foo" is an invalid render array key. Value should be an array but got a string.');
     Element::children($element);
   }
 
-- 
GitLab