Skip to content
Snippets Groups Projects
Commit c3b8e964 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 2132cc3e
No related branches found
No related tags found
No related merge requests found
Pipeline #332601 failed
......@@ -91,6 +91,33 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
}
}
/**
* Creates a partial mock term based on the provided parameters.
*
* @param int $id
* The term ID.
* @param string $label
* The term label.
* @param string|null $parent
* The parent term ID (if any).
*
* @return \Drupal\taxonomy\TermInterface|\PHPUnit\Framework\MockObject\MockObject
* The mocked term object.
*/
protected function createMockTerm(int $id, string $label, Link $link, $parent = NULL): MockObject {
$term = $this->createMock(TermInterface::class);
// Mock only the methods you need.
$term->method('id')->willReturn($id);
$term->method('label')->willReturn($label);
$term->method('toLink')->willReturn($link);
// Set the parent property dynamically if needed.
$term->parent = $parent;
return $term;
}
/**
* Creates mock terms based on a hierarchy tree and assigns parent IDs.
*
......@@ -111,49 +138,11 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
$link = $this->createMock(Link::class);
$link->method('toString')->willReturn(static::getGeneratedLink($name));
// Use an anonymous class to handle dynamic properties with the correct method signatures and Traversable interface.
$term = new class() implements TermInterface, \IteratorAggregate {
public $parent;
public $target_id;
public $label;
public $link;
/**
*
*/
public function id() {
return $this->target_id;
}
/**
*
*/
public function label() {
return $this->label;
}
/**
*
*/
public function toLink($text = NULL, $rel = 'canonical', array $options = []) {
return $this->link;
}
/**
* Implement the getIterator() method for IteratorAggregate.
*/
public function getIterator() {
return new \ArrayIterator($this);
}
};
$term->target_id = $this->getFixedTermId($name);
$term->label = $name;
$term->link = $link;
// Explicitly set the parent property.
$term->parent = NULL;
// Create a mock term using the `createMockTerm` method.
$term_id = $this->getFixedTermId($name);
$term = $this->createMockTerm($term_id, $name, $link);
// Store the created term.
$created[$name] = $term;
}
$terms[] = $created[$name];
......
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