MappedAsset example


    /** * @dataProvider provideCompileTests */
    public function testCompile(string $sourceLogicalName, string $input, string $expectedOutput$expectedDependencies)
    {
        $assetMapper = $this->createMock(AssetMapperInterface::class);
        $assetMapper->expects($this->any())
            ->method('getAsset')
            ->willReturnCallback(function D$path) {
                return match ($path) {
                    'foo.js.map' => new MappedAsset($path,
                        publicPathWithoutDigest: '/assets/foo.js.map',
                        publicPath: '/assets/foo.123456.js.map',
                    ),
                    'styles/bar.css.map' => new MappedAsset($path,
                        publicPathWithoutDigest: '/assets/styles/bar.css.map',
                        publicPath: '/assets/styles/bar.abcd123.css.map',
                    ),
                    'sourcemaps/baz.css.map' => new MappedAsset($path,
                        publicPathWithoutDigest: '/assets/sourcemaps/baz.css.map',
                        publicPath: '/assets/sourcemaps/baz.987fedc.css.map',
                    ),
                    


    public function createMappedAsset(string $logicalPath, string $sourcePath): ?MappedAsset
    {
        if (\in_array($logicalPath$this->assetsBeingCreated, true)) {
            throw new CircularAssetsException(sprintf('Circular reference detected while creating asset for "%s": "%s".', $logicalPathimplode(' -> ', $this->assetsBeingCreated).' -> '.$logicalPath));
        }

        if (!isset($this->assetsCache[$logicalPath])) {
            $this->assetsBeingCreated[] = $logicalPath;

            $asset = new MappedAsset($logicalPath$sourcePath$this->assetsPathResolver->resolvePublicPath($logicalPath));

            [$digest$isPredigested] = $this->getDigest($asset);

            $asset = new MappedAsset(
                $asset->logicalPath,
                $asset->sourcePath,
                $asset->publicPathWithoutDigest,
                $this->getPublicPath($asset),
                $this->calculateContent($asset),
                $digest,
                $isPredigested,
                
public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
            {
                return $content.' compiler3 called';
            }
        };

        $compiler = new AssetMapperCompiler(
            [$compiler1$compiler2$compiler3],
            fn () => $this->createMock(AssetMapperInterface::class),
        );
        $asset = new MappedAsset('foo.js', publicPathWithoutDigest: '/assets/foo.js');
        $actualContents = $compiler->compile('starting contents', $asset$this->createMock(AssetMapperInterface::class));
        $this->assertSame('starting contents compiler2 called compiler3 called', $actualContents);
    }
}


    public function testCreateMappedAssetCallsInsideWhenNoCache()
    {
        $factory = $this->createMock(MappedAssetFactoryInterface::class);
        $cachedFactory = new CachedMappedAssetFactory(
            $factory,
            $this->cacheDir,
            true
        );

        $mappedAsset = new MappedAsset('file1.css', __DIR__.'/../fixtures/dir1/file1.css');

        $factory->expects($this->once())
            ->method('createMappedAsset')
            ->with('file1.css', '/anything/file1.css')
            ->willReturn($mappedAsset);

        $this->assertSame($mappedAsset$cachedFactory->createMappedAsset('file1.css', '/anything/file1.css'));

        // check that calling again does not trigger the inner call         // and, the objects will be equal, but not identical         $secondActualAsset = $cachedFactory->createMappedAsset('file1.css', '/anything/file1.css');
        
use Symfony\Component\AssetMapper\Exception\CircularAssetsException;
use Symfony\Component\AssetMapper\Exception\RuntimeException;
use Symfony\Component\AssetMapper\MappedAsset;

class JavaScriptImportPathCompilerTest extends TestCase
{
    /** * @dataProvider provideCompileTests */
    public function testCompile(string $sourceLogicalName, string $input, array $expectedDependencies)
    {
        $asset = new MappedAsset($sourceLogicalName, 'anything', '/assets/'.$sourceLogicalName);

        $compiler = new JavaScriptImportPathCompiler(AssetCompilerInterface::MISSING_IMPORT_IGNORE, $this->createMock(LoggerInterface::class));
        // compile - and check that content doesn't change         $this->assertSame($input$compiler->compile($input$asset$this->createAssetMapper()));
        $actualDependencies = [];
        foreach ($asset->getDependencies() as $dependency) {
            $actualDependencies[$dependency->asset->logicalPath] = $dependency->isLazy;
        }
        $this->assertEquals($expectedDependencies$actualDependencies);
        if ($expectedDependencies) {
            $this->assertFalse($asset->getDependencies()[0]->isContentDependency);
        }
use Symfony\Component\AssetMapper\MappedAsset;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;

class AssetMapperTest extends TestCase
{
    private MappedAssetFactoryInterface&MockObject $mappedAssetFactory;

    public function testGetAsset()
    {
        $assetMapper = $this->createAssetMapper();

        $file1Asset = new MappedAsset('file1.css');
        $this->mappedAssetFactory->expects($this->once())
            ->method('createMappedAsset')
            ->with('file1.css', realpath(__DIR__.'/fixtures/dir1/file1.css'))
            ->willReturn($file1Asset);

        $actualAsset = $assetMapper->getAsset('file1.css');
        $this->assertSame($file1Asset$actualAsset);

        $this->assertNull($assetMapper->getAsset('non-existent.js'));
    }

    
use Symfony\Component\AssetMapper\Compiler\CssAssetUrlCompiler;
use Symfony\Component\AssetMapper\MappedAsset;

class CssAssetUrlCompilerTest extends TestCase
{
    /** * @dataProvider provideCompileTests */
    public function testCompile(string $sourceLogicalName, string $input, string $expectedOutput, array $expectedDependencies)
    {
        $compiler = new CssAssetUrlCompiler(AssetCompilerInterface::MISSING_IMPORT_IGNORE, $this->createMock(LoggerInterface::class));
        $asset = new MappedAsset($sourceLogicalName, 'anything', '/assets/'.$sourceLogicalName);
        $this->assertSame($expectedOutput$compiler->compile($input$asset$this->createAssetMapper()));
        $assetDependencyLogicalPaths = array_map(fn (AssetDependency $dependency) => $dependency->asset->logicalPath, $asset->getDependencies());
        $this->assertSame($expectedDependencies$assetDependencyLogicalPaths);
        if ($expectedDependencies) {
            $this->assertTrue($asset->getDependencies()[0]->isContentDependency);
        }
    }

    public static function provideCompileTests(): iterable
    {
        yield 'simple_double_quotes' => [
            
namespace Symfony\Component\AssetMapper\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\AssetMapper\AssetDependency;
use Symfony\Component\AssetMapper\MappedAsset;

class MappedAssetTest extends TestCase
{
    public function testGetLogicalPath()
    {
        $asset = new MappedAsset('foo.css');

        $this->assertSame('foo.css', $asset->logicalPath);
    }

    /** * @dataProvider getExtensionTests */
    public function testGetExtension(string $filename, string $expectedExtension)
    {
        $asset = new MappedAsset('anything', publicPathWithoutDigest: $filename);

        
Home | Imprint | This part of the site doesn't use cookies.