url.test.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. use Laravel\Routing\Router;
  3. class URLTest extends PHPUnit_Framework_TestCase {
  4. /**
  5. * Setup the test enviornment.
  6. */
  7. public function setUp()
  8. {
  9. URL::$base = null;
  10. Router::$routes = array();
  11. Router::$names = array();
  12. Router::$uses = array();
  13. Router::$fallback = array();
  14. Config::set('application.url', 'http://localhost');
  15. }
  16. /**
  17. * Destroy the test enviornment.
  18. */
  19. public function tearDown()
  20. {
  21. $_SERVER = array();
  22. Router::$routes = array();
  23. Router::$names = array();
  24. Router::$uses = array();
  25. Router::$fallback = array();
  26. Config::set('application.ssl', true);
  27. Config::set('application.url', '');
  28. Config::set('application.index', 'index.php');
  29. }
  30. /**
  31. * Test the URL::to method.
  32. *
  33. * @group laravel
  34. */
  35. public function testToMethodGeneratesURL()
  36. {
  37. $this->assertEquals('http://localhost/index.php/user/profile', URL::to('user/profile'));
  38. $this->assertEquals('https://localhost/index.php/user/profile', URL::to('user/profile', true));
  39. Config::set('application.index', '');
  40. $this->assertEquals('http://localhost/user/profile', URL::to('user/profile'));
  41. $this->assertEquals('https://localhost/user/profile', URL::to('user/profile', true));
  42. Config::set('application.ssl', false);
  43. $this->assertEquals('http://localhost/user/profile', URL::to('user/profile', true));
  44. }
  45. /**
  46. * Test the URL::to_action method.
  47. *
  48. * @group laravel
  49. */
  50. public function testToActionMethodGeneratesURLToControllerAction()
  51. {
  52. Route::get('foo/bar/(:any?)', 'foo@baz');
  53. $this->assertEquals('http://localhost/index.php/x/y', URL::to_action('x@y'));
  54. $this->assertEquals('http://localhost/index.php/x/y/Taylor', URL::to_action('x@y', array('Taylor')));
  55. $this->assertEquals('http://localhost/index.php/foo/bar', URL::to_action('foo@baz'));
  56. $this->assertEquals('http://localhost/index.php/foo/bar/Taylor', URL::to_action('foo@baz', array('Taylor')));
  57. }
  58. /**
  59. * Test the URL::to_asset method.
  60. *
  61. * @group laravel
  62. */
  63. public function testToAssetGeneratesURLWithoutFrontControllerInURL()
  64. {
  65. $this->assertEquals('http://localhost/image.jpg', URL::to_asset('image.jpg'));
  66. $this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg', true));
  67. Config::set('application.index', '');
  68. $this->assertEquals('http://localhost/image.jpg', URL::to_asset('image.jpg'));
  69. $this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg', true));
  70. Request::foundation()->server->add(array('HTTPS' => 'on'));
  71. $this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg'));
  72. Request::foundation()->server->add(array('HTTPS' => 'off'));
  73. }
  74. /**
  75. * Test the URL::to_route method.
  76. *
  77. * @group laravel
  78. */
  79. public function testToRouteMethodGeneratesURLsToRoutes()
  80. {
  81. Route::get('url/test', array('as' => 'url-test'));
  82. Route::get('url/test/(:any)/(:any?)', array('as' => 'url-test-2'));
  83. Route::get('url/secure/(:any)/(:any?)', array('as' => 'url-test-3', 'https' => true));
  84. $this->assertEquals('http://localhost/index.php/url/test', URL::to_route('url-test'));
  85. $this->assertEquals('http://localhost/index.php/url/test/taylor', URL::to_route('url-test-2', array('taylor')));
  86. $this->assertEquals('https://localhost/index.php/url/secure/taylor', URL::to_route('url-test-3', array('taylor')));
  87. $this->assertEquals('http://localhost/index.php/url/test/taylor/otwell', URL::to_route('url-test-2', array('taylor', 'otwell')));
  88. }
  89. /**
  90. * Test language based URL generation.
  91. *
  92. * @group laravel
  93. */
  94. public function testUrlsGeneratedWithLanguages()
  95. {
  96. Config::set('application.languages', array('sp', 'fr'));
  97. Config::set('application.language', 'sp');
  98. $this->assertEquals('http://localhost/index.php/sp/foo', URL::to('foo'));
  99. $this->assertEquals('http://localhost/foo.jpg', URL::to_asset('foo.jpg'));
  100. Config::set('application.index', '');
  101. $this->assertEquals('http://localhost/sp/foo', URL::to('foo'));
  102. Config::set('application.index', 'index.php');
  103. Config::set('application.language', 'en');
  104. $this->assertEquals('http://localhost/index.php/foo', URL::to('foo'));
  105. Config::set('application.languages', array());
  106. }
  107. }