UrlPackageTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Component\Asset\Tests;
  11. use Symfony\Component\Asset\UrlPackage;
  12. use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
  13. use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
  14. use Symfony\Component\Asset\Exception\InvalidArgumentException;
  15. use Symfony\Component\Asset\Exception\LogicException;
  16. class UrlPackageTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @dataProvider getConfigs
  20. */
  21. public function testGetUrl($baseUrls, $format, $path, $expected)
  22. {
  23. $package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format));
  24. $this->assertEquals($expected, $package->getUrl($path));
  25. }
  26. public function getConfigs()
  27. {
  28. return array(
  29. array('http://example.net', '', 'http://example.com/foo', 'http://example.com/foo'),
  30. array('http://example.net', '', 'https://example.com/foo', 'https://example.com/foo'),
  31. array('http://example.net', '', '//example.com/foo', '//example.com/foo'),
  32. array('http://example.com', '', '/foo', 'http://example.com/foo?v1'),
  33. array('http://example.com', '', 'foo', 'http://example.com/foo?v1'),
  34. array('http://example.com/', '', 'foo', 'http://example.com/foo?v1'),
  35. array('http://example.com/foo', '', 'foo', 'http://example.com/foo/foo?v1'),
  36. array('http://example.com/foo/', '', 'foo', 'http://example.com/foo/foo?v1'),
  37. array(array('http://example.com'), '', '/foo', 'http://example.com/foo?v1'),
  38. array(array('http://example.com', 'http://example.net'), '', '/foo', 'http://example.com/foo?v1'),
  39. array(array('http://example.com', 'http://example.net'), '', '/fooa', 'http://example.net/fooa?v1'),
  40. array('http://example.com', 'version-%2$s/%1$s', '/foo', 'http://example.com/version-v1/foo'),
  41. array('http://example.com', 'version-%2$s/%1$s', 'foo', 'http://example.com/version-v1/foo'),
  42. array('http://example.com', 'version-%2$s/%1$s', 'foo/', 'http://example.com/version-v1/foo/'),
  43. array('http://example.com', 'version-%2$s/%1$s', '/foo/', 'http://example.com/version-v1/foo/'),
  44. );
  45. }
  46. /**
  47. * @dataProvider getContextConfigs
  48. */
  49. public function testGetUrlWithContext($secure, $baseUrls, $format, $path, $expected)
  50. {
  51. $package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format), $this->getContext($secure));
  52. $this->assertEquals($expected, $package->getUrl($path));
  53. }
  54. public function getContextConfigs()
  55. {
  56. return array(
  57. array(false, 'http://example.com', '', 'foo', 'http://example.com/foo?v1'),
  58. array(false, array('http://example.com'), '', 'foo', 'http://example.com/foo?v1'),
  59. array(false, array('http://example.com', 'https://example.com'), '', 'foo', 'http://example.com/foo?v1'),
  60. array(false, array('http://example.com', 'https://example.com'), '', 'fooa', 'https://example.com/fooa?v1'),
  61. array(false, array('http://example.com/bar'), '', 'foo', 'http://example.com/bar/foo?v1'),
  62. array(false, array('http://example.com/bar/'), '', 'foo', 'http://example.com/bar/foo?v1'),
  63. array(false, array('//example.com/bar/'), '', 'foo', '//example.com/bar/foo?v1'),
  64. array(true, array('http://example.com'), '', 'foo', 'http://example.com/foo?v1'),
  65. array(true, array('http://example.com', 'https://example.com'), '', 'foo', 'https://example.com/foo?v1'),
  66. );
  67. }
  68. /**
  69. * @expectedException LogicException
  70. */
  71. public function testNoBaseUrls()
  72. {
  73. new UrlPackage(array(), new EmptyVersionStrategy());
  74. }
  75. /**
  76. * @expectedException InvalidArgumentException
  77. */
  78. public function testWrongBaseUrl()
  79. {
  80. new UrlPackage(array('not-a-url'), new EmptyVersionStrategy());
  81. }
  82. private function getContext($secure)
  83. {
  84. $context = $this->getMock('Symfony\Component\Asset\Context\ContextInterface');
  85. $context->expects($this->any())->method('isSecure')->will($this->returnValue($secure));
  86. return $context;
  87. }
  88. }