This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
<?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\Asset\Tests\Context;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\Context\NullContext;
class NullContextTest extends TestCase
{
public function testGetBasePath()
{
$nullContext = new NullContext();
$this->assertEmpty($nullContext->getBasePath());
}
public function testIsSecure()
{
$nullContext = new NullContext();
$this->assertFalse($nullContext->isSecure());
}
}

View File

@@ -0,0 +1,73 @@
<?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\Asset\Tests\Context;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\Context\RequestStackContext;
class RequestStackContextTest extends TestCase
{
public function testGetBasePathEmpty()
{
$requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
$requestStackContext = new RequestStackContext($requestStack);
$this->assertEmpty($requestStackContext->getBasePath());
}
public function testGetBasePathSet()
{
$testBasePath = 'test-path';
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->method('getBasePath')
->willReturn($testBasePath);
$requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
$requestStack->method('getMasterRequest')
->willReturn($request);
$requestStackContext = new RequestStackContext($requestStack);
$this->assertSame($testBasePath, $requestStackContext->getBasePath());
}
public function testIsSecureFalse()
{
$requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
$requestStackContext = new RequestStackContext($requestStack);
$this->assertFalse($requestStackContext->isSecure());
}
public function testIsSecureTrue()
{
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->method('isSecure')
->willReturn(true);
$requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
$requestStack->method('getMasterRequest')
->willReturn($request);
$requestStackContext = new RequestStackContext($requestStack);
$this->assertTrue($requestStackContext->isSecure());
}
public function testDefaultContext()
{
$requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
$requestStackContext = new RequestStackContext($requestStack, 'default-path', true);
$this->assertSame('default-path', $requestStackContext->getBasePath());
$this->assertTrue($requestStackContext->isSecure());
}
}

View File

@@ -0,0 +1,55 @@
<?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\Asset\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
class PackageTest extends TestCase
{
/**
* @dataProvider getConfigs
*/
public function testGetUrl($version, $format, $path, $expected)
{
$package = new Package($version ? new StaticVersionStrategy($version, $format) : new EmptyVersionStrategy());
$this->assertSame($expected, $package->getUrl($path));
}
public function getConfigs()
{
return [
['v1', '', 'http://example.com/foo', 'http://example.com/foo'],
['v1', '', 'https://example.com/foo', 'https://example.com/foo'],
['v1', '', '//example.com/foo', '//example.com/foo'],
['v1', '', '/foo', '/foo?v1'],
['v1', '', 'foo', 'foo?v1'],
[null, '', '/foo', '/foo'],
[null, '', 'foo', 'foo'],
['v1', 'version-%2$s/%1$s', '/foo', '/version-v1/foo'],
['v1', 'version-%2$s/%1$s', 'foo', 'version-v1/foo'],
['v1', 'version-%2$s/%1$s', 'foo/', 'version-v1/foo/'],
['v1', 'version-%2$s/%1$s', '/foo/', '/version-v1/foo/'],
];
}
public function testGetVersion()
{
$package = new Package(new StaticVersionStrategy('v1'));
$this->assertSame('v1', $package->getVersion('/foo'));
}
}

View File

@@ -0,0 +1,71 @@
<?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\Asset\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
class PackagesTest extends TestCase
{
public function testGetterSetters()
{
$packages = new Packages();
$packages->setDefaultPackage($default = $this->getMockBuilder('Symfony\Component\Asset\PackageInterface')->getMock());
$packages->addPackage('a', $a = $this->getMockBuilder('Symfony\Component\Asset\PackageInterface')->getMock());
$this->assertSame($default, $packages->getPackage());
$this->assertSame($a, $packages->getPackage('a'));
$packages = new Packages($default, ['a' => $a]);
$this->assertSame($default, $packages->getPackage());
$this->assertSame($a, $packages->getPackage('a'));
}
public function testGetVersion()
{
$packages = new Packages(
new Package(new StaticVersionStrategy('default')),
['a' => new Package(new StaticVersionStrategy('a'))]
);
$this->assertSame('default', $packages->getVersion('/foo'));
$this->assertSame('a', $packages->getVersion('/foo', 'a'));
}
public function testGetUrl()
{
$packages = new Packages(
new Package(new StaticVersionStrategy('default')),
['a' => new Package(new StaticVersionStrategy('a'))]
);
$this->assertSame('/foo?default', $packages->getUrl('/foo'));
$this->assertSame('/foo?a', $packages->getUrl('/foo', 'a'));
}
public function testNoDefaultPackage()
{
$this->expectException('Symfony\Component\Asset\Exception\LogicException');
$packages = new Packages();
$packages->getPackage();
}
public function testUndefinedPackage()
{
$this->expectException('Symfony\Component\Asset\Exception\InvalidArgumentException');
$packages = new Packages();
$packages->getPackage('a');
}
}

View File

