From d816eb9ef31d3690f57b96e1571b60aaea85fe81 Mon Sep 17 00:00:00 2001 From: crynobone Date: Wed, 1 Aug 2012 06:34:44 +0800 Subject: [PATCH] Refix unit testing for Laravel Signed-off-by: crynobone --- laravel/tests/cases/asset.test.php | 8 +- laravel/tests/cases/auth.test.php | 176 ++++++++++++++++++------ laravel/tests/cases/blade.test.php | 8 +- laravel/tests/cases/bundle.test.php | 6 +- laravel/tests/cases/controller.test.php | 17 ++- laravel/tests/cases/cookie.test.php | 44 +++++- laravel/tests/cases/input.test.php | 2 +- laravel/tests/cases/redirect.test.php | 4 +- laravel/tests/cases/session.test.php | 4 +- laravel/tests/cases/uri.test.php | 20 ++- 10 files changed, 209 insertions(+), 80 deletions(-) diff --git a/laravel/tests/cases/asset.test.php b/laravel/tests/cases/asset.test.php index d62945de..d4214f12 100644 --- a/laravel/tests/cases/asset.test.php +++ b/laravel/tests/cases/asset.test.php @@ -184,13 +184,11 @@ public function testBundleMethodCorrectlySetsTheAssetBundle() */ public function testPathMethodReturnsCorrectPathForABundleAsset() { - Config::set('application.url', 'http://localhost'); - $container = $this->getContainer(); $container->bundle('eloquent'); - $this->assertEquals('http://localhost/bundles/eloquent/foo.jpg', $container->path('foo.jpg')); + $this->assertEquals('/bundles/eloquent/foo.jpg', $container->path('foo.jpg')); } /** @@ -200,11 +198,9 @@ public function testPathMethodReturnsCorrectPathForABundleAsset() */ public function testPathMethodReturnsCorrectPathForAnApplicationAsset() { - Config::set('application.url', 'http://localhost'); - $container = $this->getContainer(); - $this->assertEquals('http://localhost/foo.jpg', $container->path('foo.jpg')); + $this->assertEquals('/foo.jpg', $container->path('foo.jpg')); } /** diff --git a/laravel/tests/cases/auth.test.php b/laravel/tests/cases/auth.test.php index 9f6b3928..16ba428a 100644 --- a/laravel/tests/cases/auth.test.php +++ b/laravel/tests/cases/auth.test.php @@ -1,5 +1,7 @@ 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 Auth::user method. * @@ -54,7 +83,9 @@ public function testUserMethodReturnsCurrentUser() */ public function testCheckMethodReturnsTrueWhenUserIsSet() { - $this->assertTrue(AuthUserReturnsDummy::check()); + $auth = new AuthUserReturnsDummy; + + $this->assertTrue($auth->check()); } /** @@ -64,7 +95,9 @@ public function testCheckMethodReturnsTrueWhenUserIsSet() */ public function testCheckMethodReturnsFalseWhenNoUserIsSet() { - $this->assertFalse(AuthUserReturnsNull::check()); + $auth = new AuthUserReturnsNull; + + $this->assertFalse($auth->check()); } /** @@ -74,7 +107,9 @@ public function testCheckMethodReturnsFalseWhenNoUserIsSet() */ public function testGuestReturnsTrueWhenNoUserIsSet() { - $this->assertTrue(AuthUserReturnsNull::guest()); + $auth = new AuthUserReturnsNull; + + $this->assertTrue($auth->guest()); } /** @@ -84,7 +119,9 @@ public function testGuestReturnsTrueWhenNoUserIsSet() */ public function testGuestReturnsFalseWhenUserIsSet() { - $this->assertFalse(AuthUserReturnsDummy::guest()); + $auth = new AuthUserReturnsDummy; + + $this->assertFalse($auth->guest()); } /** @@ -107,10 +144,12 @@ public function testUserMethodReturnsNullWhenNoUserExistsAndNoRecallerExists() public function testUserReturnsUserByID() { Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver')); - // FIXME: Not sure whether hard-coding the key is a good idea. - Session::$instance->session['data']['laravel_auth_drivers_fluent_login'] = 1; + + Auth::login(1); $this->assertEquals('Taylor Otwell', Auth::user()->name); + + Auth::logout(); } /** @@ -121,8 +160,8 @@ public function testUserReturnsUserByID() public function testNullReturnedWhenUserIDNotValidInteger() { Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver')); - // FIXME: Not sure whether hard-coding the key is a good idea. - Session::$instance->session['data']['laravel_auth_drivers_fluent_login'] = 'asdlkasd'; + + Auth::login('asdlkasd'); $this->assertNull(Auth::user()); } @@ -137,10 +176,13 @@ public function testUserCanBeRecalledViaCookie() Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver')); $cookie = Crypter::encrypt('1|'.Str::random(40)); - Cookie::forever(Config::get('auth.cookie'), $cookie); + Cookie::forever('authloginstub_remember', $cookie); - $this->assertEquals('Taylor Otwell', AuthLoginStub::user()->name); - $this->assertTrue(AuthLoginStub::user() === $_SERVER['auth.login.stub']['user']); + $auth = new AuthLoginStub; + + $this->assertEquals('Taylor Otwell', $auth->user()->name); + + $this->assertTrue($auth->user()->id === $_SERVER['auth.login.stub']['user']); } /** @@ -150,11 +192,11 @@ public function testUserCanBeRecalledViaCookie() */ public function testAttemptMethodReturnsFalseWhenCredentialsAreInvalid() { - $this->assertFalse(Auth::attempt('foo', 'foo')); - $this->assertFalse(Auth::attempt('foo', null)); - $this->assertFalse(Auth::attempt(null, null)); - $this->assertFalse(Auth::attempt('taylor', 'password')); - $this->assertFalse(Auth::attempt('taylor', 232)); + $this->assertFalse(Auth::attempt(array('username' => 'foo', 'password' => 'foo'))); + $this->assertFalse(Auth::attempt(array('username' => 'foo', 'password' => null))); + $this->assertFalse(Auth::attempt(array('username' => null, 'password' => null))); + $this->assertFalse(Auth::attempt(array('username' => 'taylor', 'password' => 'password'))); + $this->assertFalse(Auth::attempt(array('username' => 'taylor', 'password' => 232))); } /** @@ -164,13 +206,22 @@ public function testAttemptMethodReturnsFalseWhenCredentialsAreInvalid() */ public function testAttemptReturnsTrueWhenCredentialsAreCorrect() { - $this->assertTrue(AuthLoginStub::attempt('taylor', 'password1')); - $this->assertEquals('Taylor Otwell', $_SERVER['auth.login.stub']['user']->name); + Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver')); + + $auth = new AuthLoginStub; + + $this->assertTrue($auth->attempt(array('username' => 'taylor', 'password' => 'password1'))); + $this->assertEquals('1', $_SERVER['auth.login.stub']['user']); $this->assertFalse($_SERVER['auth.login.stub']['remember']); - $this->assertTrue(AuthLoginStub::attempt('taylor', 'password1', true)); - $this->assertEquals('Taylor Otwell', $_SERVER['auth.login.stub']['user']->name); + $auth_secure = new AuthLoginStub; + + $this->assertTrue($auth_secure->attempt(array('username' => 'taylor', 'password' => 'password1', 'remember' => true))); + $this->assertEquals('1', $_SERVER['auth.login.stub']['user']); $this->assertTrue($_SERVER['auth.login.stub']['remember']); + + $auth_secure->logout(); + $auth->logout(); } /** @@ -189,9 +240,13 @@ public function testLoginMethodStoresUserKeyInSession() $user = Session::$instance->session['data']['laravel_auth_drivers_fluent_login']; $this->assertEquals(10, $user->id); + + Auth::logout(); + Auth::login(5); $user = Session::$instance->session['data']['laravel_auth_drivers_fluent_login']; $this->assertEquals(5, $user); + Auth::logout(5); } /** @@ -203,20 +258,27 @@ public function testLoginStoresRememberCookieWhenNeeded() { Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver')); + $this->setServerVar('HTTPS', 'on'); + // Set the session vars to make sure remember cookie uses them Config::set('session.path', 'foo'); Config::set('session.domain', 'bar'); Config::set('session.secure', true); - Auth::login(10); - $this->assertTrue(isset(Cookie::$jar[Config::get('auth.cookie')])); + Auth::login(1, true); - $cookie = Cookie::$jar[Config::get('auth.cookie')]['value']; + $this->assertTrue(isset(Cookie::$jar['laravel_auth_drivers_fluent_remember'])); + + $cookie = Cookie::$jar['laravel_auth_drivers_fluent_remember']['value']; $cookie = explode('|', Crypter::decrypt($cookie)); - $this->assertEquals(10, $cookie[0]); - $this->assertEquals('foo', Cookie::$jar[Config::get('auth.cookie')]['path']); - $this->assertEquals('bar', Cookie::$jar[Config::get('auth.cookie')]['domain']); - $this->assertTrue(Cookie::$jar[Config::get('auth.cookie')]['secure']); + $this->assertEquals(1, $cookie[0]); + $this->assertEquals('foo', Cookie::$jar['laravel_auth_drivers_fluent_remember']['path']); + $this->assertEquals('bar', Cookie::$jar['laravel_auth_drivers_fluent_remember']['domain']); + $this->assertTrue(Cookie::$jar['laravel_auth_drivers_fluent_remember']['secure']); + + Auth::logout(); + + $this->setServerVar('HTTPS', 'off'); } /** @@ -228,40 +290,70 @@ public function testLogoutMethodLogsOutUser() { Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver')); - //$data = Session::$instance->session['data']['laravel_auth_drivers_fluent_login'] = 10; + $data = Session::$instance->session['data']['laravel_auth_drivers_fluent_login'] = 1; - // FIXME: Restore some of these! - //Config::set('auth.logout', function($user) { $_SERVER['auth.logout.stub'] = $user; }); - - //Auth::$user = 'Taylor'; Auth::logout(); - //$this->assertEquals('Taylor', $_SERVER['auth.logout.stub']); + // A workaround since Cookie will is only stored in memory, until Response class is called. + Auth::driver()->token = null; + $this->assertNull(Auth::user()); - // FIXME: Not sure whether hard-coding the key is a good idea. + $this->assertFalse(isset(Session::$instance->session['data']['laravel_auth_drivers_fluent_login'])); $this->assertTrue(Cookie::$jar['laravel_auth_drivers_fluent_remember']['expiration'] < time()); } } -class AuthUserReturnsNull extends Laravel\Auth { +class AuthUserReturnsNull extends Laravel\Auth\Drivers\Driver { - public static function user() {} + public function user() { return null; } + + public function retrieve($id) { return null; } + + public function attempt($arguments = array()) { return null; } } -class AuthUserReturnsDummy extends Laravel\Auth { +class AuthUserReturnsDummy extends Laravel\Auth\Drivers\Driver { - public static function user() { return 'Taylor'; } + public function user() { return 'Taylor'; } -} + public function retrieve($id) { return null; } -class AuthLoginStub extends Laravel\Auth { - - public static function login($user, $remember = false) + public function attempt($arguments = array()) { + return $this->login($arguments['username']); + } + +} + +class AuthLoginStub extends Laravel\Auth\Drivers\Fluent { + + public function login($user, $remember = false) + { + if (is_null($remember)) $remember = false; + $_SERVER['auth.login.stub'] = compact('user', 'remember'); + + return parent::login($user, $remember); + } + + public function logout() + { + parent::logout(); + } + + public function retrieve($id) + { + $user = parent::retrieve($id); + + $_SERVER['auth.login.stub'] = array( + 'user' => $user->id, + 'remember' => false, + ); + + return $user; } } \ No newline at end of file diff --git a/laravel/tests/cases/blade.test.php b/laravel/tests/cases/blade.test.php index 70e80b3b..343be2ea 100644 --- a/laravel/tests/cases/blade.test.php +++ b/laravel/tests/cases/blade.test.php @@ -30,10 +30,10 @@ public function testControlStructuresAreCreatedCorrectly() $blade3 = "@if (true)\nfoo\n@elseif (false)\nbar\n@endif"; $blade4 = "@if (true)\nfoo\n@else\nbar\n@endif"; - $this->assertEquals("\nfoo\n", Blade::compile_string($blade1)); - $this->assertEquals(" 0): ?>\nfoo\n", Blade::compile_string($blade2)); - $this->assertEquals("\nfoo\n\nbar\n", Blade::compile_string($blade3)); - $this->assertEquals("\nfoo\n\nbar\n", Blade::compile_string($blade4)); + $this->assertEquals("\nfoo\n", Blade::compile_string($blade1)); + $this->assertEquals(" 0): ?>\nfoo\n", Blade::compile_string($blade2)); + $this->assertEquals("\nfoo\n\nbar\n", Blade::compile_string($blade3)); + $this->assertEquals("\nfoo\n\nbar\n", Blade::compile_string($blade4)); } /** diff --git a/laravel/tests/cases/bundle.test.php b/laravel/tests/cases/bundle.test.php index 5a107d62..9826a50f 100644 --- a/laravel/tests/cases/bundle.test.php +++ b/laravel/tests/cases/bundle.test.php @@ -147,10 +147,8 @@ public function testPathMethodReturnsCorrectPath() */ public function testAssetPathReturnsPathToBundlesAssets() { - Config::set('application.url', 'http://localhost'); - - $this->assertEquals('http://localhost/bundles/dashboard/', Bundle::assets('dashboard')); - $this->assertEquals('http://localhost/', Bundle::assets(DEFAULT_BUNDLE)); + $this->assertEquals('/bundles/dashboard/', Bundle::assets('dashboard')); + $this->assertEquals('/', Bundle::assets(DEFAULT_BUNDLE)); Config::set('application.url', ''); } diff --git a/laravel/tests/cases/controller.test.php b/laravel/tests/cases/controller.test.php index 1577ece3..9eb192b9 100644 --- a/laravel/tests/cases/controller.test.php +++ b/laravel/tests/cases/controller.test.php @@ -96,24 +96,24 @@ public function testFiltersCanBeConstrainedByRequestMethod() { $_SERVER['test-on-post'] = false; - $_SERVER['REQUEST_METHOD'] = 'GET'; + Request::$foundation->setMethod('GET'); Controller::call('filter@index'); $this->assertFalse($_SERVER['test-on-post']); - $_SERVER['REQUEST_METHOD'] = 'POST'; + Request::$foundation->setMethod('POST'); Controller::call('filter@index'); $this->assertTrue($_SERVER['test-on-post']); $_SERVER['test-on-get-put'] = false; - $_SERVER['REQUEST_METHOD'] = 'POST'; + Request::$foundation->setMethod('POST'); Controller::call('filter@index'); $this->assertFalse($_SERVER['test-on-get-put']); - $_SERVER['REQUEST_METHOD'] = 'PUT'; + Request::$foundation->setMethod('PUT'); Controller::call('filter@index'); $this->assertTrue($_SERVER['test-on-get-put']); @@ -183,15 +183,18 @@ public function testMultipleFiltersCanBeAssignedToAnAction() */ public function testRestfulControllersRespondWithRestfulMethods() { - $_SERVER['REQUEST_METHOD'] = 'GET'; + Request::$foundation->setMethod('GET'); + //$_SERVER['REQUEST_METHOD'] = 'GET'; $this->assertEquals('get_index', Controller::call('restful@index')->content); - $_SERVER['REQUEST_METHOD'] = 'PUT'; + //$_SERVER['REQUEST_METHOD'] = 'PUT'; + Request::$foundation->setMethod('PUT'); $this->assertEquals(404, Controller::call('restful@index')->status()); - $_SERVER['REQUEST_METHOD'] = 'POST'; + //$_SERVER['REQUEST_METHOD'] = 'POST'; + Request::$foundation->setMethod('POST'); $this->assertEquals('post_index', Controller::call('restful@index')->content); } diff --git a/laravel/tests/cases/cookie.test.php b/laravel/tests/cases/cookie.test.php index dcfaf5d8..f62f7857 100644 --- a/laravel/tests/cases/cookie.test.php +++ b/laravel/tests/cases/cookie.test.php @@ -1,5 +1,7 @@ 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 Cookie::has method. * @@ -69,12 +98,19 @@ public function testForeverShouldUseATonOfMinutes() { Cookie::forever('foo', 'bar'); $this->assertEquals('bar', Cookie::$jar['foo']['value']); - $this->assertEquals(525600, Cookie::$jar['foo']['expiration']); + + // Shouldn't be able to test this cause while we indicate -2000 seconds + // cookie expiration store timestamp. + // $this->assertEquals(525600, Cookie::$jar['foo']['expiration']); + + $this->setServerVar('HTTPS', 'on'); Cookie::forever('bar', 'baz', 'path', 'domain', true); $this->assertEquals('path', Cookie::$jar['bar']['path']); $this->assertEquals('domain', Cookie::$jar['bar']['domain']); $this->assertTrue(Cookie::$jar['bar']['secure']); + + $this->setServerVar('HTTPS', 'off'); } /** @@ -85,7 +121,11 @@ public function testForeverShouldUseATonOfMinutes() public function testForgetSetsCookieWithExpiration() { Cookie::forget('bar', 'path', 'domain'); - $this->assertEquals(-2000, Cookie::$jar['bar']['expiration']); + + // Shouldn't be able to test this cause while we indicate -2000 seconds + // cookie expiration store timestamp. + //$this->assertEquals(-2000, Cookie::$jar['bar']['expiration']); + $this->assertEquals('path', Cookie::$jar['bar']['path']); $this->assertEquals('domain', Cookie::$jar['bar']['domain']); $this->assertFalse(Cookie::$jar['bar']['secure']); diff --git a/laravel/tests/cases/input.test.php b/laravel/tests/cases/input.test.php index 76172c1e..5a1890f0 100644 --- a/laravel/tests/cases/input.test.php +++ b/laravel/tests/cases/input.test.php @@ -149,7 +149,7 @@ public function testFlushMethodClearsFlashedInput() { $this->setSession(); - $input = array('name' => 'Taylor'); + $input = array('name' => 'Taylor', 'age' => 30); Request::foundation()->request->add($input); Input::flash(); diff --git a/laravel/tests/cases/redirect.test.php b/laravel/tests/cases/redirect.test.php index 4f0897d7..cce63a81 100644 --- a/laravel/tests/cases/redirect.test.php +++ b/laravel/tests/cases/redirect.test.php @@ -12,7 +12,7 @@ public function setUp() Config::set('session.driver', 'foo'); Router::$routes = array(); Router::$names = array(); - Config::set('application.url', 'http://localhost'); + URL::$base = 'http://localhost/'; Config::set('application.index', ''); } @@ -25,7 +25,7 @@ public function tearDown() Config::set('session.driver', ''); Router::$routes = array(); Router::$names = array(); - Config::set('application.url', ''); + URL::$base = ''; Config::set('application.index', 'index.php'); Session::$instance = null; } diff --git a/laravel/tests/cases/session.test.php b/laravel/tests/cases/session.test.php index 05b0299d..cf6946a7 100644 --- a/laravel/tests/cases/session.test.php +++ b/laravel/tests/cases/session.test.php @@ -373,7 +373,9 @@ public function testSaveMethodSetsCookieWithCorrectValues() $cookie = Cookie::$jar[Config::get('session.cookie')]; $this->assertEquals('foo', $cookie['value']); - $this->assertEquals(Config::get('session.lifetime'), $cookie['expiration']); + // Shouldn't be able to test this cause session.lifetime store number of minutes + // while cookie expiration store timestamp when it going to expired. + // $this->assertEquals(Config::get('session.lifetime'), $cookie['expiration']); $this->assertEquals(Config::get('session.domain'), $cookie['domain']); $this->assertEquals(Config::get('session.path'), $cookie['path']); $this->assertEquals(Config::get('session.secure'), $cookie['secure']); diff --git a/laravel/tests/cases/uri.test.php b/laravel/tests/cases/uri.test.php index fc64b3fb..8f2b85ff 100644 --- a/laravel/tests/cases/uri.test.php +++ b/laravel/tests/cases/uri.test.php @@ -49,7 +49,7 @@ public function testCorrectURIIsReturnedByCurrentMethod($uri, $expectation) */ public function testSegmentMethodReturnsAURISegment() { - $this->setRequestUri('http://localhost/index.php/user/profile'); + $this->setRequestUri('/user/profile'); $this->assertEquals('user', URI::segment(1)); $this->assertEquals('profile', URI::segment(2)); @@ -61,16 +61,14 @@ public function testSegmentMethodReturnsAURISegment() public function requestUriProvider() { return array( - array('/index.php', '/'), - array('/index.php/', '/'), - array('http://localhost/user', 'user'), - array('http://localhost/user/', 'user'), - array('http://localhost/index.php', '/'), - array('http://localhost/index.php/', '/'), - array('http://localhost/index.php//', '/'), - array('http://localhost/index.php/user', 'user'), - array('http://localhost/index.php/user/', 'user'), - array('http://localhost/index.php/user/profile', 'user/profile'), + array('/user', 'user'), + array('/user/', 'user'), + array('', '/'), + array('/', '/'), + array('//', '/'), + array('/user', 'user'), + array('/user/', 'user'), + array('/user/profile', 'user/profile'), ); }