|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Bundle\TwigBundle\Tests\Extension; |
| 13 | + |
| 14 | +use Symfony\Bundle\TwigBundle\Extension\AssetsExtension; |
| 15 | +use Symfony\Bundle\TwigBundle\Tests\TestCase; |
| 16 | +use Symfony\Component\Routing\RequestContext; |
| 17 | + |
| 18 | +class AssetsExtensionTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @dataProvider provideGetGetAssetUrlArguments |
| 22 | + */ |
| 23 | + public function testGetAssetUrl($path, $packageName, $absolute, $relativeUrl, $expectedUrl, $scheme, $host, $httpPort, $httpsPort) |
| 24 | + { |
| 25 | + $helper = $this->createHelperMock($path, $packageName, $relativeUrl); |
| 26 | + $container = $this->createContainerMock($helper); |
| 27 | + |
| 28 | + $context = $this->createRequestContextMock($scheme, $host, $httpPort, $httpsPort); |
| 29 | + |
| 30 | + $extension = new AssetsExtension($container, $context); |
| 31 | + $this->assertEquals($expectedUrl, $extension->getAssetUrl($path, $packageName, $absolute)); |
| 32 | + } |
| 33 | + |
| 34 | + public function testGetAssetWithtoutHost() |
| 35 | + { |
| 36 | + $path = '/path/to/asset'; |
| 37 | + $packageName = null; |
| 38 | + $relativeUrl = '/bundle-name/path/to/asset'; |
| 39 | + |
| 40 | + $helper = $this->createHelperMock($path, $packageName, $relativeUrl); |
| 41 | + $container = $this->createContainerMock($helper); |
| 42 | + |
| 43 | + $context = $this->createRequestContextMock('http', '', 80, 443); |
| 44 | + |
| 45 | + $extension = new AssetsExtension($container, $context); |
| 46 | + $this->assertEquals($relativeUrl, $extension->getAssetUrl($path, $packageName, true)); |
| 47 | + } |
| 48 | + |
| 49 | + public function provideGetGetAssetUrlArguments() |
| 50 | + { |
| 51 | + return array( |
| 52 | + array('/path/to/asset', 'package-name', false, '/bundle-name/path/to/asset', '/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null), |
| 53 | + array('/path/to/asset', 'package-name', false, 'http://subdomain.symfony.com/bundle-name/path/to/asset', 'http://subdomain.symfony.com/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null), |
| 54 | + array('/path/to/asset', null, false, '/bundle-name/path/to/asset', '/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null), |
| 55 | + array('/path/to/asset', 'package-name', true, '/bundle-name/path/to/asset', 'http://symfony.com/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null), |
| 56 | + array('/path/to/asset', 'package-name', true, 'http://subdomain.symfony.com/bundle-name/path/to/asset', 'http://subdomain.symfony.com/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null), |
| 57 | + array('/path/to/asset', null, true, '/bundle-name/path/to/asset', 'https://symfony.com:92/bundle-name/path/to/asset', 'https', 'symfony.com', null, 92), |
| 58 | + array('/path/to/asset', null, true, '/bundle-name/path/to/asset', 'http://symfony.com:660/bundle-name/path/to/asset', 'http', 'symfony.com', 660, null), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + private function createRequestContextMock($scheme, $host, $httpPort, $httpsPort) |
| 63 | + { |
| 64 | + $context = $this->getMockBuilder('Symfony\Component\Routing\RequestContext') |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->getMock(); |
| 67 | + $context->expects($this->any()) |
| 68 | + ->method('getScheme') |
| 69 | + ->will($this->returnValue($scheme)); |
| 70 | + $context->expects($this->any()) |
| 71 | + ->method('getHost') |
| 72 | + ->will($this->returnValue($host)); |
| 73 | + $context->expects($this->any()) |
| 74 | + ->method('getHttpPort') |
| 75 | + ->will($this->returnValue($httpPort)); |
| 76 | + $context->expects($this->any()) |
| 77 | + ->method('getHttpsPort') |
| 78 | + ->will($this->returnValue($httpsPort)); |
| 79 | + |
| 80 | + return $context; |
| 81 | + } |
| 82 | + |
| 83 | + private function createContainerMock($helper) |
| 84 | + { |
| 85 | + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
| 86 | + $container->expects($this->any()) |
| 87 | + ->method('get') |
| 88 | + ->with('templating.helper.assets') |
| 89 | + ->will($this->returnValue($helper)); |
| 90 | + |
| 91 | + return $container; |
| 92 | + } |
| 93 | + |
| 94 | + private function createHelperMock($path, $packageName, $returnValue) |
| 95 | + { |
| 96 | + $helper = $this->getMockBuilder('Symfony\Component\Templating\Helper\CoreAssetsHelper') |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->getMock(); |
| 99 | + $helper->expects($this->any()) |
| 100 | + ->method('getUrl') |
| 101 | + ->with($path, $packageName) |
| 102 | + ->will($this->returnValue($returnValue)); |
| 103 | + |
| 104 | + return $helper; |
| 105 | + } |
| 106 | +} |
0 commit comments