You are here

class CriticalCssProviderTest in Critical CSS 8

Unit tests for CriticalCssProvider.

This unit test is not really useful since there is a more powerful Functional test. Anyway, we maintain it as a reference for future Unit tests.

@coversDefaultClass \Drupal\critical_css\Asset\CriticalCssProvider @group critical_css

Hierarchy

Expanded class hierarchy of CriticalCssProviderTest

File

tests/src/Unit/CriticalCssProviderTest.php, line 27

Namespace

Drupal\Tests\critical_css\Unit
View source
class CriticalCssProviderTest extends UnitTestCase {

  /**
   * The class under test.
   *
   * @var \Drupal\critical_css\Asset\CriticalCssProvider
   */
  protected $criticalCssProvider;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $config_factory = $this
      ->getConfigFactoryStub([
      'critical_css_enabled.settings' => [
        'enabled' => TRUE,
        'enabled_for_logged_in_users' => FALSE,
        'preload_non_critical_css' => FALSE,
        'dir_path' => "/css/critical",
        'excluded_ids' => NULL,
      ],
    ]);
    $module_handler = $this
      ->createConfiguredMock(ModuleHandler::class, [
      'alter' => TRUE,
    ]);
    $request = $this
      ->createConfiguredMock(Request::class, [
      'getPathInfo' => '/node/1',
    ]);
    $request_stack = $this
      ->createConfiguredMock(RequestStack::class, [
      'getCurrentRequest' => $request,
    ]);
    $current_route_match = $this
      ->createConfiguredMock(CurrentRouteMatch::class, [
      'getParameter' => NULL,
    ]);
    $current_path_stack = $this
      ->createConfiguredMock(CurrentPathStack::class, [
      'getPath' => '/article/title',
    ]);
    $current_user = $this
      ->createConfiguredMock(AccountProxy::class, [
      'isAnonymous' => TRUE,
    ]);
    $active_theme = $this
      ->createConfiguredMock(ActiveTheme::class, [
      'getPath' => 'core/themes/bartik',
    ]);
    $theme_manager = $this
      ->createConfiguredMock(ThemeManager::class, [
      'getActiveTheme' => $active_theme,
    ]);
    $admin_context = $this
      ->createConfiguredMock(AdminContext::class, [
      'isAdminRoute' => FALSE,
    ]);
    $this->criticalCssProvider = new CriticalCssProvider($module_handler, $request_stack, $config_factory, $current_route_match, $current_path_stack, $current_user, $theme_manager, $admin_context);
  }

  /**
   * Tests sanitizePath.
   *
   * @covers ::sanitizePath
   * @dataProvider sanitizePathProvider
   *
   * @throws \ReflectionException
   */
  public function testSanitizePath($rawPath, $sanitizedPath) {
    $method = new ReflectionMethod($this->criticalCssProvider, "sanitizePath");
    $method
      ->setAccessible(TRUE);
    $this
      ->assertEquals($method
      ->invoke($this->criticalCssProvider, $rawPath), $sanitizedPath);
  }

  /**
   * Data provider for testSanitizePath.
   */
  public function sanitizePathProvider() {
    return [
      [
        "/node/1",
        "node-1",
      ],
      [
        "/node /1ñá",
        "node-1",
      ],
      [
        "/node-/1",
        "node--1",
      ],
      [
        "/",
        "front",
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CriticalCssProviderTest::$criticalCssProvider protected property The class under test.
CriticalCssProviderTest::sanitizePathProvider public function Data provider for testSanitizePath.
CriticalCssProviderTest::setUp protected function Overrides UnitTestCase::setUp
CriticalCssProviderTest::testSanitizePath public function Tests sanitizePath.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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.