class LibraryDiscoveryCollectorTest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryCollectorTest.php \Drupal\Tests\Core\Asset\LibraryDiscoveryCollectorTest
  2. 10 core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryCollectorTest.php \Drupal\Tests\Core\Asset\LibraryDiscoveryCollectorTest
  3. 11.x core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryCollectorTest.php \Drupal\Tests\Core\Asset\LibraryDiscoveryCollectorTest

@coversDefaultClass \Drupal\Core\Asset\LibraryDiscoveryCollector @group Asset

Hierarchy

Expanded class hierarchy of LibraryDiscoveryCollectorTest

File

core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryCollectorTest.php, line 14

Namespace

Drupal\Tests\Core\Asset
View source
class LibraryDiscoveryCollectorTest extends UnitTestCase {
    
    /**
     * The mock cache backend.
     *
     * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $cache;
    
    /**
     * The mock lock backend.
     *
     * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $lock;
    
    /**
     * The mock library discovery parser.
     *
     * @var \Drupal\Core\Asset\LibraryDiscoveryParser|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $libraryDiscoveryParser;
    
    /**
     * The library discovery collector under test.
     *
     * @var \Drupal\Core\Asset\LibraryDiscoveryCollector
     */
    protected $libraryDiscoveryCollector;
    
