url.test.php 4.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

use Laravel\Routing\Router;

class URLTest extends PHPUnit_Framework_TestCase {

	/**
	 * Setup the test enviornment.
	 */
	public function setUp()
	{
		URL::$base = null;
		Router::$routes = array();
		Router::$names = array();
		Router::$uses = array();
		Router::$fallback = array();
		Config::set('application.url', 'http://localhost');
18
		Config::set('application.index', 'index.php');
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	}

	/**
	 * Destroy the test enviornment.
	 */
	public function tearDown()
	{
		$_SERVER = array();
		Router::$routes = array();
		Router::$names = array();
		Router::$uses = array();
		Router::$fallback = array();
		Config::set('application.ssl', true);
		Config::set('application.url', '');
		Config::set('application.index', 'index.php');
	}

	/**
	 * Test the URL::to method.
	 *
	 * @group laravel
	 */
	public function testToMethodGeneratesURL()
	{
		$this->assertEquals('http://localhost/index.php/user/profile', URL::to('user/profile'));
		$this->assertEquals('https://localhost/index.php/user/profile', URL::to('user/profile', true));

		Config::set('application.index', '');

		$this->assertEquals('http://localhost/user/profile', URL::to('user/profile'));
		$this->assertEquals('https://localhost/user/profile', URL::to('user/profile', true));

		Config::set('application.ssl', false);

		$this->assertEquals('http://localhost/user/profile', URL::to('user/profile', true));
	}

	/**
	 * Test the URL::to_action method.
	 *
	 * @group laravel
	 */
	public function testToActionMethodGeneratesURLToControllerAction()
	{
		Route::get('foo/bar/(:any?)', 'foo@baz');
		$this->assertEquals('http://localhost/index.php/x/y', URL::to_action('x@y'));
		$this->assertEquals('http://localhost/index.php/x/y/Taylor', URL::to_action('x@y', array('Taylor')));
		$this->assertEquals('http://localhost/index.php/foo/bar', URL::to_action('foo@baz'));
		$this->assertEquals('http://localhost/index.php/foo/bar/Taylor', URL::to_action('foo@baz', array('Taylor')));
	}

	/**
	 * Test the URL::to_asset method.
	 *
	 * @group laravel
	 */
	public function testToAssetGeneratesURLWithoutFrontControllerInURL()
	{
		$this->assertEquals('http://localhost/image.jpg', URL::to_asset('image.jpg'));
		$this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg', true));

		Config::set('application.index', '');

		$this->assertEquals('http://localhost/image.jpg', URL::to_asset('image.jpg'));
		$this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg', true));

		Request::foundation()->server->add(array('HTTPS' => 'on'));

		$this->assertEquals('https://localhost/image.jpg', URL::to_asset('image.jpg'));
88 89

		Request::foundation()->server->add(array('HTTPS' => 'off'));
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	}

	/**
	 * Test the URL::to_route method.
	 *
	 * @group laravel
	 */
	public function testToRouteMethodGeneratesURLsToRoutes()
	{
		Route::get('url/test', array('as' => 'url-test'));
		Route::get('url/test/(:any)/(:any?)', array('as' => 'url-test-2'));
		Route::get('url/secure/(:any)/(:any?)', array('as' => 'url-test-3', 'https' => true));

		$this->assertEquals('http://localhost/index.php/url/test', URL::to_route('url-test'));
		$this->assertEquals('http://localhost/index.php/url/test/taylor', URL::to_route('url-test-2', array('taylor')));
		$this->assertEquals('https://localhost/index.php/url/secure/taylor', URL::to_route('url-test-3', array('taylor')));
		$this->assertEquals('http://localhost/index.php/url/test/taylor/otwell', URL::to_route('url-test-2', array('taylor', 'otwell')));
	}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

	/**
	 * Test language based URL generation.
	 *
	 * @group laravel
	 */
	public function testUrlsGeneratedWithLanguages()
	{
		Config::set('application.languages', array('sp', 'fr'));
		Config::set('application.language', 'sp');
		$this->assertEquals('http://localhost/index.php/sp/foo', URL::to('foo'));
		$this->assertEquals('http://localhost/foo.jpg', URL::to_asset('foo.jpg'));

		Config::set('application.index', '');
		$this->assertEquals('http://localhost/sp/foo', URL::to('foo'));

		Config::set('application.index', 'index.php');
		Config::set('application.language', 'en');
		$this->assertEquals('http://localhost/index.php/foo', URL::to('foo'));
		Config::set('application.languages', array());
	}

131
}