You are here

class LibraryDependencyResolverTest in Zircon Profile 8

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

@coversDefaultClass \Drupal\Core\Asset\LibraryDependencyResolver @group Asset

Hierarchy

Expanded class hierarchy of LibraryDependencyResolverTest

File

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

Namespace

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

  /**
   * The tested library dependency resolver.
   *
   * @var \Drupal\Core\Asset\LibraryDependencyResolver
   */
  protected $libraryDependencyResolver;

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

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

  /**
   * Test library data.
   *
   * @var array
   */
  protected $libraryData = array(
    'no_deps_a' => [
      'js' => [],
      'css' => [],
    ],
    'no_deps_b' => [
      'js' => [],
      'css' => [],
    ],
    'no_deps_c' => [
      'js' => [],
      'css' => [],
    ],
    'deps_a' => [
      'js' => [],
      'css' => [],
      'dependencies' => [
        'test/no_deps_a',
      ],
    ],
    'deps_b' => [
      'js' => [],
      'css' => [],
      'dependencies' => [
        'test/no_deps_a',
        'test/no_deps_b',
      ],
    ],
    'deps_c' => [
      'js' => [],
      'css' => [],
      'dependencies' => [
        'test/no_deps_b',
        'test/no_deps_a',
      ],
    ],
    'nested_deps_a' => [
      'js' => [],
      'css' => [],
      'dependencies' => [
        'test/deps_a',
      ],
    ],
    'nested_deps_b' => [
      'js' => [],
      'css' => [],
      'dependencies' => [
        'test/nested_deps_a',
      ],
    ],
    'nested_deps_c' => [
      'js' => [],
      'css' => [],
      'dependencies' => [
        'test/nested_deps_b',
      ],
    ],
  );

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->libraryDiscovery = $this
      ->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscovery')
      ->disableOriginalConstructor()
      ->setMethods([
      'getLibrariesByExtension',
    ])
      ->getMock();
    $this->libraryDiscovery
      ->expects($this
      ->any())
      ->method('getLibrariesByExtension')
      ->with('test')
      ->will($this
      ->returnValue($this->libraryData));
    $this->libraryDependencyResolver = new LibraryDependencyResolver($this->libraryDiscovery);
  }

  /**
   * Provides test data for ::testGetLibrariesWithDependencies().
   */
  public function providerTestGetLibrariesWithDependencies() {
    return [
      // Empty list of libraries.
      [
        [],
        [],
      ],
      // Without dependencies.
      [
        [
          'test/no_deps_a',
        ],
        [
          'test/no_deps_a',
        ],
      ],
      [
        [
          'test/no_deps_a',
          'test/no_deps_b',
        ],
        [
          'test/no_deps_a',
          'test/no_deps_b',
        ],
      ],
      [
        [
          'test/no_deps_b',
          'test/no_deps_a',
        ],
        [
          'test/no_deps_b',
          'test/no_deps_a',
        ],
      ],
      // Single-level (direct) dependencies.
      [
        [
          'test/deps_a',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
        ],
      ],
      [
        [
          'test/deps_b',
        ],
        [
          'test/no_deps_a',
          'test/no_deps_b',
          'test/deps_b',
        ],
      ],
      [
        [
          'test/deps_c',
        ],
        [
          'test/no_deps_b',
          'test/no_deps_a',
          'test/deps_c',
        ],
      ],
      [
        [
          'test/deps_a',
          'test/deps_b',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/no_deps_b',
          'test/deps_b',
        ],
      ],
      [
        [
          'test/deps_a',
          'test/deps_c',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/no_deps_b',
          'test/deps_c',
        ],
      ],
      [
        [
          'test/deps_a',
          'test/deps_b',
          'test/deps_c',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/no_deps_b',
          'test/deps_b',
          'test/deps_c',
        ],
      ],
      [
        [
          'test/deps_b',
          'test/deps_a',
        ],
        [
          'test/no_deps_a',
          'test/no_deps_b',
          'test/deps_b',
          'test/deps_a',
        ],
      ],
      [
        [
          'test/deps_b',
          'test/deps_c',
        ],
        [
          'test/no_deps_a',
          'test/no_deps_b',
          'test/deps_b',
          'test/deps_c',
        ],
      ],
      [
        [
          'test/deps_c',
          'test/deps_b',
        ],
        [
          'test/no_deps_b',
          'test/no_deps_a',
          'test/deps_c',
          'test/deps_b',
        ],
      ],
      // Multi-level (indirect) dependencies.
      [
        [
          'test/nested_deps_a',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
        ],
      ],
      [
        [
          'test/nested_deps_b',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/nested_deps_c',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/nested_deps_a',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/nested_deps_a',
          'test/nested_deps_c',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_c',
          'test/nested_deps_a',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_a',
          'test/nested_deps_c',
          'test/nested_deps_b',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/nested_deps_a',
          'test/nested_deps_c',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/nested_deps_c',
          'test/nested_deps_a',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_c',
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_c',
          'test/nested_deps_b',
          'test/nested_deps_a',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
      ],
      // Complex dependencies, combining the above, with many intersections.
      [
        [
          'test/deps_c',
          'test/nested_deps_b',
        ],
        [
          'test/no_deps_b',
          'test/no_deps_a',
          'test/deps_c',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/no_deps_a',
          'test/deps_c',
          'test/nested_deps_b',
        ],
        [
          'test/no_deps_a',
          'test/no_deps_b',
          'test/deps_c',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/deps_c',
          'test/no_deps_c',
        ],
        [
          'test/no_deps_a',
          'test/deps_a',
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/no_deps_b',
          'test/deps_c',
          'test/no_deps_c',
        ],
      ],
    ];
  }

  /**
   * @covers ::getLibrariesWithDependencies
   *
   * @dataProvider providerTestGetLibrariesWithDependencies
   */
  public function testGetLibrariesWithDependencies(array $libraries, array $expected) {
    $this
      ->assertEquals($expected, $this->libraryDependencyResolver
      ->getLibrariesWithDependencies($libraries));
  }

  /**
   * Provides test data for ::testGetMinimalRepresentativeSubset().
   */
  public function providerTestGetMinimalRepresentativeSubset() {
    return [
      // Empty list of libraries.
      [
        [],
        [],
      ],
      // Without dependencies.
      [
        [
          'test/no_deps_a',
        ],
        [
          'test/no_deps_a',
        ],
      ],
      [
        [
          'test/no_deps_a',
          'test/no_deps_b',
        ],
        [
          'test/no_deps_a',
          'test/no_deps_b',
        ],
      ],
      [
        [
          'test/no_deps_b',
          'test/no_deps_a',
        ],
        [
          'test/no_deps_b',
          'test/no_deps_a',
        ],
      ],
      // Single-level (direct) dependencies.
      [
        [
          'test/deps_a',
        ],
        [
          'test/deps_a',
        ],
      ],
      [
        [
          'test/deps_b',
        ],
        [
          'test/deps_b',
        ],
      ],
      [
        [
          'test/deps_c',
        ],
        [
          'test/deps_c',
        ],
      ],
      [
        [
          'test/deps_a',
          'test/deps_b',
        ],
        [
          'test/deps_a',
          'test/deps_b',
        ],
      ],
      [
        [
          'test/deps_a',
          'test/deps_c',
        ],
        [
          'test/deps_a',
          'test/deps_c',
        ],
      ],
      [
        [
          'test/deps_a',
          'test/deps_b',
          'test/deps_c',
        ],
        [
          'test/deps_a',
          'test/deps_b',
          'test/deps_c',
        ],
      ],
      [
        [
          'test/deps_b',
          'test/deps_a',
        ],
        [
          'test/deps_b',
          'test/deps_a',
        ],
      ],
      [
        [
          'test/deps_b',
          'test/deps_c',
        ],
        [
          'test/deps_b',
          'test/deps_c',
        ],
      ],
      [
        [
          'test/deps_c',
          'test/deps_b',
        ],
        [
          'test/deps_c',
          'test/deps_b',
        ],
      ],
      // Multi-level (indirect) dependencies.
      [
        [
          'test/nested_deps_a',
        ],
        [
          'test/nested_deps_a',
        ],
      ],
      [
        [
          'test/nested_deps_b',
        ],
        [
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/nested_deps_c',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
        [
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/nested_deps_a',
        ],
        [
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/nested_deps_a',
          'test/nested_deps_c',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_c',
          'test/nested_deps_a',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_a',
          'test/nested_deps_b',
          'test/nested_deps_c',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_a',
          'test/nested_deps_c',
          'test/nested_deps_b',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/nested_deps_a',
          'test/nested_deps_c',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/nested_deps_c',
          'test/nested_deps_a',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_c',
          'test/nested_deps_a',
          'test/nested_deps_b',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      [
        [
          'test/nested_deps_c',
          'test/nested_deps_b',
          'test/nested_deps_a',
        ],
        [
          'test/nested_deps_c',
        ],
      ],
      // Complex dependencies, combining the above, with many intersections.
      [
        [
          'test/deps_c',
          'test/nested_deps_b',
        ],
        [
          'test/deps_c',
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/no_deps_a',
          'test/deps_c',
          'test/nested_deps_b',
        ],
        [
          'test/deps_c',
          'test/nested_deps_b',
        ],
      ],
      [
        [
          'test/nested_deps_b',
          'test/deps_c',
          'test/no_deps_c',
        ],
        [
          'test/nested_deps_b',
          'test/deps_c',
          'test/no_deps_c',
        ],
      ],
    ];
  }

  /**
   * @covers ::getMinimalRepresentativeSubset
   *
   * @dataProvider providerTestGetMinimalRepresentativeSubset
   */
  public function testGetMinimalRepresentativeSubset(array $libraries, array $expected) {
    $this
      ->assertEquals($expected, $this->libraryDependencyResolver
      ->getMinimalRepresentativeSubset($libraries));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LibraryDependencyResolverTest::$libraryData protected property Test library data.
LibraryDependencyResolverTest::$libraryDependencyResolver protected property The tested library dependency resolver.
LibraryDependencyResolverTest::$libraryDiscovery protected property The mocked library discovery service.
LibraryDependencyResolverTest::$moduleHandler protected property The mocked module handler.
LibraryDependencyResolverTest::providerTestGetLibrariesWithDependencies public function Provides test data for ::testGetLibrariesWithDependencies().
LibraryDependencyResolverTest::providerTestGetMinimalRepresentativeSubset public function Provides test data for ::testGetMinimalRepresentativeSubset().
LibraryDependencyResolverTest::setUp protected function Overrides UnitTestCase::setUp
LibraryDependencyResolverTest::testGetLibrariesWithDependencies public function @covers ::getLibrariesWithDependencies
LibraryDependencyResolverTest::testGetMinimalRepresentativeSubset public function @covers ::getMinimalRepresentativeSubset
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.