class JsOptimizerUnitTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php \Drupal\Tests\Core\Asset\JsOptimizerUnitTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php \Drupal\Tests\Core\Asset\JsOptimizerUnitTest
  3. 10 core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php \Drupal\Tests\Core\Asset\JsOptimizerUnitTest

Tests the JS asset optimizer.

@group Asset

Hierarchy

Expanded class hierarchy of JsOptimizerUnitTest

File

core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php, line 15

Namespace

Drupal\Tests\Core\Asset
View source
class JsOptimizerUnitTest extends UnitTestCase {
    
    /**
     * A JS asset optimizer.
     *
     * @var \Drupal\Core\Asset\JsOptimizer
     */
    protected $optimizer;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $logger = $this->createMock('\\Psr\\Log\\LoggerInterface');
        $this->optimizer = new JsOptimizer($logger);
    }
    
    /**
     * Provides data for the JS asset cleaning test.
     *
     * @see \Drupal\Core\Asset\JsOptimizer::clean()
     *
     * @return array
     *   An array of test data.
     */
    public static function providerTestClean() {
        $path = dirname(__FILE__) . '/js_test_files/';
        return [
            // File. Tests:
            // - Stripped sourceMappingURL with comment # syntax.
0 => [
                file_get_contents($path . 'source_mapping_url.min.js'),
                file_get_contents($path . 'source_mapping_url.min.js.optimized.js'),
            ],
            // File. Tests:
            // - Stripped sourceMappingURL with comment @ syntax.
1 => [
                file_get_contents($path . 'source_mapping_url_old.min.js'),
                file_get_contents($path . 'source_mapping_url_old.min.js.optimized.js'),
            ],
            // File. Tests:
            // - Stripped sourceURL with comment # syntax.
2 => [
                file_get_contents($path . 'source_url.min.js'),
                file_get_contents($path . 'source_url.min.js.optimized.js'),
            ],
            // File. Tests:
            // - Stripped sourceURL with comment @ syntax.
3 => [
                file_get_contents($path . 'source_url_old.min.js'),
                file_get_contents($path . 'source_url_old.min.js.optimized.js'),
            ],
        ];
    }
    
    /**
     * Tests cleaning of a JS asset group containing 'type' => 'file'.
     *
     * @dataProvider providerTestClean
     */
    public function testClean($js_asset, $expected) : void {
        $this->assertEquals($expected, $this->optimizer
            ->clean($js_asset));
    }
    
    /**
     * Provides data for the JS asset optimize test.
     *
     * @see \Drupal\Core\Asset\JsOptimizer::optimize()
     *
     * @return array
     *   An array of test data.
     */
    public static function providerTestOptimize() {
        $path = dirname(__FILE__) . '/js_test_files/';
        return [
            0 => [
                [
                    'type' => 'file',
                    'preprocess' => TRUE,
                    'data' => $path . 'utf8_bom.js',
                ],
                file_get_contents($path . 'utf8_bom.js.optimized.js'),
            ],
            1 => [
                [
                    'type' => 'file',
                    'preprocess' => TRUE,
                    'data' => $path . 'utf16_bom.js',
                ],
                file_get_contents($path . 'utf16_bom.js.optimized.js'),
            ],
            2 => [
                [
                    'type' => 'file',
                    'preprocess' => TRUE,
                    'data' => $path . 'latin_9.js',
                    'attributes' => [
                        'charset' => 'ISO-8859-15',
                    ],
                ],
                file_get_contents($path . 'latin_9.js.optimized.js'),
            ],
            3 => [
                [
                    'type' => 'file',
                    'preprocess' => TRUE,
                    'data' => $path . 'to_be_minified.js',
                ],
                file_get_contents($path . 'to_be_minified.js.optimized.js'),
            ],
            4 => [
                [
                    'type' => 'file',
                    'preprocess' => TRUE,
                    'data' => $path . 'syntax_error.js',
                ],
                // When there is a syntax error, the 'optimized' contents are the
                // contents of the original file.
file_get_contents($path . 'syntax_error.js'),
            ],
        ];
    }
    
    /**
     * Tests cleaning of a JS asset group containing 'type' => 'file'.
     *
     * @dataProvider providerTestOptimize
     */
    public function testOptimize($js_asset, $expected) : void {
        $this->assertEquals($expected, $this->optimizer
            ->optimize($js_asset));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
JsOptimizerUnitTest::$optimizer protected property A JS asset optimizer.
JsOptimizerUnitTest::providerTestClean public static function Provides data for the JS asset cleaning test.
JsOptimizerUnitTest::providerTestOptimize public static function Provides data for the JS asset optimize test.
JsOptimizerUnitTest::setUp protected function Overrides UnitTestCase::setUp
JsOptimizerUnitTest::testClean public function Tests cleaning of a JS asset group containing 'type' => 'file'.
JsOptimizerUnitTest::testOptimize public function Tests cleaning of a JS asset group containing 'type' => 'file'.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.