Skip to content
Snippets Groups Projects
Commit cefff381 authored by Tobias Zimmermann's avatar Tobias Zimmermann
Browse files

by tstoeckler: Expand PhpFileLibraryTest

parent 9fb27dd7
No related branches found
No related tags found
No related merge requests found
......@@ -70,9 +70,11 @@ class PhpFileLibrary implements PhpFileLibraryInterface {
throw new LibraryNotInstalledException($this);
}
$processed_files = [];
foreach ($this->files as $file) {
yield $this->getLocalPath() . DIRECTORY_SEPARATOR . $file;
$processed_files[] = $this->getLocalPath() . DIRECTORY_SEPARATOR . $file;
}
return $processed_files;
}
/**
......
......@@ -19,7 +19,7 @@ interface PhpFileLibraryInterface extends LocalLibraryInterface {
/**
* Returns the PHP files of this library.
*
* @return string[]|\Generator
* @return string[]
* An array of absolute file paths of PHP files.
*/
public function getPhpFiles();
......
......@@ -7,6 +7,10 @@
namespace Drupal\Tests\libraries\Kernel\ExternalLibrary\PhpFile;
use Drupal\Core\Asset\Exception\LibraryDefinitionMissingLicenseException;
use Drupal\libraries\ExternalLibrary\Exception\LibraryClassNotFoundException;
use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
use Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLibrary;
use Drupal\Tests\libraries\Kernel\ExternalLibraryKernelTestBase;
/**
......@@ -28,6 +32,13 @@ class PhpFileLibraryTest extends ExternalLibraryKernelTestBase {
*/
protected $externalLibraryManager;
/**
* The external library registry.
*
* @var \Drupal\libraries\ExternalLibrary\Registry\ExternalLibraryRegistryInterface
*/
protected $externalLibraryRegistry;
/**
* {@inheritdoc}
*/
......@@ -35,18 +46,40 @@ class PhpFileLibraryTest extends ExternalLibraryKernelTestBase {
parent::setUp();
$this->externalLibraryManager = $this->container->get('libraries.manager');
$this->externalLibraryRegistry = $this->container->get('libraries.registry');
$this->container->set('stream_wrapper.php_library_files', new TestPhpLibraryFilesStream());
}
/**
* Tests that the external library manager properly loads PHP file libraries.
* Tests that library metadata is correctly gathered.
*/
public function testMetadata() {
try {
/** @var \Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLibrary $library */
$library = $this->externalLibraryRegistry->getLibrary('test_php_file_library');
$this->assertInstanceOf(PhpFileLibrary::class, $library);
$this->assertEquals('test_php_file_library', $library->getId());
$expected = [$this->modulePath . DIRECTORY_SEPARATOR . 'tests/libraries/test_php_file_library/test_php_file_library.php'];
$this->assertEquals($expected, $library->getPhpFiles());
}
catch (LibraryClassNotFoundException $exception) {
$this->fail();
}
catch (LibraryDefinitionNotFoundException $exception) {
$this->fail();
}
}
/**
* Tests that the external library manager properly loads PHP files.
*
* @see \Drupal\libraries\ExternalLibrary\ExternalLibraryManager
* @see \Drupal\libraries\ExternalLibrary\ExternalLibraryTrait
* @see \Drupal\libraries\ExternalLibrary\PhpFile\PhpRequireLoader
*/
public function testPhpFileLibrary() {
public function testFileLoading() {
$function_name = '_libraries_test_php_function';
if (function_exists($function_name)) {
$this->markTestSkipped('Cannot test file inclusion if the file to be included has already been included prior.');
......
......@@ -18,6 +18,13 @@ use Drupal\Core\Site\Settings;
*/
abstract class ExternalLibraryKernelTestBase extends CoreKernelTestBase {
/**
* The absolute path to the Libraries API module.
*
* @var string
*/
protected $modulePath;
/**
* {@inheritdoc}
*/
......@@ -27,13 +34,13 @@ abstract class ExternalLibraryKernelTestBase extends CoreKernelTestBase {
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
$root = $this->container->get('app.root');
$module_handler = $this->container->get('module_handler');
$module_path = $module_handler->getModule('libraries')->getPath();
$this->modulePath = "$root/" . $module_handler->getModule('libraries')->getPath();
$this->installConfig('libraries');
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = $this->container->get('config.factory');
$config_factory->getEditable('libraries.settings')
->set('library_definitions.local.path', "$root/$module_path/tests/library_definitions")
->set('library_definitions.local.path', "{$this->modulePath}/tests/library_definitions")
->save();
}
......
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