url.test.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  73. /**
  74. * Test the URL::to_route method.
  75. *
  76. * @group laravel
  77. */
  78. public function testToRouteMethodGeneratesURLsToRoutes()
  79. {
  80. Route::get('url/test', array('as' => 'url-test'));
  81. Route::get('url/test/(:any)/(:any?)', array('as' => 'url-test-2'));
  82. Route::get('url/secure/(:any)/(:any?)', array('as' => 'url-test-3', 'https' => true));
  83. $this->assertEquals('http://localhost/index.php/url/test', URL::to_route('url-test'));
  84. $this->assertEquals('http://localhost/index.php/url/test/taylor', URL::to_route('url-test-2', array('taylor')));
  85. $this->assertEquals('https://localhost/index.php/url/secure/taylor', URL::to_route('url-test-3', array('taylor')));
  86. $this->assertEquals('http://localhost/index.php/url/test/taylor/otwell', URL::to_route('url-test-2', array('taylor', 'otwell')));
  87. }
  88. }