url.test.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. Router::$routes = array();
  10. Router::$names = array();
  11. Router::$uses = array();
  12. Router::$fallback = array();
  13. Config::set('application.url', 'http://localhost');
  14. }
  15. /**
  16. * Destroy the test enviornment.
  17. */
  18. public function tearDown()
  19. {
  20. $_SERVER = array();
  21. Router::$routes = array();
  22. Router::$names = array();
  23. Router::$uses = array();
  24. Router::$fallback = array();
  25. Config::set('application.ssl', true);
  26. Config::set('application.url', '');
  27. Config::set('application.index', 'index.php');
  28. }
  29. /**
  30. * Test the URL::to method.
  31. *
  32. * @group laravel
  33. */
  34. public function testToMethodGeneratesURL()
  35. {
  36. $this->assertEquals('http://localhost/index.php/user/profile', URL::to('user/profile'));
  37. $this->assertEquals('https://localhost/index.php/user/profile', URL::to('user/profile', true));
  38. Config::set('application.index', '');
  39. $this->assertEquals('http://localhost/user/profile', URL::to('user/profile'));
  40. $this->assertEquals('https://localhost/user/profile', URL::to('user/profile', true));
  41. Config::set('application.ssl', false);
  42. $this->assertEquals('http://localhost/user/profile', URL::to('user/profile', true));
  43. }
  44. /**
  45. * Test the URL::to_action method.
  46. *
  47. * @group laravel
  48. */
  49. public function testToActionMethodGeneratesURLToControllerAction()
  50. {
  51. Route::get('foo/bar/(:any?)', 'foo@baz');
  52. $this->assertEquals('http://localhost/index.php/x/y', URL::to_action('x@y'));
  53. $this->assertEquals('http://localhost/index.php/x/y/Taylor', URL::to_action('x@y', array('Taylor')));
  54. $this->assertEquals('http://localhost/index.php/foo/bar', URL::to_action('foo@baz'));
  55. $this->assertEquals('http://localhost/index.php/foo/bar/Taylor', URL::to_action('foo@baz', array('Taylor')));
  56. }
  57. /**
  58. * Test the URL::to_asset method.
  59. *
  60. * @group laravel
  61. */
  62. public function testToAssetGeneratesURLWithoutFrontControllerInURL()
  63. {
  64. $this->assertEquals('http://localhost/image.jpg', URL::to_asset('image.jpg'));
  65. $this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg', true));
  66. Config::set('application.index', '');
  67. $this->assertEquals('http://localhost/image.jpg', URL::to_asset('image.jpg'));
  68. $this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg', true));
  69. $_SERVER['HTTPS'] = 'on';
  70. $this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg'));
  71. }
  72. /**
  73. * Test the URL::to_route method.
  74. *
  75. * @group laravel
  76. */
  77. public function testToRouteMethodGeneratesURLsToRoutes()
  78. {
  79. Route::get('url/test', array('as' => 'url-test'));
  80. Route::get('url/test/(:any)/(:any?)', array('as' => 'url-test-2'));
  81. Route::get('url/secure/(:any)/(:any?)', array('as' => 'url-test-3', 'https' => true));
  82. $this->assertEquals('http://localhost/index.php/url/test', URL::to_route('url-test'));
  83. $this->assertEquals('http://localhost/index.php/url/test/taylor', URL::to_route('url-test-2', array('taylor')));
  84. $this->assertEquals('https://localhost/index.php/url/secure/taylor', URL::to_route('url-test-3', array('taylor')));
  85. $this->assertEquals('http://localhost/index.php/url/test/taylor/otwell', URL::to_route('url-test-2', array('taylor', 'otwell')));
  86. }
  87. }