View source
<?php
namespace Drupal\Tests\Core\Asset;
use Drupal\Core\Asset\LibraryDependencyResolver;
use Drupal\Tests\UnitTestCase;
class LibraryDependencyResolverTest extends UnitTestCase {
protected $libraryDependencyResolver;
protected $libraryDiscovery;
protected $moduleHandler;
protected $libraryData = [
'no_deps_a' => [
'js' => [],
'css' => [],
],
'no_deps_b' => [
'js' => [],
'css' => [],
],
'no_deps_c' => [
'js' => [],
'css' => [],
],
'deps_a' => [
'js' => [],
'css' => [],
'dependencies' => [
'test/no_deps_a',
],
],
'deps_b' => [
'js' => [],
'css' => [],
'dependencies' => [
'test/no_deps_a',
'test/no_deps_b',
],
],
'deps_c' => [
'js' => [],
'css' => [],
'dependencies' => [
'test/no_deps_b',
'test/no_deps_a',
],
],
'nested_deps_a' => [
'js' => [],
'css' => [],
'dependencies' => [
'test/deps_a',
],
],
'nested_deps_b' => [
'js' => [],
'css' => [],
'dependencies' => [
'test/nested_deps_a',
],
],
'nested_deps_c' => [
'js' => [],
'css' => [],
'dependencies' => [
'test/nested_deps_b',
],
],
];
protected function setUp() : void {
$this->libraryDiscovery = $this
->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscovery')
->disableOriginalConstructor()
->onlyMethods([
'getLibrariesByExtension',
])
->getMock();
$this->libraryDiscovery
->expects($this
->any())
->method('getLibrariesByExtension')
->with('test')
->will($this
->returnValue($this->libraryData));
$this->libraryDependencyResolver = new LibraryDependencyResolver($this->libraryDiscovery);
}
public function providerTestGetLibrariesWithDependencies() {
return [
[
[],
[],
],
[
[
'test/no_deps_a',
],
[
'test/no_deps_a',
],
],
[
[
'test/no_deps_a',
'test/no_deps_b',
],
[
'test/no_deps_a',
'test/no_deps_b',
],
],
[
[
'test/no_deps_b',
'test/no_deps_a',
],
[
'test/no_deps_b',
'test/no_deps_a',
],
],
[
[
'test/deps_a',
],
[
'test/no_deps_a',
'test/deps_a',
],
],
[
[
'test/deps_b',
],
[
'test/no_deps_a',
'test/no_deps_b',
'test/deps_b',
],
],
[
[
'test/deps_c',
],
[
'test/no_deps_b',
'test/no_deps_a',
'test/deps_c',
],
],
[
[
'test/deps_a',
'test/deps_b',
],
[
'test/no_deps_a',
'test/deps_a',
'test/no_deps_b',
'test/deps_b',
],
],
[
[
'test/deps_a',
'test/deps_c',
],
[
'test/no_deps_a',
'test/deps_a',
'test/no_deps_b',
'test/deps_c',
],
],
[
[
'test/deps_a',
'test/deps_b',
'test/deps_c',
],
[
'test/no_deps_a',
'test/deps_a',
'test/no_deps_b',
'test/deps_b',
'test/deps_c',
],
],
[
[
'test/deps_b',
'test/deps_a',
],
[
'test/no_deps_a',
'test/no_deps_b',
'test/deps_b',
'test/deps_a',
],
],
[
[
'test/deps_b',
'test/deps_c',
],
[
'test/no_deps_a',
'test/no_deps_b',
'test/deps_b',
'test/deps_c',
],
],
[
[
'test/deps_c',
'test/deps_b',
],
[
'test/no_deps_b',
'test/no_deps_a',
'test/deps_c',
'test/deps_b',
],
],
[
[
'test/nested_deps_a',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
],
],
[
[
'test/nested_deps_b',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
],
],
[
[
'test/nested_deps_c',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_a',
'test/nested_deps_b',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
],
],
[
[
'test/nested_deps_b',
'test/nested_deps_a',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
],
],
[
[
'test/nested_deps_a',
'test/nested_deps_c',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_b',
'test/nested_deps_c',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_c',
'test/nested_deps_a',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_a',
'test/nested_deps_c',
'test/nested_deps_b',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_b',
'test/nested_deps_a',
'test/nested_deps_c',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_b',
'test/nested_deps_c',
'test/nested_deps_a',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_c',
'test/nested_deps_a',
'test/nested_deps_b',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_c',
'test/nested_deps_b',
'test/nested_deps_a',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
],
[
[
'test/deps_c',
'test/nested_deps_b',
],
[
'test/no_deps_b',
'test/no_deps_a',
'test/deps_c',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
],
],
[
[
'test/no_deps_a',
'test/deps_c',
'test/nested_deps_b',
],
[
'test/no_deps_a',
'test/no_deps_b',
'test/deps_c',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
],
],
[
[
'test/nested_deps_b',
'test/deps_c',
'test/no_deps_c',
],
[
'test/no_deps_a',
'test/deps_a',
'test/nested_deps_a',
'test/nested_deps_b',
'test/no_deps_b',
'test/deps_c',
'test/no_deps_c',
],
],
];
}
public function testGetLibrariesWithDependencies(array $libraries, array $expected) {
$this
->assertEquals($expected, $this->libraryDependencyResolver
->getLibrariesWithDependencies($libraries));
}
public function providerTestGetMinimalRepresentativeSubset() {
return [
[
[],
[],
],
[
[
'test/no_deps_a',
],
[
'test/no_deps_a',
],
],
[
[
'test/no_deps_a',
'test/no_deps_b',
],
[
'test/no_deps_a',
'test/no_deps_b',
],
],
[
[
'test/no_deps_b',
'test/no_deps_a',
],
[
'test/no_deps_b',
'test/no_deps_a',
],
],
[
[
'test/deps_a',
],
[
'test/deps_a',
],
],
[
[
'test/deps_b',
],
[
'test/deps_b',
],
],
[
[
'test/deps_c',
],
[
'test/deps_c',
],
],
[
[
'test/deps_a',
'test/deps_b',
],
[
'test/deps_a',
'test/deps_b',
],
],
[
[
'test/deps_a',
'test/deps_c',
],
[
'test/deps_a',
'test/deps_c',
],
],
[
[
'test/deps_a',
'test/deps_b',
'test/deps_c',
],
[
'test/deps_a',
'test/deps_b',
'test/deps_c',
],
],
[
[
'test/deps_b',
'test/deps_a',
],
[
'test/deps_b',
'test/deps_a',
],
],
[
[
'test/deps_b',
'test/deps_c',
],
[
'test/deps_b',
'test/deps_c',
],
],
[
[
'test/deps_c',
'test/deps_b',
],
[
'test/deps_c',
'test/deps_b',
],
],
[
[
'test/nested_deps_a',
],
[
'test/nested_deps_a',
],
],
[
[
'test/nested_deps_b',
],
[
'test/nested_deps_b',
],
],
[
[
'test/nested_deps_c',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_a',
'test/nested_deps_b',
],
[
'test/nested_deps_b',
],
],
[
[
'test/nested_deps_b',
'test/nested_deps_a',
],
[
'test/nested_deps_b',
],
],
[
[
'test/nested_deps_a',
'test/nested_deps_c',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_b',
'test/nested_deps_c',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_c',
'test/nested_deps_a',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_a',
'test/nested_deps_b',
'test/nested_deps_c',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_a',
'test/nested_deps_c',
'test/nested_deps_b',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_b',
'test/nested_deps_a',
'test/nested_deps_c',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_b',
'test/nested_deps_c',
'test/nested_deps_a',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_c',
'test/nested_deps_a',
'test/nested_deps_b',
],
[
'test/nested_deps_c',
],
],
[
[
'test/nested_deps_c',
'test/nested_deps_b',
'test/nested_deps_a',
],
[
'test/nested_deps_c',
],
],
[
[
'test/deps_c',
'test/nested_deps_b',
],
[
'test/deps_c',
'test/nested_deps_b',
],
],
[
[
'test/no_deps_a',
'test/deps_c',
'test/nested_deps_b',
],
[
'test/deps_c',
'test/nested_deps_b',
],
],
[
[
'test/nested_deps_b',
'test/deps_c',
'test/no_deps_c',
],
[
'test/nested_deps_b',
'test/deps_c',
'test/no_deps_c',
],
],
];
}
public function testGetMinimalRepresentativeSubset(array $libraries, array $expected) {
$this
->assertEquals($expected, $this->libraryDependencyResolver
->getMinimalRepresentativeSubset($libraries));
}
public function testGetMinimalRepresentativeSubsetInvalidInput() {
$this
->expectException(\AssertionError::class);
$this
->expectExceptionMessage('$libraries can\'t contain duplicate items.');
$this->libraryDependencyResolver
->getMinimalRepresentativeSubset([
'test/no_deps_a',
'test/no_deps_a',
]);
}
}