Skip to content

[AssetMapper] use constants in MappedAssetFactoryTest #59057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@

class MappedAssetFactoryTest extends TestCase
{
private const DEFAULT_FIXTURES = __DIR__.'/../Fixtures/assets/vendor';
private const FIXTURES_DIR = __DIR__.'/../Fixtures';
private const VENDOR_FIXTURES_DIR = self::FIXTURES_DIR.'/assets/vendor';

private AssetMapperInterface&MockObject $assetMapper;

public function testCreateMappedAsset()
{
$factory = $this->createFactory();

$asset = $factory->createMappedAsset('file2.js', __DIR__.'/../Fixtures/dir1/file2.js');
$asset = $factory->createMappedAsset('file2.js', self::FIXTURES_DIR.'/dir1/file2.js');
$this->assertSame('file2.js', $asset->logicalPath);
$this->assertMatchesRegularExpression('/^\/final-assets\/file2-[a-zA-Z0-9]{7,128}\.js$/', $asset->publicPath);
$this->assertSame('/final-assets/file2.js', $asset->publicPathWithoutDigest);
Expand All @@ -43,7 +44,7 @@ public function testCreateMappedAsset()
public function testCreateMappedAssetRespectsPreDigestedPaths()
{
$assetMapper = $this->createFactory();
$asset = $assetMapper->createMappedAsset('already-abcdefVWXYZ0123456789.digested.css', __DIR__.'/../Fixtures/dir2/already-abcdefVWXYZ0123456789.digested.css');
$asset = $assetMapper->createMappedAsset('already-abcdefVWXYZ0123456789.digested.css', self::FIXTURES_DIR.'/dir2/already-abcdefVWXYZ0123456789.digested.css');
$this->assertSame('already-abcdefVWXYZ0123456789.digested.css', $asset->logicalPath);
$this->assertSame('/final-assets/already-abcdefVWXYZ0123456789.digested.css', $asset->publicPath);
// for pre-digested files, the digest *is* part of the public path
Expand All @@ -67,18 +68,18 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
$assetMapper = $this->createFactory($file1Compiler);
$expected = 'totally changed';

$asset = $assetMapper->createMappedAsset('file1.css', __DIR__.'/../Fixtures/dir1/file1.css');
$asset = $assetMapper->createMappedAsset('file1.css', self::FIXTURES_DIR.'/dir1/file1.css');
$this->assertSame($expected, $asset->content);

// verify internal caching doesn't cause issues
$asset = $assetMapper->createMappedAsset('file1.css', __DIR__.'/../Fixtures/dir1/file1.css');
$asset = $assetMapper->createMappedAsset('file1.css', self::FIXTURES_DIR.'/dir1/file1.css');
$this->assertSame($expected, $asset->content);
}

public function testCreateMappedAssetWithContentThatDoesNotChange()
{
$assetMapper = $this->createFactory();
$asset = $assetMapper->createMappedAsset('file1.css', __DIR__.'/../Fixtures/dir1/file1.css');
$asset = $assetMapper->createMappedAsset('file1.css', self::FIXTURES_DIR.'/dir1/file1.css');
// null content because the final content matches the file source
$this->assertNull($asset->content);
}
Expand All @@ -89,7 +90,7 @@ public function testCreateMappedAssetWithContentErrorsOnCircularReferences()

$this->expectException(CircularAssetsException::class);
$this->expectExceptionMessage('Circular reference detected while creating asset for "circular1.css": "circular1.css -> circular2.css -> circular1.css".');
$factory->createMappedAsset('circular1.css', __DIR__.'/../Fixtures/circular_dir/circular1.css');
$factory->createMappedAsset('circular1.css', self::FIXTURES_DIR.'/circular_dir/circular1.css');
}

public function testCreateMappedAssetWithDigest()
Expand All @@ -111,43 +112,43 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
};

$factory = $this->createFactory();
$asset = $factory->createMappedAsset('subdir/file6.js', __DIR__.'/../Fixtures/dir2/subdir/file6.js');
$asset = $factory->createMappedAsset('subdir/file6.js', self::FIXTURES_DIR.'/dir2/subdir/file6.js');
$this->assertSame('7f983f4053a57f07551fed6099c0da4e', $asset->digest);
$this->assertFalse($asset->isPredigested);

// trigger the compiler, which will change file5.js
// since file6.js imports file5.js, the digest for file6 should change,
// because, internally, the file path in file6.js to file5.js will need to change
$factory = $this->createFactory($file6Compiler);
$asset = $factory->createMappedAsset('subdir/file6.js', __DIR__.'/../Fixtures/dir2/subdir/file6.js');
$asset = $factory->createMappedAsset('subdir/file6.js', self::FIXTURES_DIR.'/dir2/subdir/file6.js');
$this->assertSame('7e4f24ebddd4ab2a3bcf0d89270b9f30', $asset->digest);
}

public function testCreateMappedAssetWithPredigested()
{
$assetMapper = $this->createFactory();
$asset = $assetMapper->createMappedAsset('already-abcdefVWXYZ0123456789.digested.css', __DIR__.'/../Fixtures/dir2/already-abcdefVWXYZ0123456789.digested.css');
$asset = $assetMapper->createMappedAsset('already-abcdefVWXYZ0123456789.digested.css', self::FIXTURES_DIR.'/dir2/already-abcdefVWXYZ0123456789.digested.css');
$this->assertSame('abcdefVWXYZ0123456789.digested', $asset->digest);
$this->assertTrue($asset->isPredigested);
}

public function testCreateMappedAssetInVendor()
{
$assetMapper = $this->createFactory();
$asset = $assetMapper->createMappedAsset('lodash.js', __DIR__.'/../Fixtures/assets/vendor/lodash/lodash.index.js');
$asset = $assetMapper->createMappedAsset('lodash.js', self::VENDOR_FIXTURES_DIR.'/lodash/lodash.index.js');
$this->assertSame('lodash.js', $asset->logicalPath);
$this->assertTrue($asset->isVendor);
}

public function testCreateMappedAssetInMissingVendor()
{
$assetMapper = $this->createFactory(null, '/this-path-does-not-exist/');
$asset = $assetMapper->createMappedAsset('lodash.js', __DIR__.'/../Fixtures/assets/vendor/lodash/lodash.index.js');
$asset = $assetMapper->createMappedAsset('lodash.js', self::VENDOR_FIXTURES_DIR.'/lodash/lodash.index.js');
$this->assertSame('lodash.js', $asset->logicalPath);
$this->assertFalse($asset->isVendor);
}

private function createFactory(?AssetCompilerInterface $extraCompiler = null, ?string $vendorDir = self::DEFAULT_FIXTURES): MappedAssetFactory
private function createFactory(?AssetCompilerInterface $extraCompiler = null, ?string $vendorDir = self::VENDOR_FIXTURES_DIR): MappedAssetFactory
{
$compilers = [
new JavaScriptImportPathCompiler($this->createMock(ImportMapConfigReader::class)),
Expand Down