Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #26 from Bendihossan/honour-assets-base-url

Honour assets base url
  • Loading branch information...
commit 53b61f8fc2f8f16fa4c7a7c6a57924f42718dcaa 2 parents 343fef4 + 6cab4ca
@IgorTimoshenko IgorTimoshenko authored
View
15 Configuration/ConfigurationBuilder.php
@@ -108,18 +108,25 @@ public function setOption($option, $value)
*/
public function getConfiguration()
{
- $rootUrl = '';
if ($this->useControllerForAssets && $this->container->isScopeActive('request')) {
- $rootUrl = $this->container->get('request')->getBaseUrl();
+ $baseUrl = $this->container->get('request')->getBaseUrl();
+ $baseUrl = $baseUrl . '/' . \ltrim($this->baseUrl, '/');
+ } else {
+ $baseUrl = $this->container->get('templating.helper.assets')->getUrl(\ltrim($this->baseUrl, '/'));
+ // remove ?version from end of URL
+ if (($p = strpos($baseUrl, '?')) !== false) {
+ $baseUrl = substr($baseUrl, 0, $p);
+ }
}
+
$config = array(
- 'baseUrl' => $rootUrl . '/' . \ltrim($this->baseUrl, '/'),
+ 'baseUrl' => $baseUrl,
'locale' => $this->translator->getLocale(),
);
if ($this->paths !== null) {
$config['paths'] = $this->paths;
}
return array_merge($config, $this->additionalConfig);
- }
+ }
}
View
48 Tests/Configuration/ConfigurationBuilderTest.php
@@ -32,8 +32,34 @@
*/
class ConfigurationBuilderTest extends \PHPUnit_Framework_TestCase
{
+ /**
+ * @param $container
+ * @param $filename
+ */
+ protected function setMockAssetTwigExtension($container, $filename)
+ {
+
+ $mockAssetsHelper = $this
+ ->getMockBuilder('Symfony\Component\Templating\Helper\AssetsHelper')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $mockAssetsHelper
+ ->expects($this->any())
+ ->method('getUrl')
+ ->will($this->returnValue($filename));
+
+ $container->set('templating.helper.assets', $mockAssetsHelper);
+ }
+
public function testConfigurationGenerated()
{
+ $container = new \Symfony\Component\DependencyInjection\Container();
+ $requestScope = new \Symfony\Component\DependencyInjection\Scope('request');
+
+ // Assets twig funtion only available in request scope, so mock it out
+ $this->setMockAssetTwigExtension($container, 'base');
+
$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
$translator->expects($this->any())
->method('getLocale')
@@ -42,8 +68,7 @@ public function testConfigurationGenerated()
$request->expects($this->any())
->method('getBaseUrl')
->will($this->returnValue('/base'));
- $container = new \Symfony\Component\DependencyInjection\Container();
- $requestScope = new \Symfony\Component\DependencyInjection\Scope('request');
+
$container->addScope($requestScope);
$container->enterScope('request');
$container->set('request', $request);
@@ -75,17 +100,22 @@ public function testBaseUrlSlashesTrimmed()
$builder = new ConfigurationBuilder($translator, $container, true, '/js');
$configuration = $builder->getConfiguration();
- $this->assertEquals('/base/js', $configuration['baseUrl'], 'Expected slashes to be trimmed when generating base URL');
+ $this->assertEquals('/base/js', $configuration['baseUrl'], 'Expected slashes to be trimmed when generating base URL');
}
public function testRootUrlIgnoredIfAppropriate()
{
+ $container = new \Symfony\Component\DependencyInjection\Container();
+ $requestScope = new \Symfony\Component\DependencyInjection\Scope('request');
+
+ // Assets twig funtion only available in request scope, so mock it out
+ $this->setMockAssetTwigExtension($container, '/js');
+
$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request->expects($this->never())
->method('getBaseUrl');
- $container = new \Symfony\Component\DependencyInjection\Container();
- $requestScope = new \Symfony\Component\DependencyInjection\Scope('request');
+
$container->addScope($requestScope);
$container->enterScope('request');
$container->set('request', $request);
@@ -99,11 +129,17 @@ public function testRootUrlIgnoredIfAppropriate()
public function testPathsAdded()
{
+ $container = new \Symfony\Component\DependencyInjection\Container();
+ $requestScope = new \Symfony\Component\DependencyInjection\Scope('request');
+
+ // Assets twig funtion only available in request scope, so mock it out
+ $this->setMockAssetTwigExtension($container, 'base');
+
$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
$translator->expects($this->any())
->method('getLocale')
->will($this->returnValue('fr_FR'));
- $container = new \Symfony\Component\DependencyInjection\Container();
+
$builder = new ConfigurationBuilder($translator, $container, false, 'js');
$builder->setPath('namespace', '/path/to/namespace');
Please sign in to comment.
Something went wrong with that request. Please try again.