From 0c9ae67deb4966f365dae73bad9a5b712a6d2390 Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Sat, 18 Sep 2021 10:24:59 +1000
Subject: [PATCH] Issue #3192839 by neclimdul: Convert tests in Renderer to
 assertions

---
 core/lib/Drupal/Core/Render/Renderer.php      | 26 +++++--------------
 .../Core/Render/RendererPlaceholdersTest.php  | 10 +++----
 2 files changed, 11 insertions(+), 25 deletions(-)

diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php
index 2ea2ca113c0b..9deb15d9f419 100644
--- a/core/lib/Drupal/Core/Render/Renderer.php
+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -304,23 +304,12 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
     // First validate the usage of #lazy_builder; both of the next if-statements
     // use it if available.
     if (isset($elements['#lazy_builder'])) {
-      // @todo Convert to assertions once https://www.drupal.org/node/2408013
-      //   lands.
-      if (!is_array($elements['#lazy_builder'])) {
-        throw new \DomainException('The #lazy_builder property must have an array as a value.');
-      }
-      if (count($elements['#lazy_builder']) !== 2) {
-        throw new \DomainException('The #lazy_builder property must have an array as a value, containing two values: the callback, and the arguments for the callback.');
-      }
-      if (count($elements['#lazy_builder'][1]) !== count(array_filter($elements['#lazy_builder'][1], function ($v) {
+      assert(is_array($elements['#lazy_builder']), 'The #lazy_builder property must have an array as a value.');
+      assert(count($elements['#lazy_builder']) === 2, 'The #lazy_builder property must have an array as a value, containing two values: the callback, and the arguments for the callback.');
+      assert(count($elements['#lazy_builder'][1]) === count(array_filter($elements['#lazy_builder'][1], function ($v) {
         return is_null($v) || is_scalar($v);
-      }))) {
-        throw new \DomainException("A #lazy_builder callback's context may only contain scalar values or NULL.");
-      }
-      $children = Element::children($elements);
-      if ($children) {
-        throw new \DomainException(sprintf('When a #lazy_builder callback is specified, no children can exist; all children must be generated by the #lazy_builder callback. You specified the following children: %s.', implode(', ', $children)));
-      }
+      })), "A #lazy_builder callback's context may only contain scalar values or NULL.");
+      assert(!Element::children($elements), sprintf('When a #lazy_builder callback is specified, no children can exist; all children must be generated by the #lazy_builder callback. You specified the following children: %s.', implode(', ', Element::children($elements))));
       $supported_keys = [
         '#lazy_builder',
         '#cache',
@@ -331,10 +320,7 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
         '#weight',
         '#printed',
       ];
-      $unsupported_keys = array_diff(array_keys($elements), $supported_keys);
-      if (count($unsupported_keys)) {
-        throw new \DomainException(sprintf('When a #lazy_builder callback is specified, no properties can exist; all properties must be generated by the #lazy_builder callback. You specified the following properties: %s.', implode(', ', $unsupported_keys)));
-      }
+      assert(empty(array_diff(array_keys($elements), $supported_keys)), sprintf('When a #lazy_builder callback is specified, no properties can exist; all properties must be generated by the #lazy_builder callback. You specified the following properties: %s.', implode(', ', array_diff(array_keys($elements), $supported_keys))));
     }
     // Determine whether to do auto-placeholdering.
     if ($this->placeholderGenerator->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) {
diff --git a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
index 1d41764d35b4..d13b41614008 100644
--- a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
+++ b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
@@ -853,7 +853,7 @@ public function testInvalidLazyBuilder() {
     $element = [];
     $element['#lazy_builder'] = '\Drupal\Tests\Core\Render\PlaceholdersTest::callback';
 
-    $this->expectException(\DomainException::class);
+    $this->expectException(\AssertionError::class);
     $this->expectExceptionMessage('The #lazy_builder property must have an array as a value.');
     $this->renderer->renderRoot($element);
   }
@@ -866,7 +866,7 @@ public function testInvalidLazyBuilderArguments() {
     $element = [];
     $element['#lazy_builder'] = ['\Drupal\Tests\Core\Render\PlaceholdersTest::callback', 'arg1', 'arg2'];
 
-    $this->expectException(\DomainException::class);
+    $this->expectException(\AssertionError::class);
     $this->expectExceptionMessage('The #lazy_builder property must have an array as a value, containing two values: the callback, and the arguments for the callback.');
     $this->renderer->renderRoot($element);
   }
@@ -914,7 +914,7 @@ public function testNonScalarLazybuilderCallbackContext() {
       ],
     ];
 
-    $this->expectException(\DomainException::class);
+    $this->expectException(\AssertionError::class);
     $this->expectExceptionMessage("A #lazy_builder callback's context may only contain scalar values or NULL.");
     $this->renderer->renderRoot($element);
   }
@@ -929,7 +929,7 @@ public function testChildrenPlusBuilder() {
     $element['child_a']['#markup'] = 'Oh hai!';
     $element['child_b']['#markup'] = 'kthxbai';
 
-    $this->expectException(\DomainException::class);
+    $this->expectException(\AssertionError::class);
     $this->expectExceptionMessage('When a #lazy_builder callback is specified, no children can exist; all children must be generated by the #lazy_builder callback. You specified the following children: child_a, child_b.');
     $this->renderer->renderRoot($element);
   }
@@ -944,7 +944,7 @@ public function testPropertiesPlusBuilder() {
     $element['#llama'] = '#awesome';
     $element['#piglet'] = '#cute';
 
-    $this->expectException(\DomainException::class);
+    $this->expectException(\AssertionError::class);
     $this->expectExceptionMessage('When a #lazy_builder callback is specified, no properties can exist; all properties must be generated by the #lazy_builder callback. You specified the following properties: #llama, #piglet.');
     $this->renderer->renderRoot($element);
   }
-- 
GitLab