class LibraryDiscoveryTest in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php \Drupal\Tests\Core\Asset\LibraryDiscoveryTest
@coversDefaultClass \Drupal\Core\Asset\LibraryDiscovery @group Asset
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\Asset\LibraryDiscoveryTest
Expanded class hierarchy of LibraryDiscoveryTest
File
- core/
tests/ Drupal/ Tests/ Core/ Asset/ LibraryDiscoveryTest.php, line 12
Namespace
Drupal\Tests\Core\AssetView source
class LibraryDiscoveryTest extends UnitTestCase {
/**
* The tested library discovery service.
*
* @var \Drupal\Core\Asset\LibraryDiscovery
*/
protected $libraryDiscovery;
/**
* The mocked library discovery cache collector.
*
* @var \Drupal\Core\Cache\CacheCollectorInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $libraryDiscoveryCollector;
/**
* The cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $cacheTagsInvalidator;
/**
* Test library data.
*
* @var array
*/
protected $libraryData = [
'test_1' => [
'js' => [],
'css' => [
'foo.css' => [],
],
],
'test_2' => [
'js' => [
'bar.js' => [],
],
'css' => [],
],
'test_3' => [
'js' => [
'baz.js' => [],
],
'css' => [],
'deprecated' => 'The "%library_id%" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use the test_2 library instead. See https://www.example.com',
],
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->cacheTagsInvalidator = $this
->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
$this->libraryDiscoveryCollector = $this
->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscoveryCollector')
->disableOriginalConstructor()
->getMock();
$this->libraryDiscovery = new LibraryDiscovery($this->libraryDiscoveryCollector, $this->cacheTagsInvalidator);
$this->libraryDiscoveryCollector
->expects($this
->once())
->method('get')
->with('test')
->willReturn($this->libraryData);
}
/**
* @covers ::getLibrariesByExtension
*/
public function testGetLibrariesByExtension() {
$this->libraryDiscovery
->getLibrariesbyExtension('test');
// Verify that subsequent calls don't trigger hook_library_info_alter()
// and hook_js_settings_alter() invocations, nor do they talk to the
// collector again. This ensures that the alterations made by
// hook_library_info_alter() and hook_js_settings_alter() implementations
// are statically cached, as desired.
$this->libraryDiscovery
->getLibraryByName('test', 'test_1');
$this->libraryDiscovery
->getLibrariesbyExtension('test');
}
/**
* Tests getting a library by name.
*
* @covers ::getLibraryByName
*/
public function testGetLibraryByName() {
$this
->assertSame($this->libraryData['test_1'], $this->libraryDiscovery
->getLibraryByName('test', 'test_1'));
}
/**
* Tests getting a deprecated library.
*/
public function testAssetLibraryDeprecation() {
$previous_error_handler = set_error_handler(function ($severity, $message, $file, $line, $context) use (&$previous_error_handler) {
// Convert deprecation error into a catchable exception.
if ($severity === E_USER_DEPRECATED) {
throw new \ErrorException($message, 0, $severity, $file, $line);
}
if ($previous_error_handler) {
return $previous_error_handler($severity, $message, $file, $line, $context);
}
});
try {
$this->libraryDiscovery
->getLibraryByName('test', 'test_3');
$this
->fail('No deprecation error triggered.');
} catch (\ErrorException $e) {
$this
->assertSame('The "test/test_3" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use the test_2 library instead. See https://www.example.com', $e
->getMessage());
}
restore_error_handler();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LibraryDiscoveryTest:: |
protected | property | The cache tags invalidator. | |
LibraryDiscoveryTest:: |
protected | property | Test library data. | |
LibraryDiscoveryTest:: |
protected | property | The tested library discovery service. | |
LibraryDiscoveryTest:: |
protected | property | The mocked library discovery cache collector. | |
LibraryDiscoveryTest:: |
protected | function |
Overrides UnitTestCase:: |
|
LibraryDiscoveryTest:: |
public | function | Tests getting a deprecated library. | |
LibraryDiscoveryTest:: |
public | function | @covers ::getLibrariesByExtension | |
LibraryDiscoveryTest:: |
public | function | Tests getting a library by name. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
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 | Mocks a block with a block plugin. | 1 |
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. |