class HelpTopicTwigLoaderTest in Drupal 9
Same name and namespace in other branches
- 8 core/modules/help_topics/tests/src/Unit/HelpTopicTwigLoaderTest.php \Drupal\Tests\help_topics\Unit\HelpTopicTwigLoaderTest
Unit test for the HelpTopicTwigLoader class.
@coversDefaultClass \Drupal\help_topics\HelpTopicTwigLoader @group help_topics
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\help_topics\Unit\HelpTopicTwigLoaderTest
Expanded class hierarchy of HelpTopicTwigLoaderTest
File
- core/
modules/ help_topics/ tests/ src/ Unit/ HelpTopicTwigLoaderTest.php, line 16
Namespace
Drupal\Tests\help_topics\UnitView source
class HelpTopicTwigLoaderTest extends UnitTestCase {
/**
* The help topic loader instance to test.
*
* @var \Drupal\help_topics\HelpTopicTwigLoader
*/
protected $helpLoader;
/**
* The virtual directories to use in testing.
*
* @var array
*/
protected $directories;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
$this
->setUpVfs();
$this->helpLoader = new HelpTopicTwigLoader('\\fake\\root\\path', $this
->getHandlerMock('module'), $this
->getHandlerMock('theme'));
}
/**
* @covers ::__construct
*/
public function testConstructor() {
// Verify that the module/theme directories were added in the constructor,
// and non-existent directories were omitted.
$paths = $this->helpLoader
->getPaths(HelpTopicTwigLoader::MAIN_NAMESPACE);
$this
->assertCount(2, $paths);
$this
->assertContains($this->directories['module']['test'] . '/help_topics', $paths);
$this
->assertContains($this->directories['theme']['test'] . '/help_topics', $paths);
}
/**
* @covers ::getSourceContext
*/
public function testGetSourceContext() {
$source = $this->helpLoader
->getSourceContext('@' . HelpTopicTwigLoader::MAIN_NAMESPACE . '/test.topic.html.twig');
$this
->assertEquals('{% line 4 %}<h2>Test</h2>', $source
->getCode());
}
/**
* @covers ::getSourceContext
*/
public function testGetSourceContextException() {
$this
->expectException(LoaderError::class);
$this
->expectExceptionMessage("Malformed YAML in help topic \"vfs://root/modules/test/help_topics/test.invalid_yaml.html.twig\":");
$this->helpLoader
->getSourceContext('@' . HelpTopicTwigLoader::MAIN_NAMESPACE . '/test.invalid_yaml.html.twig');
}
/**
* Creates a mock module or theme handler class for the test.
*
* @param string $type
* Type of handler to return: 'module' or 'theme'.
*
* @return \PHPUnit\Framework\MockObject\MockObject
* The mock of module or theme handler.
*/
protected function getHandlerMock($type) {
if ($type == 'module') {
$class = 'Drupal\\Core\\Extension\\ModuleHandlerInterface';
$method = 'getModuleDirectories';
}
else {
$class = 'Drupal\\Core\\Extension\\ThemeHandlerInterface';
$method = 'getThemeDirectories';
}
$handler = $this
->getMockBuilder($class)
->disableOriginalConstructor()
->getMock();
$handler
->method($method)
->willReturn($this->directories[$type]);
return $handler;
}
/**
* Sets up the virtual file system.
*/
protected function setUpVfs() {
$content = <<<EOF
---
label: Test
---
<h2>Test</h2>
EOF;
$invalid_content = <<<EOF
---
foo : [bar}
---
<h2>Test</h2>
EOF;
$help_topics_dir = [
'help_topics' => [
'test.topic.html.twig' => $content,
'test.invalid_yaml.html.twig' => $invalid_content,
],
];
vfsStream::setup('root');
vfsStream::create([
'modules' => [
'test' => $help_topics_dir,
],
'themes' => [
'test' => $help_topics_dir,
],
]);
$this->directories = [
'root' => vfsStream::url('root'),
'module' => [
'test' => vfsStream::url('root/modules/test'),
'not_a_dir' => vfsStream::url('root/modules/not_a_dir'),
],
'theme' => [
'test' => vfsStream::url('root/themes/test'),
'not_a_dir' => vfsStream::url('root/themes/not_a_dir'),
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HelpTopicTwigLoaderTest:: |
protected | property | The virtual directories to use in testing. | |
HelpTopicTwigLoaderTest:: |
protected | property | The help topic loader instance to test. | |
HelpTopicTwigLoaderTest:: |
protected | function | Creates a mock module or theme handler class for the test. | |
HelpTopicTwigLoaderTest:: |
protected | function |
Overrides UnitTestCase:: |
|
HelpTopicTwigLoaderTest:: |
protected | function | Sets up the virtual file system. | |
HelpTopicTwigLoaderTest:: |
public | function | @covers ::__construct | |
HelpTopicTwigLoaderTest:: |
public | function | @covers ::getSourceContext | |
HelpTopicTwigLoaderTest:: |
public | function | @covers ::getSourceContext | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |