View source
<?php
namespace Drupal\Tests\components\Unit;
use Drupal\components\Template\Loader\ComponentsLoader;
use Drupal\Tests\UnitTestCase;
class ComponentsLoaderTest extends UnitTestCase {
protected $componentsRegistry;
protected $systemUnderTest;
public function invokeProtectedMethod(?object $obj, string $method, ...$args) {
$methodUnderTest = new \ReflectionMethod($obj, $method);
$methodUnderTest
->setAccessible(TRUE);
return $methodUnderTest
->invokeArgs($obj, $args);
}
public function testFindTemplate(string $name, bool $throw, ?string $getTemplate, ?string $expected, ?string $exception = NULL) {
$componentsRegistry = $this
->createMock('\\Drupal\\components\\Template\\ComponentsRegistry');
$componentsRegistry
->method('getTemplate')
->willReturn($getTemplate);
$this->systemUnderTest = new ComponentsLoader($componentsRegistry);
try {
$result = $this
->invokeProtectedMethod($this->systemUnderTest, 'findTemplate', $name, $throw);
if (!$exception) {
$this
->assertEquals($expected, $result, $this
->getName());
}
} catch (\Exception $e) {
if ($exception) {
$this
->assertEquals($exception, $e
->getMessage(), $this
->getName());
$exception = '';
}
else {
$this
->fail('No exception expected; "' . $e
->getMessage() . '" thrown during: ' . $this
->getName());
}
}
if ($exception) {
$this
->fail('No exception thrown, but "' . $exception . '" was expected during: ' . $this
->getName());
}
}
public function providerTestFindTemplate() : array {
return [
'error when template name has no @' => [
'name' => 'n/template.twig',
'throw' => FALSE,
'getTemplate' => 'not called',
'expected' => NULL,
'exception' => NULL,
],
'error when template name has no namespace' => [
'name' => '@/template.twig',
'throw' => FALSE,
'getTemplate' => 'not called',
'expected' => NULL,
'exception' => NULL,
],
'error when template name does not have an expected extension' => [
'name' => '@ns/template.txt',
'throw' => FALSE,
'getTemplate' => 'not called',
'expected' => NULL,
'exception' => NULL,
],
'exception when invalid template name and $throw = TRUE' => [
'name' => '@ns/template.txt',
'throw' => TRUE,
'getTemplate' => 'not called',
'expected' => '',
'exception' => 'Malformed namespaced template name "@ns/template.txt" (expecting "@namespace/template_name.twig").',
],
'error when template not found' => [
'name' => '@ns/template.twig',
'throw' => FALSE,
'getTemplate' => NULL,
'expected' => NULL,
'exception' => NULL,
],
'exception when template not found and $throw = TRUE' => [
'name' => '@ns/template.twig',
'throw' => TRUE,
'getTemplate' => NULL,
'expected' => NULL,
'exception' => 'Unable to find template "@ns/template.twig" in the components registry.',
],
'template (.twig) found' => [
'name' => '@ns/template.twig',
'throw' => TRUE,
'getTemplate' => 'themes/contrib/example/ns/template.twig',
'expected' => 'themes/contrib/example/ns/template.twig',
'exception' => NULL,
],
'template (.html) found' => [
'name' => '@ns/template.html',
'throw' => TRUE,
'getTemplate' => 'themes/contrib/example/ns/template.html',
'expected' => 'themes/contrib/example/ns/template.html',
'exception' => NULL,
],
'template (.svg) found' => [
'name' => '@ns/icon.svg',
'throw' => TRUE,
'getTemplate' => 'themes/contrib/example/ns/icon.svg',
'expected' => 'themes/contrib/example/ns/icon.svg',
'exception' => NULL,
],
];
}
public function testExists(string $template, ?string $getTemplate, bool $expected) {
$componentsRegistry = $this
->createMock('\\Drupal\\components\\Template\\ComponentsRegistry');
$componentsRegistry
->method('getTemplate')
->willReturn($getTemplate);
$this->systemUnderTest = new ComponentsLoader($componentsRegistry);
$result = $this->systemUnderTest
->exists($template);
$this
->assertEquals($expected, $result, $this
->getName());
}
public function providerTestExists() : array {
return [
'confirms a template does exist' => [
'template' => '@ns/example-exists.twig',
'getTemplate' => 'themes/contrib/example/ns/example-exists.twig',
'expected' => TRUE,
],
'confirms a template does not exists' => [
'template' => '@ns/example-does-not-exist.twig',
'getTemplate' => NULL,
'expected' => FALSE,
],
];
}
}