Skip to content
Snippets Groups Projects
Commit 9c78d482 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 58922d9d
No related branches found
No related tags found
No related merge requests found
Pipeline #332552 failed
......@@ -51,24 +51,20 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
$term_storage = $this->createMock(TermStorageInterface::class);
foreach ($terms_to_view as $index => $term) {
$expected_parents = array_reverse($terms[$index]);
// 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];
if (!$parent) {
break;
}
$parents[] = $parent;
$term_id = $parent->id();
// Use class property `$this->parentRelationships` in the callback
$term_storage->method('loadAllParents')
->willReturnCallback(function ($term_id) {
$parents = [];
while (isset($this->parentRelationships[$term_id])) {
$parent = $this->parentRelationships[$term_id];
if (!$parent) {
break;
}
return array_reverse($parents);
});
}
$parents[] = $parent;
$term_id = $parent->id();
}
return array_reverse($parents);
});
$mock->method('getTermStorage')->willReturn($term_storage);
$mock->method('getTranslationFromContext')->willReturnArgument(0);
......@@ -78,24 +74,15 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
LanguageInterface::LANGCODE_DEFAULT,
);
$this->assertCount(count($expectations), $elements, "Mismatch in expected element count.");
// Enhanced assertion and debugging as before
$this->assertCount(count($expectations), $elements);
foreach ($expectations as $expected_terms) {
$group_title = (string) array_shift($expected_terms);
$group = current($elements);
// Use array_values to enforce numeric indexing for better comparison.
$this->assertSame(
$group_title,
(string) $group['#title'],
"Mismatch in group title for expected terms."
);
$this->assertSame((string) $group['#title'], $group_title);
$actual_labels = array_map(static fn($term) => (string) $term['label'], $group['#terms']);
$this->assertSame(
array_values(array_map('strval', $expected_terms)),
array_values($actual_labels),
"Mismatch in term labels between expected and actual."
);
$this->assertSame(array_values($actual_labels), array_values(array_map('strval', $expected_terms)));
next($elements);
}
......@@ -112,41 +99,34 @@ 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];
}
$previous = NULL;
// Set up parent relationships using the class property
$previous = NULL;
foreach (array_reverse($terms) as $term) {
if ($previous) {
$parentRelationships[$previous->id()] = $term;
$this->parentRelationships[$previous->id()] = $term;
}
$previous = $term;
}
// Debug output of relationships for each term.
echo "Parent relationships:\n";
foreach ($parentRelationships as $child_id => $parent) {
echo "Child ID: $child_id, Parent ID: " . ($parent->id() ?? 'None') . "\n";
$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