AssetsTest.php 4.4 KB

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