url.test.php 4.1 KB

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