AssetsTest.php 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Component\Templating\Helper;
  11. use Symfony\Component\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. $URLs[] = $helper->getBaseURL($i);
  48. }
  49. $URLs = array_values(array_unique($URLs));
  50. sort($URLs);
  51. $this->assertEquals(array('http://www1.example.com', 'http://www2.example.com'), $URLs, '->getBaseURL() returns a random base URL if several are given');
  52. $helper->setBaseURLs('');
  53. $this->assertEquals('', $helper->getBaseURL(1), '->getBaseURL() returns an empty string if no base URL exist');
  54. }
  55. public function testGetUrl()
  56. {
  57. $helper = new AssetsHelper();
  58. $this->assertEquals('http://example.com/foo.js', $helper->getUrl('http://example.com/foo.js'), '->getUrl() does nothing if an absolute URL is given');
  59. $helper = new AssetsHelper();
  60. $this->assertEquals('/foo.js', $helper->getUrl('foo.js'), '->getUrl() appends a / on relative paths');
  61. $this->assertEquals('/foo.js', $helper->getUrl('/foo.js'), '->getUrl() does nothing on absolute paths');
  62. $helper = new AssetsHelper('/foo');
  63. $this->assertEquals('/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() appends the basePath on relative paths');
  64. $this->assertEquals('/foo.js', $helper->getUrl('/foo.js'), '->getUrl() does not append the basePath on absolute paths');
  65. $helper = new AssetsHelper(null, 'http://assets.example.com/');
  66. $this->assertEquals('http://assets.example.com/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL');
  67. $this->assertEquals('http://assets.example.com/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL');
  68. $helper = new AssetsHelper(null, 'http://www.example.com/foo');
  69. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL with a path');
  70. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL with a path');
  71. $helper = new AssetsHelper('/foo', 'http://www.example.com/');
  72. $this->assertEquals('http://www.example.com/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
  73. $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');
  74. $helper = new AssetsHelper('/bar', 'http://www.example.com/foo');
  75. $this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
  76. $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');
  77. $helper = new AssetsHelper('/bar', 'http://www.example.com/foo', 'abcd');
  78. $this->assertEquals('http://www.example.com/foo/foo.js?abcd', $helper->getUrl('foo.js'), '->getUrl() appends the version if defined');
  79. }
  80. }