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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\critical_css\Unit\CriticalCssProviderTest
Expanded class hierarchy of CriticalCssProviderTest
File
- tests/
src/ Unit/ CriticalCssProviderTest.php, line 27
Namespace
Drupal\Tests\critical_css\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CriticalCssProviderTest:: |
protected | property | The class under test. | |
CriticalCssProviderTest:: |
public | function | Data provider for testSanitizePath. | |
CriticalCssProviderTest:: |
protected | function |
Overrides UnitTestCase:: |
|
CriticalCssProviderTest:: |
public | function | Tests sanitizePath. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |