Skip to content

Commit 2faff1c

Browse files
minor symfony#57675 [AssetMapper] Split test dirs in tests (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- [AssetMapper] Split test dirs in tests | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Commits ------- 35d6066 [AssetMapper] Split test dirs in tests
2 parents ea760e7 + 35d6066 commit 2faff1c

8 files changed

+57
-47
lines changed

src/Symfony/Component/AssetMapper/Tests/CompiledAssetMapperConfigReaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class CompiledAssetMapperConfigReaderTest extends TestCase
2323
protected function setUp(): void
2424
{
2525
$this->filesystem = new Filesystem();
26-
$this->writableRoot = __DIR__.'/../Fixtures/importmaps_for_writing';
27-
if (!file_exists(__DIR__.'/../Fixtures/importmaps_for_writing')) {
26+
$this->writableRoot = __DIR__.'/../Fixtures/compiled_asset_mapper_config_reader';
27+
if (!file_exists(__DIR__.'/../Fixtures/compiled_asset_mapper_config_reader')) {
2828
$this->filesystem->mkdir($this->writableRoot);
2929
}
3030
// realpath to help path comparisons in the tests

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapConfigReaderTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,14 @@ class ImportMapConfigReaderTest extends TestCase
2626
protected function setUp(): void
2727
{
2828
$this->filesystem = new Filesystem();
29-
if (!file_exists(__DIR__.'/../Fixtures/importmaps_for_writing')) {
30-
$this->filesystem->mkdir(__DIR__.'/../Fixtures/importmaps_for_writing');
31-
}
32-
if (!file_exists(__DIR__.'/../Fixtures/importmaps_for_writing/assets')) {
33-
$this->filesystem->mkdir(__DIR__.'/../Fixtures/importmaps_for_writing/assets');
29+
if (!file_exists(__DIR__.'/../Fixtures/importmap_config_reader/assets')) {
30+
$this->filesystem->mkdir(__DIR__.'/../Fixtures/importmap_config_reader/assets');
3431
}
3532
}
3633

3734
protected function tearDown(): void
3835
{
39-
$this->filesystem->remove(__DIR__.'/../Fixtures/importmaps_for_writing');
36+
$this->filesystem->remove(__DIR__.'/../Fixtures/importmap_config_reader');
4037
}
4138

4239
public function testGetEntriesAndWriteEntries()
@@ -63,7 +60,7 @@ public function testGetEntriesAndWriteEntries()
6360
],
6461
];
6562
EOF;
66-
file_put_contents(__DIR__.'/../Fixtures/importmaps_for_writing/importmap.php', $importMap);
63+
file_put_contents(__DIR__.'/../Fixtures/importmap_config_reader/importmap.php', $importMap);
6764

6865
$remotePackageStorage = $this->createMock(RemotePackageStorage::class);
6966
$remotePackageStorage->expects($this->any())
@@ -72,7 +69,7 @@ public function testGetEntriesAndWriteEntries()
7269
return '/path/to/vendor/'.$packageModuleSpecifier.'.'.$type->value;
7370
});
7471
$reader = new ImportMapConfigReader(
75-
__DIR__.'/../Fixtures/importmaps_for_writing/importmap.php',
72+
__DIR__.'/../Fixtures/importmap_config_reader/importmap.php',
7673
$remotePackageStorage,
7774
);
7875
$entries = $reader->getEntries();
@@ -100,11 +97,11 @@ public function testGetEntriesAndWriteEntries()
10097
$this->assertSame('package/with_file.js', $packageWithFileEntry->packageModuleSpecifier);
10198

10299
// now save the original raw data from importmap.php and delete the file
103-
$originalImportMapData = (static fn () => include __DIR__.'/../Fixtures/importmaps_for_writing/importmap.php')();
104-
unlink(__DIR__.'/../Fixtures/importmaps_for_writing/importmap.php');
100+
$originalImportMapData = (static fn () => eval('?>'.file_get_contents(__DIR__.'/../Fixtures/importmap_config_reader/importmap.php')))();
101+
unlink(__DIR__.'/../Fixtures/importmap_config_reader/importmap.php');
105102
// dump the entries back to the file
106103
$reader->writeEntries($entries);
107-
$newImportMapData = (static fn () => include __DIR__.'/../Fixtures/importmaps_for_writing/importmap.php')();
104+
$newImportMapData = (static fn () => eval('?>'.file_get_contents(__DIR__.'/../Fixtures/importmap_config_reader/importmap.php')))();
108105

109106
$this->assertSame($originalImportMapData, $newImportMapData);
110107
}

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapGeneratorTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ class ImportMapGeneratorTest extends TestCase
3030
private AssetMapperInterface&MockObject $assetMapper;
3131
private CompiledAssetMapperConfigReader&MockObject $compiledConfigReader;
3232
private ImportMapConfigReader&MockObject $configReader;
33-
private ImportMapGenerator $importMapGenerator;
3433

