3 namespace Drupal\Tests\Core\Asset;
5 use Drupal\Core\Asset\LibraryDiscoveryCollector;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\Core\Asset\LibraryDiscoveryCollector
13 class LibraryDiscoveryCollectorTest extends UnitTestCase {
16 * The mock cache backend.
18 * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
23 * The mock lock backend.
25 * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
30 * The mock library discovery parser.
32 * @var \Drupal\Core\Asset\LibraryDiscoveryParser|\PHPUnit_Framework_MockObject_MockObject
34 protected $libraryDiscoveryParser;
37 * The library discovery collector under test.
39 * @var \Drupal\Core\Asset\LibraryDiscoveryCollector
41 protected $libraryDiscoveryCollector;
44 * The mocked theme manager.
46 * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
48 protected $themeManager;
55 protected $libraryData = [
66 protected $activeTheme;
71 protected function setUp() {
72 $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
73 $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
74 $this->themeManager = $this->getMockBuilder('Drupal\Core\Theme\ThemeManagerInterface')
75 ->disableOriginalConstructor()
77 $this->libraryDiscoveryParser = $this->getMockBuilder('Drupal\Core\Asset\LibraryDiscoveryParser')
78 ->disableOriginalConstructor()
84 * Tests the resolve cache miss function.
86 * @covers ::resolveCacheMiss
88 public function testResolveCacheMiss() {
89 $this->activeTheme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme')
90 ->disableOriginalConstructor()
92 $this->themeManager->expects($this->exactly(3))
93 ->method('getActiveTheme')
94 ->willReturn($this->activeTheme);
95 $this->activeTheme->expects($this->once())
97 ->willReturn('kitten_theme');
98 $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
100 $this->libraryDiscoveryParser->expects($this->once())
101 ->method('buildByExtension')
103 ->willReturn($this->libraryData);
105 $this->assertSame($this->libraryData, $this->libraryDiscoveryCollector->get('test'));
106 $this->assertSame($this->libraryData, $this->libraryDiscoveryCollector->get('test'));
110 * Tests the destruct method.
114 public function testDestruct() {
115 $this->activeTheme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme')
116 ->disableOriginalConstructor()
118 $this->themeManager->expects($this->exactly(3))
119 ->method('getActiveTheme')
120 ->willReturn($this->activeTheme);
121 $this->activeTheme->expects($this->once())
123 ->willReturn('kitten_theme');
124 $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
126 $this->libraryDiscoveryParser->expects($this->once())
127 ->method('buildByExtension')
129 ->willReturn($this->libraryData);
131 $lock_key = 'library_info:kitten_theme:Drupal\Core\Cache\CacheCollector';
133 $this->lock->expects($this->once())
136 ->will($this->returnValue(TRUE));
137 $this->cache->expects($this->exactly(2))
139 ->with('library_info:kitten_theme')
141 $this->cache->expects($this->once())
143 ->with('library_info:kitten_theme', ['test' => $this->libraryData], Cache::PERMANENT, ['library_info']);
144 $this->lock->expects($this->once())
148 // This should get data and persist the key.
149 $this->libraryDiscoveryCollector->get('test');
150 $this->libraryDiscoveryCollector->destruct();