@@ -0,0 +1,96 @@
<?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\Asset\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\PathPackage;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
class PathPackageTest extends TestCase
{
/**
* @dataProvider getConfigs
*/
public function testGetUrl($basePath, $format, $path, $expected)
{
$package = new PathPackage($basePath, new StaticVersionStrategy('v1', $format));
$this->assertSame($expected, $package->getUrl($path));
}
public function getConfigs()
{
return [
['/foo', '', 'http://example.com/foo', 'http://example.com/foo'],
['/foo', '', 'https://example.com/foo', 'https://example.com/foo'],
['/foo', '', '//example.com/foo', '//example.com/foo'],
['', '', '/foo', '/foo?v1'],
['/foo', '', '/bar', '/bar?v1'],
['/foo', '', 'bar', '/foo/bar?v1'],
['foo', '', 'bar', '/foo/bar?v1'],
['foo/', '', 'bar', '/foo/bar?v1'],
['/foo/', '', 'bar', '/foo/bar?v1'],
['/foo', 'version-%2$s/%1$s', '/bar', '/version-v1/bar'],
['/foo', 'version-%2$s/%1$s', 'bar', '/foo/version-v1/bar'],
['/foo', 'version-%2$s/%1$s', 'bar/', '/foo/version-v1/bar/'],
['/foo', 'version-%2$s/%1$s', '/bar/', '/version-v1/bar/'],
];
}
/**
* @dataProvider getContextConfigs
*/
public function testGetUrlWithContext($basePathRequest, $basePath, $format, $path, $expected)
{
$package = new PathPackage($basePath, new StaticVersionStrategy('v1', $format), $this->getContext($basePathRequest));
$this->assertSame($expected, $package->getUrl($path));
}
public function getContextConfigs()
{
return [
['', '/foo', '', '/baz', '/baz?v1'],
['', '/foo', '', 'baz', '/foo/baz?v1'],
['', 'foo', '', 'baz', '/foo/baz?v1'],
['', 'foo/', '', 'baz', '/foo/baz?v1'],
['', '/foo/', '', 'baz', '/foo/baz?v1'],
['/bar', '/foo', '', '/baz', '/baz?v1'],
['/bar', '/foo', '', 'baz', '/bar/foo/baz?v1'],
['/bar', 'foo', '', 'baz', '/bar/foo/baz?v1'],
['/bar', 'foo/', '', 'baz', '/bar/foo/baz?v1'],
['/bar', '/foo/', '', 'baz', '/bar/foo/baz?v1'],
];
}
public function testVersionStrategyGivesAbsoluteURL()
{
$versionStrategy = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface')->getMock();
$versionStrategy->expects($this->any())
->method('applyVersion')
->willReturn('https://cdn.com/bar/main.css');
$package = new PathPackage('/subdirectory', $versionStrategy, $this->getContext('/bar'));
$this->assertSame('https://cdn.com/bar/main.css', $package->getUrl('main.css'));
}
private function getContext($basePath)
{
$context = $this->getMockBuilder('Symfony\Component\Asset\Context\ContextInterface')->getMock();
$context->expects($this->any())->method('getBasePath')->willReturn($basePath);
return $context;
}
}

View File

