From feefa8d9c441086a42306179855cbc060bda46bc Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 13 Jul 2012 03:05:55 +0200 Subject: [PATCH] Fix PHP errors in test cases related to now using Symfony's HttpFoundation component. --- laravel/tests/cases/controller.test.php | 2 +- laravel/tests/cases/redirect.test.php | 20 +++---- laravel/tests/cases/request.test.php | 78 +++++++++++++++++-------- laravel/tests/cases/response.test.php | 11 ++-- laravel/tests/cases/uri.test.php | 22 ++++++- 5 files changed, 91 insertions(+), 42 deletions(-) diff --git a/laravel/tests/cases/controller.test.php b/laravel/tests/cases/controller.test.php index 9147bad9..1577ece3 100644 --- a/laravel/tests/cases/controller.test.php +++ b/laravel/tests/cases/controller.test.php @@ -189,7 +189,7 @@ public function testRestfulControllersRespondWithRestfulMethods() $_SERVER['REQUEST_METHOD'] = 'PUT'; - $this->assertEquals(404, Controller::call('restful@index')->status); + $this->assertEquals(404, Controller::call('restful@index')->status()); $_SERVER['REQUEST_METHOD'] = 'POST'; diff --git a/laravel/tests/cases/redirect.test.php b/laravel/tests/cases/redirect.test.php index 2e6fb193..4f0897d7 100644 --- a/laravel/tests/cases/redirect.test.php +++ b/laravel/tests/cases/redirect.test.php @@ -39,18 +39,18 @@ public function testSimpleRedirectSetsCorrectHeaders() { $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 @@ public function testRedirectsCanBeGeneratedForNamedRoutes() 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')); } /** diff --git a/laravel/tests/cases/request.test.php b/laravel/tests/cases/request.test.php index d3e6b479..4641a532 100644 --- a/laravel/tests/cases/request.test.php +++ b/laravel/tests/cases/request.test.php @@ -1,5 +1,7 @@ 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 @@ public function tearDown() */ 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 @@ public function testMethodReturnsTheHTTPRequestMethod() */ 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 @@ public function testServerMethodReturnsFromServerArray() */ 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 @@ public function testProtocolMethodReturnsProtocol() */ 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 @@ public function testAjaxMethodIndicatesWhenAjax() { $this->assertFalse(Request::ajax()); - $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; + $this->setServerVar('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'); $this->assertTrue(Request::ajax()); } diff --git a/laravel/tests/cases/response.test.php b/laravel/tests/cases/response.test.php index 5907eebd..11e48e74 100644 --- a/laravel/tests/cases/response.test.php +++ b/laravel/tests/cases/response.test.php @@ -12,8 +12,9 @@ public function testMakeMethodProperlySetsContent() $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 @@ public function testErrorMethodSetsContentToErrorView() { $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 @@ public function testHeaderMethodSetsValueInHeaderArray() { $response = Response::make('')->header('foo', 'bar'); - $this->assertEquals('bar', $response->headers['foo']); + $this->assertEquals('bar', $response->headers()->get('foo')); } /** @@ -82,7 +83,7 @@ public function testStatusMethodSetsStatusCode() { $response = Response::make('')->status(404); - $this->assertEquals(404, $response->status); + $this->assertEquals(404, $response->status()); } } \ No newline at end of file diff --git a/laravel/tests/cases/uri.test.php b/laravel/tests/cases/uri.test.php index be779ecf..fc64b3fb 100644 --- a/laravel/tests/cases/uri.test.php +++ b/laravel/tests/cases/uri.test.php @@ -1,5 +1,7 @@ setRequestUri($uri); + $this->assertEquals($expectation, URI::current()); } @@ -31,7 +49,7 @@ public function testCorrectURIIsReturnedByCurrentMethod($uri, $expectation) */ 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));