You are here

class AssetResolverTest in Zircon Profile 8

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

@coversDefaultClass \Drupal\Core\Asset\AssetResolver @group Asset

Hierarchy

Expanded class hierarchy of AssetResolverTest

File

core/tests/Drupal/Tests/Core/Asset/AssetResolverTest.php, line 20
Contains \Drupal\Tests\Core\Asset\AssetResolverTest.

Namespace

Drupal\Tests\Core\Asset
View source
class AssetResolverTest extends UnitTestCase {

  /**
   * The tested asset resolver service.
   *
   * @var \Drupal\Core\Asset\AssetResolver
   */
  protected $assetResolver;

  /**
   * The mocked library discovery service.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $libraryDiscovery;

  /**
   * The mocked library dependency resolver.
   *
   * @var \Drupal\Core\Asset\LibraryDependencyResolverInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $libraryDependencyResolver;

  /**
   * The mocked module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The mocked theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $themeManager;

  /**
   * The mocked language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $languageManager;

  /**
   * The cache backend to use.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cache;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->libraryDiscovery = $this
      ->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscovery')
      ->disableOriginalConstructor()
      ->getMock();
    $this->libraryDependencyResolver = $this
      ->getMock('\\Drupal\\Core\\Asset\\LibraryDependencyResolverInterface');
    $this->libraryDependencyResolver
      ->expects($this
      ->any())
      ->method('getLibrariesWithDependencies')
      ->willReturnArgument(0);
    $this->moduleHandler = $this
      ->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $this->themeManager = $this
      ->getMock('\\Drupal\\Core\\Theme\\ThemeManagerInterface');
    $active_theme = $this
      ->getMockBuilder('\\Drupal\\Core\\Theme\\ActiveTheme')
      ->disableOriginalConstructor()
      ->getMock();
    $active_theme
      ->expects($this
      ->any())
      ->method('getName')
      ->willReturn('bartik');
    $this->themeManager
      ->expects($this
      ->any())
      ->method('getActiveTheme')
      ->willReturn($active_theme);
    $this->languageManager = $this
      ->getMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
    $english = $this
      ->getMock('\\Drupal\\Core\\Language\\LanguageInterface');
    $english
      ->expects($this
      ->any())
      ->method('getId')
      ->willReturn('en');
    $japanese = $this
      ->getMock('\\Drupal\\Core\\Language\\LanguageInterface');
    $japanese
      ->expects($this
      ->any())
      ->method('getId')
      ->willReturn('jp');
    $this->languageManager = $this
      ->getMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
    $this->languageManager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->will($this
      ->onConsecutiveCalls($english, $english, $japanese, $japanese));
    $this->cache = new TestMemoryBackend('llama');
    $this->assetResolver = new AssetResolver($this->libraryDiscovery, $this->libraryDependencyResolver, $this->moduleHandler, $this->themeManager, $this->languageManager, $this->cache);
  }

  /**
   * @covers ::getCssAssets
   * @dataProvider providerAttachedAssets
   */
  public function testGetCssAssets(AttachedAssetsInterface $assets_a, AttachedAssetsInterface $assets_b, $expected_cache_item_count) {
    $this->assetResolver
      ->getCssAssets($assets_a, FALSE);
    $this->assetResolver
      ->getCssAssets($assets_b, FALSE);
    $this
      ->assertCount($expected_cache_item_count, $this->cache
      ->getAllCids());
  }

  /**
   * @covers ::getJsAssets
   * @dataProvider providerAttachedAssets
   */
  public function testGetJsAssets(AttachedAssetsInterface $assets_a, AttachedAssetsInterface $assets_b, $expected_cache_item_count) {
    $this->assetResolver
      ->getJsAssets($assets_a, FALSE);
    $this->assetResolver
      ->getJsAssets($assets_b, FALSE);
    $this
      ->assertCount($expected_cache_item_count, $this->cache
      ->getAllCids());
    $this->assetResolver
      ->getJsAssets($assets_a, FALSE);
    $this->assetResolver
      ->getJsAssets($assets_b, FALSE);
    $this
      ->assertCount($expected_cache_item_count * 2, $this->cache
      ->getAllCids());
  }
  public function providerAttachedAssets() {
    $time = time();
    return [
      'same libraries, different timestamps' => [
        (new AttachedAssets())
          ->setAlreadyLoadedLibraries([])
          ->setLibraries([
          'core/drupal',
        ])
          ->setSettings([
          'currentTime' => $time,
        ]),
        (new AttachedAssets())
          ->setAlreadyLoadedLibraries([])
          ->setLibraries([
          'core/drupal',
        ])
          ->setSettings([
          'currentTime' => $time + 100,
        ]),
        1,
      ],
      'different libraries, same timestamps' => [
        (new AttachedAssets())
          ->setAlreadyLoadedLibraries([])
          ->setLibraries([
          'core/drupal',
        ])
          ->setSettings([
          'currenttime' => $time,
        ]),
        (new AttachedAssets())
          ->setAlreadyLoadedLibraries([])
          ->setLibraries([
          'core/drupal',
          'core/jquery',
        ])
          ->setSettings([
          'currentTime' => $time,
        ]),
        2,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssetResolverTest::$assetResolver protected property The tested asset resolver service.
AssetResolverTest::$cache protected property The cache backend to use.
AssetResolverTest::$languageManager protected property The mocked language manager.
AssetResolverTest::$libraryDependencyResolver protected property The mocked library dependency resolver.
AssetResolverTest::$libraryDiscovery protected property The mocked library discovery service.
AssetResolverTest::$moduleHandler protected property The mocked module handler.
AssetResolverTest::$themeManager protected property The mocked theme manager.
AssetResolverTest::providerAttachedAssets public function
AssetResolverTest::setUp protected function Overrides UnitTestCase::setUp
AssetResolverTest::testGetCssAssets public function @covers ::getCssAssets @dataProvider providerAttachedAssets
AssetResolverTest::testGetJsAssets public function @covers ::getJsAssets @dataProvider providerAttachedAssets
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.