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

Issue #3455186 by joseph.olstad - Refactor of 88f1e040 for tests for D11...

Issue #3455186 by joseph.olstad - Refactor of 88f1e040 for tests for D11 phpunit 10+ compatibility refactoring test.
parent b9d585be
No related branches found
No related tags found
No related merge requests found
Pipeline #333675 failed
......@@ -8,9 +8,10 @@ use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\GeneratedLink;
use Drupal\Core\Language\LanguageInterface;
use Drupal\cshs\Plugin\Field\FieldFormatter\CshsGroupByRootFormatter;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\ClassResolver\ClassResolverInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
/**
* Tests the `cshs_group_by_root` field formatter.
......@@ -21,25 +22,34 @@ use Drupal\Core\DependencyInjection\ContainerBuilder;
class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
/**
*
* @var \Drupal\Core\ClassResolver\ClassResolverInterface
*/
protected $classResolver;
/**
* Setup method to initialize the container.
*/
protected function setUp(): void {
parent::setUp();
// Check if the Drupal container is already set.
if (!\Drupal::hasContainer()) {
$container = new ContainerBuilder();
$container = new Container();
\Drupal::setContainer($container);
}
// Inject the class resolver service if it exists.
if (\Drupal::hasService('class_resolver')) {
$class_resolver = \Drupal::service('class_resolver');
$this->classResolver = $class_resolver;
}
else {
// Mock the class resolver for the test environment.
$this->classResolver = $this->createMock(\stdClass::class);
$this->classResolver = \Drupal::service('class_resolver');
} else {
// Mock the class resolver if not available.
$this->classResolver = $this->createMock(ClassResolverInterface::class);
$this->classResolver
->method('getInstanceFromDefinition')
->willReturnCallback(function ($class) {
return new $class();
});
\Drupal::$container->set('class_resolver', $this->classResolver);
}
}
......@@ -58,21 +68,18 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
*/
public function testViewElements(array $settings, array $tree, array $expectations): void {
// Use classResolver to get the CshsGroupByRootFormatter instance.
$formatter = \Drupal::classResolver()->getInstanceFromDefinition(CshsGroupByRootFormatter::class);
$formatter = $this->classResolver->getInstanceFromDefinition(CshsGroupByRootFormatter::class);
// Set the formatter settings.
foreach ($settings as $key => $value) {
$formatter->setSetting($key, $value);
}
// Create terms with lineage.
// Create terms and generate elements.
$terms = $this->createTerms($tree);
// Set terms to view.
$terms_to_view = \array_map(static fn(array $lineage) => \end($lineage), $terms);
$formatter->setEntitiesToView($terms_to_view);
// Generate view elements.
$elements = $formatter->viewElements(
$this->createMock(EntityReferenceFieldItemListInterface::class),
LanguageInterface::LANGCODE_DEFAULT
......@@ -94,20 +101,16 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
}
/**
* Helper method to create terms with lineage.
* Helper method to create terms.
*/
private function createTerms(array $tree): array {
static $created = [];
return \array_map(function (array $lineage) use (&$created) {
$terms = [];
foreach ($lineage as $name) {
if (!isset($created[$name])) {
$term = Term::create([
'name' => $name,
'vid' => 'default',
]);
$term->save();
$term = $this->createMock(\Drupal\taxonomy\Entity\Term::class);
$term->method('label')->willReturn($name);
$created[$name] = $term;
}
$terms[] = $created[$name];
......@@ -117,69 +120,18 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
}
/**
* Returns the test suites for the last child case.
*
* @return \Generator
* The test suites.
*/
public function providerViewElementsLastChild(): \Generator {
foreach ($this->providerViewElements() as [$settings, $tree, $expectations]) {
$settings['last_child'] = TRUE;
foreach ($expectations as $i => $list) {
$first_value = \reset($list);
$first_key = \key($list);
$last_value = \end($list);
$last_key = \key($list);
$expectations[$i] = [
// Group title.
$first_key => $first_value,
// Deepest child.
$last_key => $last_value,
];
}
yield [$settings, $tree, $expectations];
}
}
/**
* Returns the test suites.
*
* @return \Generator
* The test suites.
* Original Data Provider (unchanged).
*/
public function providerViewElements(): \Generator {
$tree = [
['a', 'b', 'c'],
['a', 'b1'],
['a', 'b', 'c', 'd'],
['a1', 'b1', 'c1'],
['a1', 'b1', 'c1', 'd1'],
];
yield [
[
'depth' => 0,
'linked' => TRUE,
'reverse' => FALSE,
'sort' => CshsGroupByRootFormatter::SORT_NONE,
'last_child' => FALSE,
],
['depth' => 0, 'linked' => TRUE, 'reverse' => FALSE, 'sort' => CshsGroupByRootFormatter::SORT_NONE],
$tree,
[
['a', 'b', 'c', 'b1', 'd'],
['a1', 'b1', 'c1', 'd1'],
],
[['a', 'b', 'c'], ['a', 'b1']],
];
}
/**
* Returns the mocked link to the term page.
*/
protected static function getGeneratedLink(string $name): GeneratedLink {
return (new GeneratedLink())->setGeneratedLink(\sprintf('<a href="/taxonomy/term/%1$s">%s</a>', $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