LibraryTypeKernelTestBase.php in Libraries API 8.3
File
tests/src/Kernel/LibraryTypeKernelTestBase.php
View source
<?php
namespace Drupal\Tests\libraries\Kernel;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\KernelTests\KernelTestBase;
use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
use Drupal\libraries\ExternalLibrary\Exception\LibraryTypeNotFoundException;
use Drupal\libraries\ExternalLibrary\LibraryInterface;
use Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface;
abstract class LibraryTypeKernelTestBase extends KernelTestBase {
protected $libraryManager;
protected $libraryTypeFactory;
protected $modulePath;
public static $modules = [
'libraries',
'libraries_test',
];
protected abstract function getLibraryTypeId();
protected function setUp() {
parent::setUp();
$module_handler = $this->container
->get('module_handler');
$this->modulePath = $module_handler
->getModule('libraries')
->getPath();
$this
->installConfig('libraries');
$config_factory = $this->container
->get('config.factory');
$config_factory
->getEditable('libraries.settings')
->set('definition.local.path', "{$this->modulePath}/tests/library_definitions")
->set('definition.remote.enable', FALSE)
->save();
$kernel = $this->container
->get('kernel');
$this->container = $kernel
->rebuildContainer();
$this->libraryManager = $this->container
->get('libraries.manager');
$this->libraryTypeFactory = $this->container
->get('plugin.manager.libraries.library_type');
}
public function testLibraryType() {
$type_id = $this
->getLibraryTypeId();
try {
$this->libraryTypeFactory
->createInstance($type_id);
$this
->assertTrue(TRUE, "Library type '{$type_id}' can be instantiated.");
} catch (PluginException $exception) {
$this
->fail("Library type '{$type_id}' cannot be instantiated.");
}
}
public function testLibrary() {
$type_id = $this
->getLibraryTypeId();
$id = $this
->getLibraryId();
try {
$library = $this->libraryManager
->getLibrary($id);
$this
->assertTrue(TRUE, "Test {$type_id} library can be instantiated.");
$this
->assertInstanceOf($this
->getLibraryType()
->getLibraryClass(), $library);
$this
->assertEquals($this
->getLibraryId(), $library
->getId());
} catch (LibraryDefinitionNotFoundException $exception) {
$this
->fail("Missing library definition for test {$type_id} library.");
} catch (LibraryTypeNotFoundException $exception) {
$this
->fail("Missing library type declaration for test {$type_id} library.");
}
}
protected function getLibraryType() {
try {
$library_type = $this->libraryTypeFactory
->createInstance($this
->getLibraryTypeId());
} catch (PluginException $exception) {
$library_type = $this
->prophesize(LibraryTypeInterface::class)
->reveal();
} finally {
return $library_type;
}
}
protected function getLibraryId() {
$type_id = $this
->getLibraryTypeId();
return "test_{$type_id}_library";
}
protected function getLibrary() {
try {
$library = $this->libraryManager
->getLibrary($this
->getLibraryId());
} catch (LibraryDefinitionNotFoundException $exception) {
$library = $this
->prophesize(LibraryInterface::class)
->reveal();
} catch (LibraryTypeNotFoundException $exception) {
$library = $this
->prophesize(LibraryInterface::class)
->reveal();
} catch (PluginException $exception) {
$library = $this
->prophesize(LibraryInterface::class)
->reveal();
} finally {
return $library;
}
}
}