|
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\Attributes\CoversClass;
|
6 | 6 | use PHPUnit\Framework\TestCase;
|
| 7 | +use Shopware\Core\Framework\Adapter\AdapterException; |
7 | 8 | use Shopware\Core\Framework\Adapter\Asset\AssetPackageService;
|
| 9 | +use Shopware\Core\Framework\Adapter\Asset\FallbackUrlPackage; |
| 10 | +use Shopware\Core\Framework\Test\TestCaseBase\EnvTestBehaviour; |
8 | 11 | use Symfony\Component\Asset\Package;
|
| 12 | +use Symfony\Component\Asset\Packages; |
9 | 13 | use Symfony\Component\Asset\UrlPackage;
|
10 | 14 | use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
|
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\RequestStack; |
11 | 17 |
|
12 | 18 | /**
|
13 | 19 | * @internal
|
14 | 20 | */
|
15 | 21 | #[CoversClass(AssetPackageService::class)]
|
16 | 22 | class AssetPackageServiceTest extends TestCase
|
17 | 23 | {
|
18 |
| - public function testCreate(): void |
| 24 | + use EnvTestBehaviour; |
| 25 | + |
| 26 | + public function testCreateWithRequest(): void |
19 | 27 | {
|
20 |
| - $bundleMap = [ |
21 |
| - 'TestBundle' => '/var/www/html/vendor/shopware/core/TestBundle', |
22 |
| - 'TestPlugin' => '/var/www/html/custom/plugins/TestPlugin', |
23 |
| - ]; |
| 28 | + $requestStack = new RequestStack(); |
| 29 | + $requestStack->push(Request::create('https://test.de')); |
| 30 | + |
| 31 | + $packages = $this->getPackages($requestStack); |
24 | 32 |
|
25 |
| - $package = $this->createMock(Package::class); |
26 |
| - $package->method('getUrl') |
27 |
| - ->willReturnCallback(static function (string $path): string { |
28 |
| - $urls = [ |
29 |
| - // bundle prefix should be removed @see AssetService::getTargetDirectory |
30 |
| - // and the path should be lowercased |
31 |
| - '/bundles/test' => 'http://localhost/bundles/test', |
32 |
| - '/bundles/testplugin' => 'http://localhost/bundles/testplugin', |
33 |
| - ]; |
| 33 | + $bundlePackage = $packages->getPackage('@TestBundle'); |
| 34 | + static::assertInstanceOf(UrlPackage::class, $bundlePackage); |
| 35 | + static::assertSame('https://test.de/bundles/test/foo', $bundlePackage->getUrl('/foo')); |
34 | 36 |
|
35 |
| - return $urls[$path]; |
36 |
| - }); |
| 37 | + $pluginPackage = $packages->getPackage('@TestPlugin'); |
| 38 | + static::assertInstanceOf(UrlPackage::class, $pluginPackage); |
| 39 | + static::assertSame('https://test.de/bundles/testplugin/foo', $pluginPackage->getUrl('/foo')); |
| 40 | + } |
37 | 41 |
|
38 |
| - $defaultPackage = new Package(new EmptyVersionStrategy()); |
39 |
| - $packages = AssetPackageService::create($bundleMap, $package, new EmptyVersionStrategy(), $defaultPackage); |
| 42 | + public function testCreateWithAppUrl(): void |
| 43 | + { |
| 44 | + $this->setEnvVars(['APP_URL' => 'https://test.de']); |
40 | 45 |
|
41 |
| - static::assertSame($defaultPackage, $packages->getPackage()); |
| 46 | + $packages = $this->getPackages(); |
42 | 47 |
|
43 | 48 | $bundlePackage = $packages->getPackage('@TestBundle');
|
44 | 49 | static::assertInstanceOf(UrlPackage::class, $bundlePackage);
|
45 |
| - static::assertSame('http://localhost/bundles/test/foo', $bundlePackage->getUrl('/foo')); |
| 50 | + static::assertSame('https://test.de/bundles/test/foo', $bundlePackage->getUrl('/foo')); |
46 | 51 |
|
47 | 52 | $pluginPackage = $packages->getPackage('@TestPlugin');
|
48 | 53 | static::assertInstanceOf(UrlPackage::class, $pluginPackage);
|
49 |
| - static::assertSame('http://localhost/bundles/testplugin/foo', $pluginPackage->getUrl('/foo')); |
| 54 | + static::assertSame('https://test.de/bundles/testplugin/foo', $pluginPackage->getUrl('/foo')); |
| 55 | + } |
| 56 | + |
| 57 | + public function testCreateWithoutAppUrl(): void |
| 58 | + { |
| 59 | + $this->setEnvVars(['APP_URL' => '']); |
| 60 | + $this->expectException(AdapterException::class); |
| 61 | + $this->expectExceptionMessage('Invalid asset URL. Check the "APP_URL" environment variable. Error message: "/bundles/test" is not a valid URL.'); |
| 62 | + $this->getPackages(); |
| 63 | + } |
| 64 | + |
| 65 | + private function getPackages(?RequestStack $requestStack = null): Packages |
| 66 | + { |
| 67 | + $emptyVersionStrategy = new EmptyVersionStrategy(); |
| 68 | + |
| 69 | + return AssetPackageService::create( |
| 70 | + [ |
| 71 | + 'TestBundle' => '/var/www/html/vendor/shopware/core/TestBundle', |
| 72 | + 'TestPlugin' => '/var/www/html/custom/plugins/TestPlugin', |
| 73 | + ], |
| 74 | + new FallbackUrlPackage('', $emptyVersionStrategy, $requestStack), |
| 75 | + $emptyVersionStrategy, |
| 76 | + new Package($emptyVersionStrategy) |
| 77 | + ); |
50 | 78 | }
|
51 | 79 | }
|
0 commit comments