/
githubmirror
/
symfony
Обзор
Документация
Войти
/
githubmirror
/
symfony
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
8.2
src/Symfony/Component/AssetMapper/Tests/AssetMapperTest.php
133 строки
5 KB
Simon André
[AssetMapper] Ignore compiled assets in debug mode
10 июл 2026, 02:43
10 июл 2026, 02:43
1cf0f01
Код
Авторство
О чём код?
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\AssetMapper\Tests; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\AssetMapper\AssetMapper; use Symfony\Component\AssetMapper\AssetMapperRepository; use Symfony\Component\AssetMapper\CompiledAssetMapperConfigReader; use Symfony\Component\AssetMapper\Factory\MappedAssetFactoryInterface; use Symfony\Component\AssetMapper\MappedAsset; use Symfony\Component\Filesystem\Filesystem; 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')); } public function testGetPublicPath() { $assetMapper = $this->createAssetMapper(); $file1Asset = new MappedAsset('file1.css', publicPath: '/final-assets/file1-the-checksum.css'); $this->mappedAssetFactory->expects($this->once()) ->method('createMappedAsset') ->willReturn($file1Asset); $this->assertSame('/final-assets/file1-the-checksum.css', $assetMapper->getPublicPath('file1.css')); // check the manifest is used $this->assertSame('/final-assets/file4.checksumfrommanifest.js', $assetMapper->getPublicPath('file4.js')); } public function testCompiledManifestIsIgnoredInDebugMode() { $repository = new AssetMapperRepository(['dir1' => '', 'dir2' => '', 'dir3' => ''], __DIR__.'/Fixtures'); $filesystem = new Filesystem(); $writableRoot = __DIR__.'/Fixtures/debug_compiled_manifest'; $filesystem->dumpFile($writableRoot.'/manifest.json', json_encode(['file1.css' => '/from-manifest/file1.css'])); try { $factory = $this->createStub(MappedAssetFactoryInterface::class); $factory->method('createMappedAsset') ->willReturn(new MappedAsset('file1.css', publicPath: '/dynamically-computed/file1.css')); // debug: true -> the compiled manifest is ignored, the public path is computed dynamically $debugReader = new CompiledAssetMapperConfigReader($writableRoot, true); $assetMapper = new AssetMapper($repository, $factory, $debugReader); $this->assertSame('/dynamically-computed/file1.css', $assetMapper->getPublicPath('file1.css')); } finally { $filesystem->remove($writableRoot); } } public function testAllAssets() { $assetMapper = $this->createAssetMapper(); $this->mappedAssetFactory->expects($this->exactly(8)) ->method('createMappedAsset') ->willReturnCallback(static function (string $logicalPath, string $filePath) { $asset = new MappedAsset($logicalPath, publicPath: '/final-assets/'.$logicalPath); return $asset; }); $assets = $assetMapper->allAssets(); $this->assertIsIterable($assets); $assets = iterator_to_array($assets); $this->assertCount(8, $assets); $this->assertInstanceOf(MappedAsset::class, $assets[0]); } public function testGetAssetFromFilesystemPath() { $assetMapper = $this->createAssetMapper(); $this->mappedAssetFactory->expects($this->once()) ->method('createMappedAsset') ->with('file1.css', realpath(__DIR__.'/Fixtures/dir1/file1.css')) ->willReturn(new MappedAsset('file1.css')); $asset = $assetMapper->getAssetFromSourcePath(__DIR__.'/Fixtures/dir1/file1.css'); $this->assertSame('file1.css', $asset->logicalPath); } private function createAssetMapper(): AssetMapper { $dirs = ['dir1' => '', 'dir2' => '', 'dir3' => '']; $repository = new AssetMapperRepository($dirs, __DIR__.'/Fixtures'); $compiledConfigReader = $this->createStub(CompiledAssetMapperConfigReader::class); $compiledConfigReader ->method('configExists') ->willReturn(true); $compiledConfigReader ->method('loadConfig') ->willReturn(['file4.js' => '/final-assets/file4.checksumfrommanifest.js']); $this->mappedAssetFactory = $this->createMock(MappedAssetFactoryInterface::class); return new AssetMapper( $repository, $this->mappedAssetFactory, $compiledConfigReader, ); } }