    /**
     * The mocked theme manager.
     *
     * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $themeManager;
    
    /**
     * Test library data.
     *
     * @var array
     */
    protected $libraryData = [
        'test_1' => [
            'js' => [],
            'css' => [],
        ],
        'test_2' => [
            'js' => [],
            'css' => [],
        ],
        'test_3' => [
            'js' => [],
            'css' => [
                'theme' => [
                    'foo.css' => [],
                ],
            ],
        ],
        'test_4' => [
            'js' => [],
            'css' => [
                'theme' => [
                    'bar.css' => [],
                ],
            ],
            'deprecated' => 'The "%library_id%" asset library is deprecated in drupal:X.0.0 and is removed from drupal:Y.0.0. Use the test_3 library instead. See https://www.example.com',
        ],
    ];
    protected $activeTheme;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        $this->cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
        $this->lock = $this->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
        $this->themeManager = $this->getMockBuilder('Drupal\\Core\\Theme\\ThemeManagerInterface')
            ->disableOriginalConstructor()
            ->getMock();
        $this->libraryDiscoveryParser = $this->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscoveryParser')
            ->disableOriginalConstructor()
            ->getMock();
    }
    
    /**
     * Tests the resolve cache miss function.
     *
     * @covers ::resolveCacheMiss
     */
    public function testResolveCacheMiss() {
        $this->activeTheme = $this->getMockBuilder(ActiveTheme::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->themeManager
            ->expects($this->exactly(5))
            ->method('getActiveTheme')
            ->willReturn($this->activeTheme);
        $this->activeTheme
            ->expects($this->once())
            ->method('getName')
            ->willReturn('kitten_theme');
        $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
        $this->libraryDiscoveryParser
            ->expects($this->once())
            ->method('buildByExtension')
            ->with('test')
            ->willReturn($this->libraryData);
        $this->assertSame($this->libraryData, $this->libraryDiscoveryCollector
            ->get('test'));
        $this->assertSame($this->libraryData, $this->libraryDiscoveryCollector
            ->get('test'));
    }
    
    /**
     * Tests the destruct method.
     *
     * @covers ::destruct
     */
    public function testDestruct() {
        $this->activeTheme = $this->getMockBuilder(ActiveTheme::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->themeManager
            ->expects($this->exactly(5))
            ->method('getActiveTheme')
            ->willReturn($this->activeTheme);
        $this->activeTheme
            ->expects($this->once())
            ->method('getName')
            ->willReturn('kitten_theme');
        $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
        $this->libraryDiscoveryParser
            ->expects($this->once())
            ->method('buildByExtension')
            ->with('test')
            ->willReturn($this->libraryData);
        $lock_key = 'library_info:kitten_theme:Drupal\\Core\\Cache\\CacheCollector';
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with($lock_key)
            ->willReturn(TRUE);
        $this->cache
            ->expects($this->exactly(2))
            ->method('get')
            ->with('library_info:kitten_theme')
            ->willReturn(FALSE);
        $this->cache
            ->expects($this->once())
            ->method('set')
            ->with('library_info:kitten_theme', [
            'test' => $this->libraryData,
        ], Cache::PERMANENT, [
            'library_info',
        ]);
        $this->lock
            ->expects($this->once())
            ->method('release')
            ->with($lock_key);
        // This should get data and persist the key.
        $this->libraryDiscoveryCollector
            ->get('test');
        $this->libraryDiscoveryCollector
            ->destruct();
    }
    
    /**
     * Tests library with an extend.
     *
     * @covers ::applyLibrariesExtend
     */
    public function testLibrariesExtend() {
        $this->activeTheme = $this->getMockBuilder(ActiveTheme::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->themeManager
            ->expects($this->any())
            ->method('getActiveTheme')
            ->willReturn($this->activeTheme);
        $this->activeTheme
            ->expects($this->once())
            ->method('getName')
            ->willReturn('kitten_theme');
        $this->activeTheme
            ->expects($this->atLeastOnce())
            ->method('getLibrariesExtend')
            ->willReturn([
            'test/test_3' => [
                'kitten_theme/extend',
            ],
        ]);
        $this->libraryDiscoveryParser
            ->expects($this->exactly(2))
            ->method('buildByExtension')
            ->willReturnMap([
            [
                'test',
                $this->libraryData,
            ],
            [
                'kitten_theme',
                [
                    'extend' => [
                        'css' => [
                            'theme' => [
                                'baz.css' => [],
                            ],
                        ],
                    ],
                ],
            ],
        ]);
        $library_discovery_collector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
        $libraries = $library_discovery_collector->get('test');
        $this->assertSame([
            'foo.css',
            'baz.css',
        ], array_keys($libraries['test_3']['css']['theme']));
    }
    
    /**
     * Tests a deprecated library with an extend.
     *
     * @covers ::applyLibrariesExtend
     *
     * @group legacy
     */
    public function testLibrariesExtendDeprecated() {
        $this->expectDeprecation('Theme "test" is extending a deprecated library. The "test/test_4" asset library is deprecated in drupal:X.0.0 and is removed from drupal:Y.0.0. Use the test_3 library instead. See https://www.example.com');
        $this->activeTheme = $this->getMockBuilder(ActiveTheme::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->themeManager
            ->expects($this->any())
            ->method('getActiveTheme')
            ->willReturn($this->activeTheme);
        $this->activeTheme
            ->expects($this->once())
            ->method('getName')
            ->willReturn('kitten_theme');
        $this->activeTheme
            ->expects($this->atLeastOnce())
            ->method('getLibrariesExtend')
            ->willReturn([
            'test/test_4' => [
                'kitten_theme/extend',
            ],
        ]);
        $this->libraryDiscoveryParser
            ->expects($this->exactly(2))
            ->method('buildByExtension')
            ->willReturnMap([
            [
                'test',
                $this->libraryData,
            ],
            [
                'kitten_theme',
                [
                    'extend' => [
                        'css' => [
                            'theme' => [
                                'baz.css' => [],
                            ],
                        ],
                    ],
                ],
            ],
        ]);
        $library_discovery_collector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
        $library_discovery_collector->get('test');
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
LibraryDiscoveryCollectorTest::$activeTheme protected property
LibraryDiscoveryCollectorTest::$cache protected property The mock cache backend.
LibraryDiscoveryCollectorTest::$libraryData protected property Test library data.
LibraryDiscoveryCollectorTest::$libraryDiscoveryCollector protected property The library discovery collector under test.
LibraryDiscoveryCollectorTest::$libraryDiscoveryParser protected property The mock library discovery parser.
LibraryDiscoveryCollectorTest::$lock protected property The mock lock backend.
LibraryDiscoveryCollectorTest::$themeManager protected property The mocked theme manager.
LibraryDiscoveryCollectorTest::setUp protected function Overrides UnitTestCase::setUp
LibraryDiscoveryCollectorTest::testDestruct public function Tests the destruct method.
LibraryDiscoveryCollectorTest::testLibrariesExtend public function Tests library with an extend.
LibraryDiscoveryCollectorTest::testLibrariesExtendDeprecated public function Tests a deprecated library with an extend.
LibraryDiscoveryCollectorTest::testResolveCacheMiss public function Tests the resolve cache miss function.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.