Browse Source

Fix PHP errors in test cases related to now using Symfony's HttpFoundation component.

Franz Liedke 12 years ago
parent
commit
feefa8d9c4

+ 1 - 1
laravel/tests/cases/controller.test.php

@@ -189,7 +189,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase {
 
 		$_SERVER['REQUEST_METHOD'] = 'PUT';
 
-		$this->assertEquals(404, Controller::call('restful@index')->status);
+		$this->assertEquals(404, Controller::call('restful@index')->status());
 
 		$_SERVER['REQUEST_METHOD'] = 'POST';
 

+ 10 - 10
laravel/tests/cases/redirect.test.php

@@ -39,18 +39,18 @@ class RedirectTest extends PHPUnit_Framework_TestCase {
 	{
 		$redirect = Redirect::to('user/profile');
 
-		$this->assertEquals(302, $redirect->status);
-		$this->assertEquals('http://localhost/user/profile', $redirect->headers['location']);
+		$this->assertEquals(302, $redirect->status());
+		$this->assertEquals('http://localhost/user/profile', $redirect->headers()->get('location'));
 
 		$redirect = Redirect::to('user/profile', 301, true);
 
-		$this->assertEquals(301, $redirect->status);
-		$this->assertEquals('https://localhost/user/profile', $redirect->headers['location']);
+		$this->assertEquals(301, $redirect->status());
+		$this->assertEquals('https://localhost/user/profile', $redirect->headers()->get('location'));
 
 		$redirect = Redirect::to_secure('user/profile', 301);
 
-		$this->assertEquals(301, $redirect->status);
-		$this->assertEquals('https://localhost/user/profile', $redirect->headers['location']);
+		$this->assertEquals(301, $redirect->status());
+		$this->assertEquals('https://localhost/user/profile', $redirect->headers()->get('location'));
 	}
 
 	/**
@@ -64,10 +64,10 @@ class RedirectTest extends PHPUnit_Framework_TestCase {
 		Route::get('redirect/(:any)/(:any)', array('as' => 'redirect-2'));
 		Route::get('secure/redirect', array('https' => true, 'as' => 'redirect-3'));
 
-		$this->assertEquals(301, Redirect::to_route('redirect', array(), 301, true)->status);
-		$this->assertEquals('http://localhost/redirect', Redirect::to_route('redirect')->headers['location']);
-		$this->assertEquals('https://localhost/secure/redirect', Redirect::to_route('redirect-3', array(), 302)->headers['location']);
-		$this->assertEquals('http://localhost/redirect/1/2', Redirect::to_route('redirect-2', array('1', '2'))->headers['location']);
+		$this->assertEquals(301, Redirect::to_route('redirect', array(), 301, true)->status());
+		$this->assertEquals('http://localhost/redirect', Redirect::to_route('redirect')->headers()->get('location'));
+		$this->assertEquals('https://localhost/secure/redirect', Redirect::to_route('redirect-3', array(), 302)->headers()->get('location'));
+		$this->assertEquals('http://localhost/redirect/1/2', Redirect::to_route('redirect-2', array('1', '2'))->headers()->get('location'));
 	}
 
 	/**

+ 54 - 24
laravel/tests/cases/request.test.php

@@ -1,5 +1,7 @@
 <?php
 
+use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;
+
 class SessionPayloadTokenStub {
 
 	public function token() { return 'Taylor'; }
@@ -19,6 +21,46 @@ class RequestTest extends PHPUnit_Framework_TestCase {
 		Session::$instance = null;
 	}
 
+	/**
+	 * Set one of the $_SERVER variables.
+	 *
+	 * @param string  $key
+	 * @param string  $value
+	 */
+	protected function setServerVar($key, $value)
+	{
+		$_SERVER[$key] = $value;
+
+		$this->restartRequest();
+	}
+
+	/**
+	 * Set one of the $_POST variables.
+	 *
+	 * @param string  $key
+	 * @param string  $value
+	 */
+	protected function setPostVar($key, $value)
+	{
+		$_POST[$key] = $value;
+
+		$this->restartRequest();
+	}
+
+	/**
+	 * Reinitialize the global request.
+	 * 
+	 * @return void
+	 */
+	protected function restartRequest()
+	{
+		// FIXME: Ugly hack, but old contents from previous requests seem to
+		// trip up the Foundation class.
+		$_FILES = array();
+
+		Request::$foundation = RequestFoundation::createFromGlobals();
+	}
+
 	/**
 	 * Test the Request::method method.
 	 *
@@ -26,11 +68,11 @@ class RequestTest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testMethodReturnsTheHTTPRequestMethod()
 	{
-		$_SERVER['REQUEST_METHOD'] = 'POST';
+		$this->setServerVar('REQUEST_METHOD', 'POST');
 
 		$this->assertEquals('POST', Request::method());
 
-		$_POST[Request::spoofer] = 'PUT';
+		$this->setPostVar(Request::spoofer, 'PUT');
 
 		$this->assertEquals('PUT', Request::method());
 	}
@@ -42,7 +84,8 @@ class RequestTest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testServerMethodReturnsFromServerArray()
 	{
-		$_SERVER = array('TEST' => 'something', 'USER' => array('NAME' => 'taylor'));
+		$this->setServerVar('TEST', 'something');
+		$this->setServerVar('USER', array('NAME' => 'taylor'));
 
 		$this->assertEquals('something', Request::server('test'));
 		$this->assertEquals('taylor', Request::server('user.name'));
@@ -55,33 +98,20 @@ class RequestTest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testIPMethodReturnsClientIPAddress()
 	{
-		$_SERVER['REMOTE_ADDR'] = 'something';
+		$this->setServerVar('REMOTE_ADDR', 'something');
 		$this->assertEquals('something', Request::ip());
 
-		$_SERVER['HTTP_CLIENT_IP'] = 'something';
+		$this->setServerVar('HTTP_CLIENT_IP', 'something');
 		$this->assertEquals('something', Request::ip());
 
-		$_SERVER['HTTP_X_FORWARDED_FOR'] = 'something';
+		$this->setServerVar('HTTP_CLIENT_IP', 'something');
 		$this->assertEquals('something', Request::ip());
 
 		$_SERVER = array();
+		$this->restartRequest();
 		$this->assertEquals('0.0.0.0', Request::ip());
 	}
 
-	/**
-	 * Test the Request::protocol method.
-	 *
-	 * @group laravel
-	 */
-	public function testProtocolMethodReturnsProtocol()
-	{
-		$_SERVER['SERVER_PROTOCOL'] = 'taylor';
-		$this->assertEquals('taylor', Request::protocol());
-
-		unset($_SERVER['SERVER_PROTOCOL']);
-		$this->assertEquals('HTTP/1.1', Request::protocol());
-	}
-
 	/**
 	 * Test the Request::secure method.
 	 *
@@ -89,11 +119,11 @@ class RequestTest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testSecureMethodsIndicatesIfHTTPS()
 	{
-		$_SERVER['HTTPS'] = 'on';
-
+		$this->setServerVar('HTTPS', 'on');
+		
 		$this->assertTrue(Request::secure());
 
-		$_SERVER['HTTPS'] = 'off';
+		$this->setServerVar('HTTPS', 'off');
 
 		$this->assertFalse(Request::secure());
 	}
@@ -107,7 +137,7 @@ class RequestTest extends PHPUnit_Framework_TestCase {
 	{
 		$this->assertFalse(Request::ajax());
 
-		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
+		$this->setServerVar('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
 
 		$this->assertTrue(Request::ajax());
 	}

+ 6 - 5
laravel/tests/cases/response.test.php

@@ -12,8 +12,9 @@ class ResponseTest extends PHPUnit_Framework_TestCase {
 		$response = Response::make('foo', 201, array('bar' => 'baz'));
 
 		$this->assertEquals('foo', $response->content);
-		$this->assertEquals(201, $response->status);
-		$this->assertEquals(array('bar' => 'baz'), $response->headers);
+		$this->assertEquals(201, $response->status());
+		$this->assertArrayHasKey('bar', $response->headers()->all());
+		$this->assertEquals('baz', $response->headers()->get('bar'));
 	}
 
 	/**
@@ -38,7 +39,7 @@ class ResponseTest extends PHPUnit_Framework_TestCase {
 	{
 		$response = Response::error('404', array('name' => 'Taylor'));
 
-		$this->assertEquals(404, $response->status);
+		$this->assertEquals(404, $response->status());
 		$this->assertEquals('error.404', $response->content->view);
 		$this->assertEquals('Taylor', $response->content->data['name']);
 	}
@@ -70,7 +71,7 @@ class ResponseTest extends PHPUnit_Framework_TestCase {
 	{
 		$response = Response::make('')->header('foo', 'bar');
 
-		$this->assertEquals('bar', $response->headers['foo']);
+		$this->assertEquals('bar', $response->headers()->get('foo'));
 	}
 
 	/**
@@ -82,7 +83,7 @@ class ResponseTest extends PHPUnit_Framework_TestCase {
 	{
 		$response = Response::make('')->status(404);
 
-		$this->assertEquals(404, $response->status);
+		$this->assertEquals(404, $response->status());
 	}
 
 }

+ 20 - 2
laravel/tests/cases/uri.test.php

@@ -1,5 +1,7 @@
 <?php
 
+use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;
+
 class URITest extends PHPUnit_Framework_TestCase {
 
 	/**
@@ -12,6 +14,21 @@ class URITest extends PHPUnit_Framework_TestCase {
 		URI::$segments = array();
 	}
 
+	/**
+	 * Set this request's URI to the given string
+	 * 
+	 * @param string  $uri
+	 */
+	protected function setRequestUri($uri)
+	{
+		// FIXME: Ugly hack, but old contents from previous requests seem to
+		// trip up the Foundation class.
+		$_FILES = array();
+		
+		$_SERVER['REQUEST_URI'] = $uri;
+		Request::$foundation = RequestFoundation::createFromGlobals();
+	}
+
 	/**
 	 * Test the URI::current method.
 	 *
@@ -20,7 +37,8 @@ class URITest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testCorrectURIIsReturnedByCurrentMethod($uri, $expectation)
 	{
-		$_SERVER['REQUEST_URI'] = $uri;
+		$this->setRequestUri($uri);
+
 		$this->assertEquals($expectation, URI::current());
 	}
 
@@ -31,7 +49,7 @@ class URITest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testSegmentMethodReturnsAURISegment()
 	{
-		$_SERVER['REQUEST_URI'] = 'http://localhost/index.php/user/profile';
+		$this->setRequestUri('http://localhost/index.php/user/profile');
 
 		$this->assertEquals('user', URI::segment(1));
 		$this->assertEquals('profile', URI::segment(2));