class LibraryDiscoveryTest

Tests Drupal\Core\Asset\LibraryDiscovery.

Attributes

#[CoversClass(LibraryDiscovery::class)] #[Group('Asset')]

Hierarchy

Expanded class hierarchy of LibraryDiscoveryTest

File

core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php, line 15

Namespace

Drupal\Tests\Core\Asset
View source
class LibraryDiscoveryTest extends UnitTestCase {
  
  /**
   * The tested library discovery service.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryCollector
   */
  protected $libraryDiscovery;
  
  /**
   * 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() : void {
    parent::setUp();
    $this->cacheTagsInvalidator = $this->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
    $this->libraryDiscovery = $this->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscoveryCollector')
      ->onlyMethods([
      'resolveCacheMiss',
      'getLibrariesByExtension',
    ])
      ->disableOriginalConstructor()
      ->getMock();
    $this->libraryDiscovery
      ->expects($this->any())
      ->method('resolveCacheMiss')
      ->with('test')
      ->willReturn($this->libraryData);
    $this->libraryDiscovery
      ->expects($this->any())
      ->method('getLibrariesByExtension')
      ->with('test')
      ->willReturn($this->libraryData);
  }
  
  /**
   * Tests getting a library by name.
   *
   * @legacy-covers ::getLibraryByName
   */
  public function testGetLibraryByName() : void {
    $this->assertSame($this->libraryData['test_1'], $this->libraryDiscovery
      ->getLibraryByName('test', 'test_1'));
  }
  
  /**
   * Tests getting a deprecated library.
   */
  public function testAssetLibraryDeprecation() : void {
    $previous_error_handler = get_error_handler();
    set_error_handler(function ($severity, $message, $file, $line) 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);
      }
    });
    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

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
LibraryDiscoveryTest::$cacheTagsInvalidator protected property The cache tags invalidator.
LibraryDiscoveryTest::$libraryData protected property Test library data.
LibraryDiscoveryTest::$libraryDiscovery protected property The tested library discovery service.
LibraryDiscoveryTest::setUp protected function Overrides UnitTestCase::setUp
LibraryDiscoveryTest::testAssetLibraryDeprecation public function Tests getting a deprecated library.
LibraryDiscoveryTest::testGetLibraryByName public function Tests getting a library by name.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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