Skip to content
Snippets Groups Projects
Commit 7e947bc7 authored by Joseph Olstad's avatar Joseph Olstad
Browse files

Issue #3455186 by cmlara, joseph.olstad - Refactor tests for D11 phpunit 10+...

Issue #3455186 by cmlara, joseph.olstad - Refactor tests for D11 phpunit 10+ compatibility refactoring test.
parent f85ca4b8
No related branches found
No related tags found
No related merge requests found
Pipeline #332536 failed
......@@ -20,6 +20,9 @@ use Drupal\Tests\UnitTestCase;
*/
class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
// Define parent relationships as a class property.
protected array $parentRelationships = [];
/**
* Test the `viewElements()` logic.
*
......@@ -51,12 +54,12 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
foreach ($terms_to_view as $index => $term) {
$expected_parents = array_reverse($terms[$index]);
// Adjust `loadAllParents` method to handle missing relationships.
// Adjust `loadAllParents` to use the class property for parent relationships.
$term_storage->method('loadAllParents')
->willReturnCallback(function ($term_id) use ($parentRelationships) {
$parents = [];
while (isset($parentRelationships[$term_id])) {
$parent = $parentRelationships[$term_id];
->willReturnCallback(function ($term_id) {
$parents = [];
while (isset($this->parentRelationships[$term_id])) {
$parent = $this->parentRelationships[$term_id];
if ($parent === NULL) {
break;
}
......@@ -81,7 +84,6 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
$group_title = (string) array_shift($expected_terms);
$group = current($elements);
// Use array_values to make sure both arrays are numerically indexed.
$this->assertSame(
(string) $group['#title'],
$group_title,
......@@ -96,7 +98,6 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
next($elements);
}
}
/**
......@@ -110,36 +111,35 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
*/
protected function createTerms(array $tree): array {
$created = [];
$parentRelationships = [];
// Create mock terms based on the hierarchy tree.
return array_map(function (array $lineage) use (&$created, &$parentRelationships): array {
$terms = [];
return array_map(function (array $lineage) use (&$created): array {
$terms = [];
foreach ($lineage as $name) {
if (!isset($created[$name])) {
$link = $this->createMock(Link::class);
$link->method('toString')->willReturn(static::getGeneratedLink($name));
$link = $this->createMock(Link::class);
$link->method('toString')->willReturn(static::getGeneratedLink($name));
$term = $this->createMock(TermInterface::class);
$term->method('id')->willReturn($this->getFixedTermId($name));
$term->method('label')->willReturn($name);
$term->method('toLink')->willReturn($link);
$term = $this->createMock(TermInterface::class);
$term->method('id')->willReturn($this->getFixedTermId($name));
$term->method('label')->willReturn($name);
$term->method('toLink')->willReturn($link);
$created[$name] = $term;
$created[$name] = $term;
}
$terms[] = $created[$name];
$terms[] = $created[$name];
}
// Establish parent relationships in reverse lineage.
$previous = NULL;
// Establish parent relationships in reverse lineage.
$previous = NULL;
foreach (array_reverse($terms) as $term) {
if ($previous) {
$parentRelationships[$previous->id()] = $term;
$this->parentRelationships[$previous->id()] = $term;
}
$previous = $term;
$previous = $term;
}
return $terms;
return $terms;
}, $tree);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment