|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\AssetMapper\Tests\ImportMap; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader; |
| 7 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapEntries; |
| 8 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry; |
| 9 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapType; |
| 10 | +use Symfony\Component\AssetMapper\ImportMap\RemotePackageDownloader; |
| 11 | +use Symfony\Component\AssetMapper\ImportMap\Resolver\PackageResolverInterface; |
| 12 | +use Symfony\Component\Filesystem\Filesystem; |
| 13 | + |
| 14 | +class RemotePackageDownloaderTest extends TestCase |
| 15 | +{ |
| 16 | + private Filesystem $filesystem; |
| 17 | + private static string $writableRoot = __DIR__.'/../fixtures/importmaps_for_writing'; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + $this->filesystem = new Filesystem(); |
| 22 | + if (!file_exists(__DIR__.'/../fixtures/importmaps_for_writing')) { |
| 23 | + $this->filesystem->mkdir(self::$writableRoot); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + protected function tearDown(): void |
| 28 | + { |
| 29 | + $this->filesystem->remove(self::$writableRoot); |
| 30 | + } |
| 31 | + |
| 32 | + public function testDownloadPackagesDownloadsEverythingWithNoInstalled() |
| 33 | + { |
| 34 | + $configReader = $this->createMock(ImportMapConfigReader::class); |
| 35 | + $packageResolver = $this->createMock(PackageResolverInterface::class); |
| 36 | + |
| 37 | + $entry1 = new ImportMapEntry('foo', version: '1.0.0'); |
| 38 | + $entry2 = new ImportMapEntry('bar.js/file', version: '1.0.0'); |
| 39 | + $entry3 = new ImportMapEntry('baz', version: '1.0.0', type: ImportMapType::CSS); |
| 40 | + $importMapEntries = new ImportMapEntries([$entry1, $entry2, $entry3]); |
| 41 | + |
| 42 | + $configReader->expects($this->once()) |
| 43 | + ->method('getEntries') |
| 44 | + ->willReturn($importMapEntries); |
| 45 | + |
| 46 | + $progressCallback = fn() => null; |
| 47 | + $packageResolver->expects($this->once()) |
| 48 | + ->method('downloadPackages') |
| 49 | + ->with( |
| 50 | + ['foo' => $entry1, 'bar.js/file' => $entry2, 'baz' => $entry3], |
| 51 | + $progressCallback |
| 52 | + ) |
| 53 | + ->willReturn(['foo' => 'foo content', 'bar.js/file' => 'bar content', 'baz' => 'baz content']); |
| 54 | + |
| 55 | + $downloader = new RemotePackageDownloader( |
| 56 | + $configReader, |
| 57 | + $packageResolver, |
| 58 | + self::$writableRoot.'/assets/vendor', |
| 59 | + ); |
| 60 | + $downloader->downloadPackages($progressCallback); |
| 61 | + |
| 62 | + $this->assertFileExists(self::$writableRoot.'/assets/vendor/foo.js'); |
| 63 | + $this->assertFileExists(self::$writableRoot.'/assets/vendor/bar.js/file.js'); |
| 64 | + $this->assertFileExists(self::$writableRoot.'/assets/vendor/baz.css'); |
| 65 | + $this->assertEquals('foo content', file_get_contents(self::$writableRoot.'/assets/vendor/foo.js')); |
| 66 | + $this->assertEquals('bar content', file_get_contents(self::$writableRoot.'/assets/vendor/bar.js/file.js')); |
| 67 | + $this->assertEquals('baz content', file_get_contents(self::$writableRoot.'/assets/vendor/baz.css')); |
| 68 | + |
| 69 | + $installed = require self::$writableRoot.'/assets/vendor/installed.php'; |
| 70 | + $this->assertEquals( |
| 71 | + [ |
| 72 | + 'foo' => ['path' => 'foo.js', 'version' => '1.0.0'], |
| 73 | + 'bar.js/file' => ['path' => 'bar.js/file.js', 'version' => '1.0.0'], |
| 74 | + 'baz' => ['path' => 'baz.css', 'version' => '1.0.0'], |
| 75 | + ], |
| 76 | + $installed |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function testPackagesWithCorrectInstalledVersionSkipped() |
| 81 | + { |
| 82 | + $this->filesystem->mkdir(self::$writableRoot.'/assets/vendor'); |
| 83 | + $installed = [ |
| 84 | + 'foo' => ['path' => 'foo.js', 'version' => '1.0.0'], |
| 85 | + 'bar.js/file' => ['path' => 'bar.js/file.js', 'version' => '1.0.0'], |
| 86 | + 'baz' => ['path' => 'baz.css', 'version' => '1.0.0'], |
| 87 | + ]; |
| 88 | + file_put_contents( |
| 89 | + self::$writableRoot.'/assets/vendor/installed.php', |
| 90 | + '<?php return '.var_export($installed, true).';' |
| 91 | + ); |
| 92 | + |
| 93 | + $configReader = $this->createMock(ImportMapConfigReader::class); |
| 94 | + $packageResolver = $this->createMock(PackageResolverInterface::class); |
| 95 | + |
| 96 | + // matches installed version and file exists |
| 97 | + $entry1 = new ImportMapEntry('foo', version: '1.0.0'); |
| 98 | + file_put_contents(self::$writableRoot.'/assets/vendor/foo.js', 'original foo content'); |
| 99 | + // matches installed version but file does not exist |
| 100 | + $entry2 = new ImportMapEntry('bar.js/file', version: '1.0.0'); |
| 101 | + // does not match installed version |
| 102 | + $entry3 = new ImportMapEntry('baz', version: '1.1.0', type: ImportMapType::CSS); |
| 103 | + file_put_contents(self::$writableRoot.'/assets/vendor/baz.css', 'original baz content'); |
| 104 | + $importMapEntries = new ImportMapEntries([$entry1, $entry2, $entry3]); |
| 105 | + |
| 106 | + $configReader->expects($this->once()) |
| 107 | + ->method('getEntries') |
| 108 | + ->willReturn($importMapEntries); |
| 109 | + |
| 110 | + $packageResolver->expects($this->once()) |
| 111 | + ->method('downloadPackages') |
| 112 | + ->willReturn(['bar.js/file' => 'new bar content', 'baz' => 'new baz content']); |
| 113 | + |
| 114 | + $downloader = new RemotePackageDownloader( |
| 115 | + $configReader, |
| 116 | + $packageResolver, |
| 117 | + self::$writableRoot.'/assets/vendor', |
| 118 | + ); |
| 119 | + $downloader->downloadPackages(); |
| 120 | + |
| 121 | + $this->assertFileExists(self::$writableRoot.'/assets/vendor/foo.js'); |
| 122 | + $this->assertFileExists(self::$writableRoot.'/assets/vendor/bar.js/file.js'); |
| 123 | + $this->assertFileExists(self::$writableRoot.'/assets/vendor/baz.css'); |
| 124 | + $this->assertEquals('original foo content', file_get_contents(self::$writableRoot.'/assets/vendor/foo.js')); |
| 125 | + $this->assertEquals('new bar content', file_get_contents(self::$writableRoot.'/assets/vendor/bar.js/file.js')); |
| 126 | + $this->assertEquals('new baz content', file_get_contents(self::$writableRoot.'/assets/vendor/baz.css')); |
| 127 | + |
| 128 | + $installed = require self::$writableRoot.'/assets/vendor/installed.php'; |
| 129 | + $this->assertEquals( |
| 130 | + [ |
| 131 | + 'foo' => ['path' => 'foo.js', 'version' => '1.0.0'], |
| 132 | + 'bar.js/file' => ['path' => 'bar.js/file.js', 'version' => '1.0.0'], |
| 133 | + 'baz' => ['path' => 'baz.css', 'version' => '1.1.0'], |
| 134 | + ], |
| 135 | + $installed |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + public function testGetDownloadedPath() |
| 140 | + { |
| 141 | + $this->filesystem->mkdir(self::$writableRoot.'/assets/vendor'); |
| 142 | + $installed = [ |
| 143 | + 'foo' => ['path' => 'foo-path.js', 'version' => '1.0.0'], |
| 144 | + ]; |
| 145 | + file_put_contents( |
| 146 | + self::$writableRoot.'/assets/vendor/installed.php', |
| 147 | + '<?php return '.var_export($installed, true).';' |
| 148 | + ); |
| 149 | + file_put_contents(self::$writableRoot.'/assets/vendor/foo-path.js', 'foo content'); |
| 150 | + |
| 151 | + $downloader = new RemotePackageDownloader( |
| 152 | + $this->createMock(ImportMapConfigReader::class), |
| 153 | + $this->createMock(PackageResolverInterface::class), |
| 154 | + self::$writableRoot.'/assets/vendor', |
| 155 | + ); |
| 156 | + $this->assertSame(realpath(self::$writableRoot.'/assets/vendor/foo-path.js'), realpath($downloader->getDownloadedPath('foo'))); |
| 157 | + } |
| 158 | + |
| 159 | + public function testGetVendorDir() |
| 160 | + { |
| 161 | + $downloader = new RemotePackageDownloader( |
| 162 | + $this->createMock(ImportMapConfigReader::class), |
| 163 | + $this->createMock(PackageResolverInterface::class), |
| 164 | + self::$writableRoot.'/assets/vendor', |
| 165 | + ); |
| 166 | + $this->assertSame(realpath(self::$writableRoot.'/assets/vendor'), realpath($downloader->getVendorDir())); |
| 167 | + } |
| 168 | +} |
0 commit comments