You are here

class LibraryDiscoveryTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php \Drupal\Tests\Core\Asset\LibraryDiscoveryTest

@coversDefaultClass \Drupal\Core\Asset\LibraryDiscovery @group Asset

Hierarchy

Expanded class hierarchy of LibraryDiscoveryTest

File

core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php, line 17
Contains \Drupal\Tests\Core\Asset\LibraryDiscoveryTest.

Namespace

Drupal\Tests\Core\Asset
View 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' => [],
    ],
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->cacheTagsInvalidator = $this
      ->getMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
    $this->libraryDiscoveryCollector = $this
      ->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscoveryCollector')
      ->disableOriginalConstructor()
      ->getMock();
    $this->libraryDiscovery = new LibraryDiscovery($this->libraryDiscoveryCollector, $this->cacheTagsInvalidator);
  }

  /**
   * @covers ::getLibrariesByExtension
   */
  public function testGetLibrariesByExtension() {
    $this->libraryDiscoveryCollector
      ->expects($this
      ->once())
      ->method('get')
      ->with('test')
      ->willReturn($this->libraryData);
    $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');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::$libraryDiscoveryCollector protected property The mocked library discovery cache collector.
LibraryDiscoveryTest::setUp protected function Overrides UnitTestCase::setUp
LibraryDiscoveryTest::testGetLibrariesByExtension public function @covers ::getLibrariesByExtension
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.