@@ -0,0 +1,110 @@
<?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\Asset\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\UrlPackage;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
class UrlPackageTest extends TestCase
{
/**
* @dataProvider getConfigs
*/
public function testGetUrl($baseUrls, $format, $path, $expected)
{
$package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format));
$this->assertSame($expected, $package->getUrl($path));
}
public function getConfigs()
{
return [
['http://example.net', '', 'http://example.com/foo', 'http://example.com/foo'],
['http://example.net', '', 'https://example.com/foo', 'https://example.com/foo'],
['http://example.net', '', '//example.com/foo', '//example.com/foo'],
['http://example.com', '', '/foo', 'http://example.com/foo?v1'],
['http://example.com', '', 'foo', 'http://example.com/foo?v1'],
['http://example.com/', '', 'foo', 'http://example.com/foo?v1'],
['http://example.com/foo', '', 'foo', 'http://example.com/foo/foo?v1'],
['http://example.com/foo/', '', 'foo', 'http://example.com/foo/foo?v1'],
[['http://example.com'], '', '/foo', 'http://example.com/foo?v1'],
[['http://example.com', 'http://example.net'], '', '/foo', 'http://example.com/foo?v1'],
[['http://example.com', 'http://example.net'], '', '/fooa', 'http://example.net/fooa?v1'],
['http://example.com', 'version-%2$s/%1$s', '/foo', 'http://example.com/version-v1/foo'],
['http://example.com', 'version-%2$s/%1$s', 'foo', 'http://example.com/version-v1/foo'],
['http://example.com', 'version-%2$s/%1$s', 'foo/', 'http://example.com/version-v1/foo/'],
['http://example.com', 'version-%2$s/%1$s', '/foo/', 'http://example.com/version-v1/foo/'],
];
}
/**
* @dataProvider getContextConfigs
*/
public function testGetUrlWithContext($secure, $baseUrls, $format, $path, $expected)
{
$package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format), $this->getContext($secure));
$this->assertSame($expected, $package->getUrl($path));
}
public function getContextConfigs()
{
return [
[false, 'http://example.com', '', 'foo', 'http://example.com/foo?v1'],
[false, ['http://example.com'], '', 'foo', 'http://example.com/foo?v1'],
[false, ['http://example.com', 'https://example.com'], '', 'foo', 'http://example.com/foo?v1'],
[false, ['http://example.com', 'https://example.com'], '', 'fooa', 'https://example.com/fooa?v1'],
[false, ['http://example.com/bar'], '', 'foo', 'http://example.com/bar/foo?v1'],
[false, ['http://example.com/bar/'], '', 'foo', 'http://example.com/bar/foo?v1'],
[false, ['//example.com/bar/'], '', 'foo', '//example.com/bar/foo?v1'],
[true, ['http://example.com'], '', 'foo', 'http://example.com/foo?v1'],
[true, ['http://example.com', 'https://example.com'], '', 'foo', 'https://example.com/foo?v1'],
];
}
public function testVersionStrategyGivesAbsoluteURL()
{
$versionStrategy = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface')->getMock();
$versionStrategy->expects($this->any())
->method('applyVersion')
->willReturn('https://cdn.com/bar/main.css');
$package = new UrlPackage('https://example.com', $versionStrategy);
$this->assertSame('https://cdn.com/bar/main.css', $package->getUrl('main.css'));
}
public function testNoBaseUrls()
{
$this->expectException('Symfony\Component\Asset\Exception\LogicException');
new UrlPackage([], new EmptyVersionStrategy());
}
public function testWrongBaseUrl()
{
$this->expectException('Symfony\Component\Asset\Exception\InvalidArgumentException');
new UrlPackage(['not-a-url'], new EmptyVersionStrategy());
}
private function getContext($secure)
{
$context = $this->getMockBuilder('Symfony\Component\Asset\Context\ContextInterface')->getMock();
$context->expects($this->any())->method('isSecure')->willReturn($secure);
return $context;
}
}

View File

@@ -0,0 +1,34 @@
<?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\Asset\Tests\VersionStrategy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
class EmptyVersionStrategyTest extends TestCase
{
public function testGetVersion()
{
$emptyVersionStrategy = new EmptyVersionStrategy();
$path = 'test-path';
$this->assertEmpty($emptyVersionStrategy->getVersion($path));
}
public function testApplyVersion()
{
$emptyVersionStrategy = new EmptyVersionStrategy();
$path = 'test-path';
$this->assertSame($path, $emptyVersionStrategy->applyVersion($path));
}
}

View File

@@ -0,0 +1,59 @@
<?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\Asset\Tests\VersionStrategy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
class JsonManifestVersionStrategyTest extends TestCase
{
public function testGetVersion()
{
$strategy = $this->createStrategy('manifest-valid.json');
$this->assertSame('main.123abc.js', $strategy->getVersion('main.js'));
}
public function testApplyVersion()
{
$strategy = $this->createStrategy('manifest-valid.json');
$this->assertSame('css/styles.555def.css', $strategy->getVersion('css/styles.css'));
}
public function testApplyVersionWhenKeyDoesNotExistInManifest()
{
$strategy = $this->createStrategy('manifest-valid.json');
$this->assertSame('css/other.css', $strategy->getVersion('css/other.css'));
}
public function testMissingManifestFileThrowsException()
{
$this->expectException('RuntimeException');
$strategy = $this->createStrategy('non-existent-file.json');
$strategy->getVersion('main.js');
}
public function testManifestFileWithBadJSONThrowsException()
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('Error parsing JSON');
$strategy = $this->createStrategy('manifest-invalid.json');
$strategy->getVersion('main.js');
}
private function createStrategy($manifestFilename)
{
return new JsonManifestVersionStrategy(__DIR__.'/../fixtures/'.$manifestFilename);
}
}

View File

@@ -0,0 +1,44 @@
<?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\Asset\Tests\VersionStrategy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
class StaticVersionStrategyTest extends TestCase
{
public function testGetVersion()
{
$version = 'v1';
$path = 'test-path';
$staticVersionStrategy = new StaticVersionStrategy($version);
$this->assertSame($version, $staticVersionStrategy->getVersion($path));
}
/**
* @dataProvider getConfigs
*/
public function testApplyVersion($path, $version, $format)
{
$staticVersionStrategy = new StaticVersionStrategy($version, $format);
$formatted = sprintf($format ?: '%s?%s', $path, $version);
$this->assertSame($formatted, $staticVersionStrategy->applyVersion($path));
}
public function getConfigs()
{
return [
['test-path', 'v1', null],
['test-path', 'v2', '%s?test%s'],
];
}
}

View File

@@ -0,0 +1,4 @@
{
"main.js": main.123abc.js",
"css/styles.css": "css/styles.555def.css"
}

View File

@@ -0,0 +1,4 @@
{
"main.js": "main.123abc.js",
"css/styles.css": "css/styles.555def.css"
}