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

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

Issue #3455186 by joseph.olstad - Refactor tests for D11 phpunit 10+ compatibility refactoring test.
parent 62786c6d
No related branches found
No related tags found
No related merge requests found
Pipeline #333312 passed with warnings
......@@ -36,89 +36,83 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
*/
public function testViewElements(array $settings, array $tree, array $expectations): void {
/**
*
*/
public function testViewElements(array $settings, array $tree, array $expectations): void {
$mock = $this->getMockBuilder(CshsGroupByRootFormatter::class)
->disableOriginalConstructor()
->onlyMethods(['getSetting', 'getTermStorage', 'getEntitiesToView', 'getTranslationFromContext'])
->getMock();
// Mock getSetting to return values based on $settings.
$mock->method('getSetting')
->willReturnCallback(function ($name) use ($settings) {
$mock = $this->getMockBuilder(CshsGroupByRootFormatter::class)
->disableOriginalConstructor()
->onlyMethods(['getSetting', 'getTermStorage', 'getEntitiesToView', 'getTranslationFromContext'])
->getMock();
// Mock getSetting to return values based on $settings.
$mock->method('getSetting')
->willReturnCallback(function ($name) use ($settings) {
return $settings[$name] ?? NULL;
});
// Create terms with lineage.
$terms = \array_map(function (array $lineage) {
static $created = [];
$terms = [];
foreach ($lineage as $name) {
if (!isset($created[$name])) {
$link = $this->createMock(Link::class);
$link->method('toString')->willReturn(static::getGeneratedLink($name));
$created[$name] = $this->createMock(TermInterface::class);
$created[$name]->method('id')->willReturn(\random_int(1, 10000));
$created[$name]->method('label')->willReturn($name);
$created[$name]->method('toLink')->willReturn($link);
}
$terms[] = $created[$name];
});
// Create terms with lineage.
$terms = \array_map(function (array $lineage) {
static $created = [];
$terms = [];
foreach ($lineage as $name) {
if (!isset($created[$name])) {
$link = $this->createMock(Link::class);
$link->method('toString')->willReturn(static::getGeneratedLink($name));
$created[$name] = $this->createMock(TermInterface::class);
$created[$name]->method('id')->willReturn(\random_int(1, 10000));
$created[$name]->method('label')->willReturn($name);
$created[$name]->method('toLink')->willReturn($link);
}
// Assign parents in reverse order.
$reverse = \array_reverse($lineage);
foreach ($reverse as $name) {
$parent_item = $this->createMock(EntityReferenceFieldItemListInterface::class);
$parent_item->target_id = ($parent = \next($reverse)) ? $created[$parent]->id() : NULL;
$created[$name]->parent = $parent_item;
}
$terms[] = $created[$name];
}
// Assign parents in reverse order.
$reverse = \array_reverse($lineage);
foreach ($reverse as $name) {
$parent_item = $this->createMock(EntityReferenceFieldItemListInterface::class);
$parent_item->target_id = ($parent = \next($reverse)) ? $created[$parent]->id() : NULL;
$created[$name]->parent = $parent_item;
}
return $terms;
}, $tree);
return $terms;
}, $tree);
// Set terms to view.
$terms_to_view = \array_map(static fn (array $lineage) => \end($lineage), $terms);
$mock->method('getEntitiesToView')->willReturn($terms_to_view);
// Create term storage mock and use callback for loadAllParents.
$term_storage = $this->createMock(TermStorageInterface::class);
$term_storage->method('loadAllParents')
->willReturnCallback(function ($term_id) use ($terms_to_view, $terms) {
foreach ($terms_to_view as $index => $term) {
if ($term->id() === $term_id) {
return array_reverse($terms[$index]);
}
// Set terms to view.
$terms_to_view = \array_map(static fn (array $lineage) => \end($lineage), $terms);
$mock->method('getEntitiesToView')->willReturn($terms_to_view);
// Create term storage mock and use callback for loadAllParents.
$term_storage = $this->createMock(TermStorageInterface::class);
$term_storage->method('loadAllParents')
->willReturnCallback(function ($term_id) use ($terms_to_view, $terms) {
foreach ($terms_to_view as $index => $term) {
if ($term->id() === $term_id) {
return array_reverse($terms[$index]);
}
}
return [];
});
});
$mock->method('getTermStorage')->willReturn($term_storage);
$mock->method('getTranslationFromContext')->willReturnArgument(0);
$mock->method('getTermStorage')->willReturn($term_storage);
$mock->method('getTranslationFromContext')->willReturnArgument(0);
$elements = $mock->viewElements(
$elements = $mock->viewElements(
$this->createMock(EntityReferenceFieldItemListInterface::class),
LanguageInterface::LANGCODE_DEFAULT
);
static::assertCount(\count($expectations), $elements);
foreach ($expectations as $children) {
$group_title = (string) \array_shift($children);
$group = \current($elements);
static::assertSame((string) $group['#title'], $group_title, $group_title);
static::assertSame(
\array_map('strval', $children),
\array_map('strval', \array_column($group['#terms'], 'label')),
$group_title
);
\next($elements);
}
}
static::assertCount(\count($expectations), $elements);
foreach ($expectations as $children) {
$group_title = (string) \array_shift($children);
$group = \current($elements);
static::assertSame((string) $group['#title'], $group_title, $group_title);
static::assertSame(
\array_map('strval', $children),
\array_map('strval', \array_column($group['#terms'], 'label')),
$group_title
);
\next($elements);
}
}
/**
......
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