AssetsTest.php 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. require_once __DIR__.'/../../../bootstrap.php';
  12. use Symfony\Components\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($helper->getBasePath(), '/foo/', '__construct() takes a base path as its first argument');
  19. $this->assertEquals($helper->getBaseURLs(), array('http://www.example.com'), '__construct() takes a base URL as its second argument');
  20. $this->assertEquals($helper->getVersion(), 'abcd', '__construct() takes a version as its thrid argument');
  21. }
  22. public function testGetSetBasePath()
  23. {
  24. $helper = new AssetsHelper();
  25. $helper->setBasePath('foo/');
  26. $this->assertEquals($helper->getBasePath(), '/foo/', '->setBasePath() prepends a / if needed');
  27. $helper->setBasePath('/foo');
  28. $this->assertEquals($helper->getBasePath(), '/foo/', '->setBasePath() appends a / is needed');
  29. $helper->setBasePath('');
  30. $this->assertEquals($helper->getBasePath(), '/', '->setBasePath() returns / if no base path is defined');
  31. $helper->setBasePath('0');
  32. $this->assertEquals($helper->getBasePath(), '/0/', '->setBasePath() returns /0/ if 0 is given');
  33. }
  34. public function testGetSetVersion()
  35. {
  36. $helper = new AssetsHelper();
  37. $helper->setVersion('foo');
  38. $this->assertEquals($helper->getVersion(), 'foo', '->setVersion() sets the version');
  39. }
  40. public function testSetGetBaseURLs()
  41. {
  42. $helper = new AssetsHelper();
  43. $helper->setBaseURLs('http://www.example.com/');
  44. $this->assertEquals($helper->getBaseURLs(), array('http://www.example.com'), '->setBaseURLs() removes the / at the of an absolute base path');
  45. $helper->setBaseURLs(array('http://www1.example.com/', 'http://www2.example.com/'));
  46. $URLs = array();
  47. for ($i = 0; $i < 20; $i++)
  48. {
  49. $URLs[] = $helper->getBaseURL($i);
  50. }
  51. $URLs = array_values(array_unique($URLs));
  52. sort($URLs);
  53. $this->assertEquals($URLs, array('http://www1.example.com', 'http://www2.example.com'), '->getBaseURL() returns a random base URL if several are given');
  54. $helper->setBaseURLs('');
  55. $this->assertEquals($helper->getBaseURL(1), '', '->getBaseURL() returns an empty string if no base URL exist');
  56. }
  57. public function testGetUrl()
  58. {
  59. $helper = new AssetsHelper();
  60. $this->assertEquals($helper->getUrl('http://example.com/foo.js'), 'http://example.com/foo.js', '->getUrl() does nothing if an absolute URL is given');
  61. $helper = new AssetsHelper();
  62. $this->assertEquals($helper->getUrl('foo.js'), '/foo.js', '->getUrl() appends a / on relative paths');
  63. $this->assertEquals($helper->getUrl('/foo.js'), '/foo.js', '->getUrl() does nothing on absolute paths');
  64. $helper = new AssetsHelper('/foo');
  65. $this->assertEquals($helper->getUrl('foo.js'), '/foo/foo.js', '->getUrl() appends the basePath on relative paths');
  66. $this->assertEquals($helper->getUrl('/foo.js'), '/foo.js', '->getUrl() does not append the basePath on absolute paths');
  67. $helper = new AssetsHelper(null, 'http://assets.example.com/');
  68. $this->assertEquals($helper->getUrl('foo.js'), 'http://assets.example.com/foo.js', '->getUrl() prepends the base URL');
  69. $this->assertEquals($helper->getUrl('/foo.js'), 'http://assets.example.com/foo.js', '->getUrl() prepends the base URL');
  70. $helper = new AssetsHelper(null, 'http://www.example.com/foo');
  71. $this->assertEquals($helper->getUrl('foo.js'), 'http://www.example.com/foo/foo.js', '->getUrl() prepends the base URL with a path');
  72. $this->assertEquals($helper->getUrl('/foo.js'), 'http://www.example.com/foo/foo.js', '->getUrl() prepends the base URL with a path');
  73. $helper = new AssetsHelper('/foo', 'http://www.example.com/');
  74. $this->assertEquals($helper->getUrl('foo.js'), 'http://www.example.com/foo/foo.js', '->getUrl() prepends the base URL and the base path if defined');
  75. $this->assertEquals($helper->getUrl('/foo.js'), 'http://www.example.com/foo.js', '->getUrl() prepends the base URL but not the base path on absolute paths');
  76. $helper = new AssetsHelper('/bar', 'http://www.example.com/foo');
  77. $this->assertEquals($helper->getUrl('foo.js'), 'http://www.example.com/foo/bar/foo.js', '->getUrl() prepends the base URL and the base path if defined');
  78. $this->assertEquals($helper->getUrl('/foo.js'), 'http://www.example.com/foo/foo.js', '->getUrl() prepends the base URL but not the base path on absolute paths');
  79. $helper = new AssetsHelper('/bar', 'http://www.example.com/foo', 'abcd');
  80. $this->assertEquals($helper->getUrl('foo.js'), 'http://www.example.com/foo/bar/foo.js?abcd', '->getUrl() appends the version if defined');
  81. }
  82. }