|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\AssetMapper\Tests\Command; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 16 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 17 | +use Symfony\Component\AssetMapper\Command\ImportMapRequireCommand; |
| 18 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry; |
| 19 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapManager; |
| 20 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapType; |
| 21 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapVersionChecker; |
| 22 | +use Symfony\Component\AssetMapper\Tests\Fixtures\ImportMapTestAppKernel; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | +use Symfony\Component\Console\Tester\CommandTester; |
| 25 | +use Symfony\Component\Filesystem\Filesystem; |
| 26 | +use Symfony\Component\Finder\Finder; |
| 27 | + |
| 28 | +class ImportMapRequireCommandTest extends KernelTestCase |
| 29 | +{ |
| 30 | + protected static function getKernelClass(): string |
| 31 | + { |
| 32 | + return ImportMapTestAppKernel::class; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider getRequirePackageTests |
| 37 | + */ |
| 38 | + public function testDryRunOptionToShowInformationBeforeApplyInstallation(int $verbosity, array $packageEntries, array $packagesToInstall, string $expected, ?string $path = null) |
| 39 | + { |
| 40 | + $importMapManager = $this->createMock(ImportMapManager::class); |
| 41 | + $importMapManager |
| 42 | + ->method('requirePackages') |
| 43 | + ->willReturn($packageEntries) |
| 44 | + ; |
| 45 | + |
| 46 | + $command = new ImportMapRequireCommand( |
| 47 | + $importMapManager, |
| 48 | + $this->createMock(ImportMapVersionChecker::class), |
| 49 | + '/path/to/project/dir', |
| 50 | + ); |
| 51 | + |
| 52 | + $args = [ |
| 53 | + 'packages' => $packagesToInstall, |
| 54 | + '--dry-run' => true, |
| 55 | + ]; |
| 56 | + if ($path) { |
| 57 | + $args['--path'] = $path; |
| 58 | + } |
| 59 | + |
| 60 | + $commandTester = new CommandTester($command); |
| 61 | + $commandTester->execute($args, ['verbosity' => $verbosity]); |
| 62 | + |
| 63 | + $commandTester->assertCommandIsSuccessful(); |
| 64 | + |
| 65 | + $output = $commandTester->getDisplay(); |
| 66 | + $this->assertEquals($this->trimBeginEndOfEachLine($expected), $this->trimBeginEndOfEachLine($output)); |
| 67 | + } |
| 68 | + |
| 69 | + public static function getRequirePackageTests(): iterable |
| 70 | + { |
| 71 | + yield 'require package with dry run and normal verbosity options' => [ |
| 72 | + OutputInterface::VERBOSITY_NORMAL, |
| 73 | + [self::createRemoteEntry('bootstrap', '4.2.3', 'assets/vendor/bootstrap/bootstrap.js')], |
| 74 | + ['bootstrap'], <<<EOF |
| 75 | + [DRY-RUN] No changes will apply to the importmap configuration. |
| 76 | +
|
| 77 | + [OK] Package "bootstrap" added to importmap.php. |
| 78 | +
|
| 79 | + Use the new package normally by importing "bootstrap". |
| 80 | +
|
| 81 | + [DRY-RUN] No changes applied to the importmap configuration. |
| 82 | + EOF, |
| 83 | + ]; |
| 84 | + |
| 85 | + yield 'remote package requested with a version with dry run and verbosity verbose options' => [ |
| 86 | + OutputInterface::VERBOSITY_VERBOSE, |
| 87 | + [self::createRemoteEntry('bootstrap', '5.3.3', 'assets/vendor/bootstrap/bootstrap.js')], |
| 88 | + ['bootstrap'], <<<EOF |
| 89 | + [DRY-RUN] No changes will apply to the importmap configuration. |
| 90 | +
|
| 91 | + ----------- --------- -------------------------------------- |
| 92 | + Package Version Path |
| 93 | + ----------- --------- -------------------------------------- |
| 94 | + bootstrap 5.3.3 assets/vendor/bootstrap/bootstrap.js |
| 95 | + ----------- --------- -------------------------------------- |
| 96 | +
|
| 97 | + [OK] Package "bootstrap" added to importmap.php. |
| 98 | +
|
| 99 | + Use the new package normally by importing "bootstrap". |
| 100 | +
|
| 101 | + [DRY-RUN] No changes applied to the importmap configuration. |
| 102 | + EOF, |
| 103 | + ]; |
| 104 | + |
| 105 | + yield 'local package require a path, with dry run and verbosity verbose options' => [ |
| 106 | + OutputInterface::VERBOSITY_VERBOSE, |
| 107 | + [ImportMapEntry::createLocal('alice.js', ImportMapType::JS, 'assets/js/alice.js', false)], |
| 108 | + ['alice.js'], <<<EOF |
| 109 | + [DRY-RUN] No changes will apply to the importmap configuration. |
| 110 | +
|
| 111 | + ---------- --------- -------------------- |
| 112 | + Package Version Path |
| 113 | + ---------- --------- -------------------- |
| 114 | + alice.js - assets/js/alice.js |
| 115 | + ---------- --------- -------------------- |
| 116 | +
|
| 117 | + [OK] Package "alice.js" added to importmap.php. |
| 118 | +
|
| 119 | + Use the new package normally by importing "alice.js". |
| 120 | +
|
| 121 | + [DRY-RUN] No changes applied to the importmap configuration. |
| 122 | +EOF, |
| 123 | + './assets/alice.js', |
| 124 | + ]; |
| 125 | + |
| 126 | + yield 'multi remote packages requested with dry run and verbosity normal options' => [ |
| 127 | + OutputInterface::VERBOSITY_NORMAL, [ |
| 128 | + self::createRemoteEntry('bootstrap', '5.3.3', 'assets/vendor/bootstrap/bootstrap.index.js'), |
| 129 | + self::createRemoteEntry('lodash', '4.17.20', 'assets/vendor/lodash/lodash.index.js'), |
| 130 | + ], |
| 131 | + ['bootstrap lodash@4.17.21'], <<<EOF |
| 132 | + [DRY-RUN] No changes will apply to the importmap configuration. |
| 133 | +
|
| 134 | + [OK] 2 new items (bootstrap, lodash) added to the importmap.php! |
| 135 | +
|
| 136 | + [DRY-RUN] No changes applied to the importmap configuration. |
| 137 | + EOF, |
| 138 | + ]; |
| 139 | + |
| 140 | + yield 'multi remote packages requested with dry run and verbosity verbose options' => [ |
| 141 | + OutputInterface::VERBOSITY_VERBOSE, [ |
| 142 | + self::createRemoteEntry('bootstrap', '5.3.3', 'assets/vendor/bootstrap/bootstrap.js'), |
| 143 | + self::createRemoteEntry('lodash', '4.17.20', 'assets/vendor/lodash/lodash.index.js'), |
| 144 | + ], |
| 145 | + ['bootstrap lodash@4.17.21'], <<<EOF |
| 146 | + [DRY-RUN] No changes will apply to the importmap configuration. |
| 147 | +
|
| 148 | + ----------- --------- -------------------------------------- |
| 149 | + Package Version Path |
| 150 | + ----------- --------- -------------------------------------- |
| 151 | + bootstrap 5.3.3 assets/vendor/bootstrap/bootstrap.js |
| 152 | + lodash 4.17.20 assets/vendor/lodash/lodash.index.js |
| 153 | + ----------- --------- -------------------------------------- |
| 154 | +
|
| 155 | + [OK] 2 new items (bootstrap, lodash) added to the importmap.php! |
| 156 | +
|
| 157 | + [DRY-RUN] No changes applied to the importmap configuration. |
| 158 | + EOF, |
| 159 | + ]; |
| 160 | + } |
| 161 | + |
| 162 | + public function testNothingChangedOnFilesystemAfterUsingDryRunOption() |
| 163 | + { |
| 164 | + $kernel = self::bootKernel(); |
| 165 | + $projectDir = $kernel->getProjectDir(); |
| 166 | + |
| 167 | + $fs = new Filesystem(); |
| 168 | + $fs->mkdir($projectDir.'/public'); |
| 169 | + |
| 170 | + $fs->dumpFile($projectDir.'/public/assets/manifest.json', '{}'); |
| 171 | + $fs->dumpFile($projectDir.'/public/assets/importmap.json', '{}'); |
| 172 | + |
| 173 | + $importMapManager = $this->createMock(ImportMapManager::class); |
| 174 | + $importMapManager |
| 175 | + ->expects($this->once()) |
| 176 | + ->method('requirePackages') |
| 177 | + ->willReturn([self::createRemoteEntry('bootstrap', '5.3.3', 'assets/vendor/bootstrap/bootstrap.index.js')]); |
| 178 | + |
| 179 | + self::getContainer()->set(ImportMapManager::class, $importMapManager); |
| 180 | + |
| 181 | + $application = new Application(self::$kernel); |
| 182 | + $command = $application->find('importmap:require'); |
| 183 | + |
| 184 | + $importMapContentBefore = $fs->readFile($projectDir.'/importmap.php'); |
| 185 | + $installedVendorBefore = $fs->readFile($projectDir.'/assets/vendor/installed.php'); |
| 186 | + |
| 187 | + $tester = new CommandTester($command); |
| 188 | + $tester->execute(['packages' => ['bootstrap'], '--dry-run' => true]); |
| 189 | + |
| 190 | + $tester->assertCommandIsSuccessful(); |
| 191 | + |
| 192 | + $this->assertSame($importMapContentBefore, $fs->readFile($projectDir.'/importmap.php')); |
| 193 | + $this->assertSame($installedVendorBefore, $fs->readFile($projectDir.'/assets/vendor/installed.php')); |
| 194 | + |
| 195 | + $this->assertSame('{}', $fs->readFile($projectDir.'/public/assets/manifest.json')); |
| 196 | + $this->assertSame('{}', $fs->readFile($projectDir.'/public/assets/importmap.json')); |
| 197 | + |
| 198 | + $finder = new Finder(); |
| 199 | + $finder->in($projectDir.'/public/assets')->files()->depth(0); |
| 200 | + |
| 201 | + $this->assertCount(2, $finder); // manifest.json + importmap.json |
| 202 | + |
| 203 | + $fs->remove($projectDir.'/public'); |
| 204 | + $fs->remove($projectDir.'/var'); |
| 205 | + |
| 206 | + static::$kernel->shutdown(); |
| 207 | + } |
| 208 | + |
| 209 | + private static function createRemoteEntry(string $importName, string $version, ?string $path = null): ImportMapEntry |
| 210 | + { |
| 211 | + return ImportMapEntry::createRemote($importName, ImportMapType::JS, path: $path, version: $version, packageModuleSpecifier: $importName, isEntrypoint: false); |
| 212 | + } |
| 213 | + |
| 214 | + private function trimBeginEndOfEachLine(string $lines): string |
| 215 | + { |
| 216 | + return trim(implode("\n", array_map('trim', explode("\n", $lines)))); |
| 217 | + } |
| 218 | +} |
0 commit comments