Skip to content

Commit

Permalink
Merge pull request #26 from Bendihossan/honour-assets-base-url
Browse files Browse the repository at this point in the history
Honour assets base url
  • Loading branch information
Igor Timoshenko committed Apr 3, 2013
2 parents 343fef4 + 6cab4ca commit 53b61f8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
15 changes: 11 additions & 4 deletions Configuration/ConfigurationBuilder.php
Expand Up @@ -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);
}
}

}
48 changes: 42 additions & 6 deletions Tests/Configuration/ConfigurationBuilderTest.php
Expand Up @@ -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')
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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');

Expand Down

0 comments on commit 53b61f8

Please sign in to comment.