AssetsHelperTest.php 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Templating\Helper;
  11. use Symfony\Component\Templating\Asset\AssetPackage;
  12. use Symfony\Component\Templating\Helper\AssetsHelper;
  13. class AssetsHelperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $helper = new AssetsHelper('foo', 'http://www.example.com', 'abcd');
  18. $this->assertEquals('/foo/', $helper->getBasePath(), '__construct() takes a base path as its first argument');
  19. $this->assertEquals(new AssetPackage('http://www.example.com', 'abcd'), $helper->getPackage(), '->__construct() creates a default asset package');
  20. }
  21. public function testGetSetBasePath()
  22. {
  23. $helper = new AssetsHelper();
  24. $helper->setBasePath('foo/');
  25. $this->assertEquals('/foo/', $helper->getBasePath(), '->setBasePath() prepends a / if needed');
  26. $helper->setBasePath('/foo');
  27. $this->assertEquals('/foo/', $helper->getBasePath(), '->setBasePath() appends a / is needed');
  28. $helper->setBasePath('');
  29. $this->assertEquals('/', $helper->getBasePath(), '->setBasePath() returns / if no base path is defined');
  30. $helper->setBasePath('0');
  31. $this->assertEquals('/0/', $helper->getBasePath(), '->setBasePath() returns /0/ if 0 is given');
  32. }
  33. public function testGetVersion()
  34. {
  35. $helper = new AssetsHelper(null, array(), 'foo');
  36. $this->assertEquals('foo', $helper->getVersion(), '->getVersion() returns the version');
  37. }
  38. public function testGetUrl()
  39. {
  40. $helper = new AssetsHelper();
  41. $this->assertEquals('http://example.com/foo.js', $helper->getUrl('http://example.com/foo.js'), '->getUrl() does nothing if an absolute URL is given');
  42. $helper = new AssetsHelper();
  43. $this->assertEquals('/foo.js', $helper->getUrl('foo.js'), '->getUrl() appends a / on relative paths');
  44. $this->assertEquals('/foo.js', $helper->getUrl('/foo.js'), '->getUrl() does nothing on absolute paths');
  45. $helper = new AssetsHelper('/foo');
  46. $this->assertEquals('/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() appends the basePath on relative paths');
  47. $this->assertEquals('/foo.js', $helper->getUrl('/foo.js'), '->getUrl() does not append the basePath on absolute paths');
  48. $helper = new AssetsHelper(null, 'http://assets.example.com/');
  49. $this->assertEquals('http://assets.example.com/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL');
  50. $this->assertEquals('http://assets.example.com/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL');
  51. $helper = new AssetsHelper(null, 'http://www.example.com/foo');
  52. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL with a path');
  53. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL with a path');
  54. $helper = new AssetsHelper('/foo', 'http://www.example.com/');
  55. $this->assertEquals('http://www.example.com/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
  56. $this->assertEquals('http://www.example.com/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL but not the base path on absolute paths');
  57. $helper = new AssetsHelper('/bar', 'http://www.example.com/foo');
  58. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
  59. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL but not the base path on absolute paths');
  60. $helper = new AssetsHelper('/bar', 'http://www.example.com/foo', 'abcd');
  61. $this->assertEquals('http://www.example.com/foo/foo.js?abcd', $helper->getUrl('foo.js'), '->getUrl() appends the version if defined');
  62. }
  63. public function testGetUrlLeavesProtocolRelativePathsUntouched()
  64. {
  65. $helper = new AssetsHelper(null, 'http://foo.com');
  66. $this->assertEquals('//bar.com/asset', $helper->getUrl('//bar.com/asset'));
  67. }
  68. }