From cefff381fd2dc2c9063cecefe2d985af5beb1986 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20St=C3=B6ckler?= <tobiasstoeckler@googlemail.com>
Date: Wed, 11 Nov 2015 00:32:07 +0100
Subject: [PATCH] by tstoeckler: Expand PhpFileLibraryTest

---
 .../PhpFile/PhpFileLibrary.php                |  4 +-
 .../PhpFile/PhpFileLibraryInterface.php       |  2 +-
 .../PhpFile/PhpFileLibraryTest.php            | 37 ++++++++++++++++++-
 .../Kernel/ExternalLibraryKernelTestBase.php  | 11 +++++-
 4 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/src/ExternalLibrary/PhpFile/PhpFileLibrary.php b/src/ExternalLibrary/PhpFile/PhpFileLibrary.php
index 576fb86..788f007 100644
--- a/src/ExternalLibrary/PhpFile/PhpFileLibrary.php
+++ b/src/ExternalLibrary/PhpFile/PhpFileLibrary.php
@@ -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;
   }
 
   /**
diff --git a/src/ExternalLibrary/PhpFile/PhpFileLibraryInterface.php b/src/ExternalLibrary/PhpFile/PhpFileLibraryInterface.php
index f149a9f..af7900c 100644
--- a/src/ExternalLibrary/PhpFile/PhpFileLibraryInterface.php
+++ b/src/ExternalLibrary/PhpFile/PhpFileLibraryInterface.php
@@ -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();
diff --git a/tests/src/Kernel/ExternalLibrary/PhpFile/PhpFileLibraryTest.php b/tests/src/Kernel/ExternalLibrary/PhpFile/PhpFileLibraryTest.php
index be98bf5..f257c32 100644
--- a/tests/src/Kernel/ExternalLibrary/PhpFile/PhpFileLibraryTest.php
+++ b/tests/src/Kernel/ExternalLibrary/PhpFile/PhpFileLibraryTest.php
@@ -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.');
diff --git a/tests/src/Kernel/ExternalLibraryKernelTestBase.php b/tests/src/Kernel/ExternalLibraryKernelTestBase.php
index 938214e..7a6b13f 100644
--- a/tests/src/Kernel/ExternalLibraryKernelTestBase.php
+++ b/tests/src/Kernel/ExternalLibraryKernelTestBase.php
@@ -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();
   }
 
-- 
GitLab