3534
private Filesystem $filesystem;
36-
private static string $writableRoot = __DIR__.'/../Fixtures/importmaps_for_writing';
35+
private static string $writableRoot = __DIR__.'/../Fixtures/importmap_generator';
3736

3837
protected function setUp(): void
3938
{
@@ -725,7 +724,7 @@ private function createImportMapGenerator(): ImportMapGenerator
725724
return ImportMapEntry::createRemote($importName, $type, $path, $version, $packageModuleSpecifier, $isEntrypoint);
726725
});
727726

728-
return $this->importMapGenerator = new ImportMapGenerator(
727+
return new ImportMapGenerator(
729728
$this->assetMapper,
730729
$this->compiledConfigReader,
731730
$this->configReader,

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,14 @@ class ImportMapManagerTest extends TestCase
3232
private PackageResolverInterface&MockObject $packageResolver;
3333
private ImportMapConfigReader&MockObject $configReader;
3434
private RemotePackageDownloader&MockObject $remotePackageDownloader;
35-
private ImportMapManager $importMapManager;
3635

3736
private Filesystem $filesystem;
38-
private static string $writableRoot = __DIR__.'/../Fixtures/importmaps_for_writing';
37+
private static string $writableRoot = __DIR__.'/../Fixtures/importmap_manager';
3938

4039
protected function setUp(): void
4140
{
4241
$this->filesystem = new Filesystem();
43-
if (!file_exists(__DIR__.'/../Fixtures/importmaps_for_writing')) {
44-
$this->filesystem->mkdir(self::$writableRoot);
45-
}
46-
if (!file_exists(__DIR__.'/../Fixtures/importmaps_for_writing/assets')) {
42+
if (!file_exists(__DIR__.'/../Fixtures/importmap_manager/assets')) {
4743
$this->filesystem->mkdir(self::$writableRoot.'/assets');
4844
}
4945
}
@@ -386,7 +382,7 @@ private function createImportMapManager(): ImportMapManager
386382
return ImportMapEntry::createRemote($importName, $type, $path, $version, $packageModuleSpecifier, $isEntrypoint);
387383
});
388384

389-
return $this->importMapManager = new ImportMapManager(
385+
return new ImportMapManager(
390386
$this->assetMapper,
391387
$this->configReader,
392388
$this->remotePackageDownloader,

src/Symfony/Component/AssetMapper/Tests/ImportMap/RemotePackageDownloaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class RemotePackageDownloaderTest extends TestCase
2525
{
2626
private Filesystem $filesystem;
27-
private static string $writableRoot = __DIR__.'/../Fixtures/importmaps_for_writing';
27+
private static string $writableRoot = __DIR__.'/../Fixtures/remote_package_downloader';
2828

2929
protected function setUp(): void
3030
{

src/Symfony/Component/AssetMapper/Tests/ImportMap/RemotePackageStorageTest.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
class RemotePackageStorageTest extends TestCase
2121
{
2222
private Filesystem $filesystem;
23-
private static string $writableRoot = __DIR__.'/../Fixtures/importmaps_for_writing';
23+
private static string $writableRoot;
24+
private static int $writableRootIndex = 0;
2425

2526
protected function setUp(): void
2627
{
28+
self::$writableRoot = sys_get_temp_dir().'/remote_package_storage'.++self::$writableRootIndex;
2729
$this->filesystem = new Filesystem();
28-
if (!$this->filesystem->exists(self::$writableRoot)) {
29-
$this->filesystem->mkdir(self::$writableRoot);
30-
}
30+
$this->filesystem->mkdir(self::$writableRoot);
3131
}
3232

3333
protected function tearDown(): void
@@ -41,19 +41,24 @@ public function testGetStorageDir()
4141
$this->assertSame(realpath(self::$writableRoot.'/assets/vendor'), realpath($storage->getStorageDir()));
4242
}
4343

44-
public function testSaveThrowsWhenVendorDirectoryIsNotWritable()
44+
public function testSaveThrowsWhenFailing()
4545
{
46-
$this->filesystem->mkdir($vendorDir = self::$writableRoot.'/assets/acme/vendor');
47-
$this->filesystem->chmod($vendorDir, 0555);
46+
$vendorDir = self::$writableRoot.'/assets/acme/vendor';
47+
$this->filesystem->mkdir($vendorDir.'/module_specifier');
48+
$this->filesystem->touch($vendorDir.'/module_specifier/module_specifier.index.js');
49+
$this->filesystem->chmod($vendorDir.'/module_specifier/module_specifier.index.js', 0555);
4850

4951
$storage = new RemotePackageStorage($vendorDir);
5052
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);
5153

5254
$this->expectException(\RuntimeException::class);
53-
$this->expectExceptionMessage('file_put_contents('.$vendorDir.'/module_specifier/module_specifier.index.js): Failed to open stream: No such file or directory');
54-
$storage->save($entry, 'any content');
55+
$this->expectExceptionMessage('file_put_contents('.$vendorDir.'/module_specifier/module_specifier.index.js): Failed to open stream: Permission denied');
5556

56-
$this->filesystem->remove($vendorDir);
57+
try {
58+
$storage->save($entry, 'any content');
59+
} finally {
60+
$this->filesystem->chmod($vendorDir.'/module_specifier/module_specifier.index.js', 0777);
61+
}
5762
}
5863

5964
public function testIsDownloaded()
@@ -106,33 +111,33 @@ public function testSaveExtraFile()
106111
public function testGetDownloadedPath(string $packageModuleSpecifier, ImportMapType $importMapType, string $expectedPath)
107112
{
108113
$storage = new RemotePackageStorage(self::$writableRoot.'/assets/vendor');
109-
$this->assertSame($expectedPath, $storage->getDownloadPath($packageModuleSpecifier, $importMapType));
114+
$this->assertSame(self::$writableRoot.$expectedPath, $storage->getDownloadPath($packageModuleSpecifier, $importMapType));
110115
}
111116

112117
public static function getDownloadPathTests(): iterable
113118
{
114119
yield 'javascript bare package' => [
115120
'packageModuleSpecifier' => 'foo',
116121
'importMapType' => ImportMapType::JS,
117-
'expectedPath' => self::$writableRoot.'/assets/vendor/foo/foo.index.js',
122+
'expectedPath' => '/assets/vendor/foo/foo.index.js',
118123
];
119124

120125
yield 'javascript package with path' => [
121126
'packageModuleSpecifier' => 'foo/bar',
122127
'importMapType' => ImportMapType::JS,
123-
'expectedPath' => self::$writableRoot.'/assets/vendor/foo/bar.js',
128+
'expectedPath' => '/assets/vendor/foo/bar.js',
124129
];
125130

126131
yield 'javascript package with path and extension' => [
127132
'packageModuleSpecifier' => 'foo/bar.js',
128133
'importMapType' => ImportMapType::JS,
129-
'expectedPath' => self::$writableRoot.'/assets/vendor/foo/bar.js',
134+
'expectedPath' => '/assets/vendor/foo/bar.js',
130135
];
131136

132137
yield 'CSS package with path' => [
133138
'packageModuleSpecifier' => 'foo/bar',
134139
'importMapType' => ImportMapType::CSS,
135-
'expectedPath' => self::$writableRoot.'/assets/vendor/foo/bar.css',
140+
'expectedPath' => '/assets/vendor/foo/bar.css',
136141
];
137142
}
138143
}

src/Symfony/Component/AssetMapper/Tests/MapperAwareAssetPackageIntegrationTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,31 @@
1111

1212
namespace Symfony\Component\AssetMapper\Tests;
1313

14-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
14+
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Asset\Packages;
1616
use Symfony\Component\AssetMapper\Tests\Fixtures\AssetMapperTestAppKernel;
17+
use Symfony\Component\Filesystem\Filesystem;
1718

18-
class MapperAwareAssetPackageIntegrationTest extends KernelTestCase
19+
class MapperAwareAssetPackageIntegrationTest extends TestCase
1920
{
20-
public function testDefaultAssetPackageIsDecorated()
21+
private AssetMapperTestAppKernel $kernel;
22+
private Filesystem $filesystem;
23+
24+
protected function setUp(): void
25+
{
26+
$this->filesystem = new Filesystem();
27+
$this->kernel = new AssetMapperTestAppKernel('test', true);
28+
$this->kernel->boot();
29+
}
30+
31+
protected function tearDown(): void
2132
{
22-
$kernel = new AssetMapperTestAppKernel('test', true);
23-
$kernel->boot();
33+
$this->filesystem->remove($this->kernel->getProjectDir().'/var');
34+
}
2435

25-
$packages = $kernel->getContainer()->get('public.assets.packages');
36+
public function testDefaultAssetPackageIsDecorated()
37+
{
38+
$packages = $this->kernel->getContainer()->get('public.assets.packages');
2639
\assert($packages instanceof Packages);
2740
$this->assertSame('/assets/file1-b3445cb7a86a0795a7af7f2004498aef.css', $packages->getUrl('file1.css'));
2841
$this->assertSame('/non-existent.css', $packages->getUrl('non-existent.css'));

src/Symfony/Component/AssetMapper/Tests/Path/LocalPublicAssetsFilesystemTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
class LocalPublicAssetsFilesystemTest extends TestCase
1919
{
2020
private Filesystem $filesystem;
21-
private static string $writableRoot = __DIR__.'/../Fixtures/importmaps_for_writing';
21+
private static string $writableRoot = __DIR__.'/../Fixtures/local_public_assets_filesystem';
2222

2323
protected function setUp(): void
2424
{
2525
$this->filesystem = new Filesystem();
26-
if (!file_exists(__DIR__.'/../Fixtures/importmaps_for_writing')) {
26+
if (!file_exists(__DIR__.'/../Fixtures/local_public_assets_filesystem')) {
2727
$this->filesystem->mkdir(self::$writableRoot);
2828
}
2929
}

0 commit comments

Comments
 (0)