AssetsTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Components\Templating\Helper;
  11. use Symfony\Components\Templating\Helper\AssetsHelper;
  12. class AssetsHelperTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $helper = new AssetsHelper('foo', 'http://www.example.com', 'abcd');
  17. $this->assertEquals('/foo/', $helper->getBasePath(), '__construct() takes a base path as its first argument');
  18. $this->assertEquals(array('http://www.example.com'), $helper->getBaseURLs(), '__construct() takes a base URL as its second argument');
  19. $this->assertEquals('abcd', $helper->getVersion(), '__construct() takes a version as its thrid argument');
  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 testGetSetVersion()
  34. {
  35. $helper = new AssetsHelper();
  36. $helper->setVersion('foo');
  37. $this->assertEquals('foo', $helper->getVersion(), '->setVersion() sets the version');
  38. }
  39. public function testSetGetBaseURLs()
  40. {
  41. $helper = new AssetsHelper();
  42. $helper->setBaseURLs('http://www.example.com/');
  43. $this->assertEquals(array('http://www.example.com'), $helper->getBaseURLs(), '->setBaseURLs() removes the / at the of an absolute base path');
  44. $helper->setBaseURLs(array('http://www1.example.com/', 'http://www2.example.com/'));
  45. $URLs = array();
  46. for ($i = 0; $i < 20; $i++)
  47. {
  48. $URLs[] = $helper->getBaseURL($i);
  49. }
  50. $URLs = array_values(array_unique($URLs));
  51. sort($URLs);
  52. $this->assertEquals(array('http://www1.example.com', 'http://www2.example.com'), $URLs, '->getBaseURL() returns a random base URL if several are given');
  53. $helper->setBaseURLs('');
  54. $this->assertEquals('', $helper->getBaseURL(1), '->getBaseURL() returns an empty string if no base URL exist');
  55. }
  56. public function testGetUrl()
  57. {
  58. $helper = new AssetsHelper();
  59. $this->assertEquals('http://example.com/foo.js', $helper->getUrl('http://example.com/foo.js'), '->getUrl() does nothing if an absolute URL is given');
  60. $helper = new AssetsHelper();
  61. $this->assertEquals('/foo.js', $helper->getUrl('foo.js'), '->getUrl() appends a / on relative paths');
  62. $this->assertEquals('/foo.js', $helper->getUrl('/foo.js'), '->getUrl() does nothing on absolute paths');
  63. $helper = new AssetsHelper('/foo');
  64. $this->assertEquals('/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() appends the basePath on relative paths');
  65. $this->assertEquals('/foo.js', $helper->getUrl('/foo.js'), '->getUrl() does not append the basePath on absolute paths');
  66. $helper = new AssetsHelper(null, 'http://assets.example.com/');
  67. $this->assertEquals('http://assets.example.com/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL');
  68. $this->assertEquals('http://assets.example.com/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL');
  69. $helper = new AssetsHelper(null, 'http://www.example.com/foo');
  70. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL with a path');
  71. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL with a path');
  72. $helper = new AssetsHelper('/foo', 'http://www.example.com/');
  73. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
  74. $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');
  75. $helper = new AssetsHelper('/bar', 'http://www.example.com/foo');
  76. $this->assertEquals('http://www.example.com/foo/bar/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
  77. $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');
  78. $helper = new AssetsHelper('/bar', 'http://www.example.com/foo', 'abcd');
  79. $this->assertEquals('http://www.example.com/foo/bar/foo.js?abcd', $helper->getUrl('foo.js'), '->getUrl() appends the version if defined');
  80. }
  81. }