Skip to content
Snippets Groups Projects
Commit c5e530dd 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 7f5f0f8a
No related branches found
No related tags found
No related merge requests found
Pipeline #332581 failed
......@@ -20,7 +20,9 @@ use Drupal\Tests\UnitTestCase;
*/
class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
// Define parent relationships as a class property.
/**
* Define parent relationships as a class property.
*/
protected array $parentRelationships = [];
/**
......@@ -51,17 +53,17 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
$term_storage = $this->createMock(TermStorageInterface::class);
// Use class property `$this->parentRelationships` in the callback
// 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;
$parent = $this->parentRelationships[$term_id];
if ($parent === NULL || !property_exists($parent, 'target_id')) {
break;
}
$parents[] = $parent;
$term_id = $parent->id();
$parents[] = $parent;
$term_id = $parent->target_id;
}
return array_reverse($parents);
});
......@@ -75,7 +77,7 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
);
var_dump($elements);
// Enhanced assertion and debugging as before
// Enhanced assertion and debugging as before.
$this->assertCount(count($expectations), $elements);
foreach ($expectations as $expected_terms) {
$group_title = (string) array_shift($expected_terms);
......@@ -100,7 +102,6 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
*/
protected function createTerms(array $tree): array {
$created = [];
// Reset parent relationships before each test
$this->parentRelationships = [];
return array_map(function (array $lineage) use (&$created): array {
......@@ -110,13 +111,38 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
$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);
// Explicitly set up the `parent` property to avoid warnings
// Assign a default `parent` value
// Use an anonymous class to handle dynamic properties.
$term = new class() implements TermInterface {
public $parent;
public $target_id;
/**
*
*/
public function id() {
return $this->target_id;
}
/**
*
*/
public function label() {
return $this->label;
}
/**
*
*/
public function toLink() {
return $this->link;
}
};
$term->target_id = $this->getFixedTermId($name);
$term->label = $name;
$term->link = $link;
// Explicitly set the parent property.
$term->parent = NULL;
$created[$name] = $term;
......@@ -124,12 +150,11 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
$terms[] = $created[$name];
}
// Establish parent relationships in reverse lineage
// Establish parent relationships.
$previous = NULL;
foreach (array_reverse($terms) as $term) {
if ($previous) {
$this->parentRelationships[$previous->id()] = $term;
// Explicitly set `parent` property
$previous->parent = $term;
}
$previous = $term;
......
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