Browse Source

Merge pull request #1 from crynobone/core-tests

Refix unit testing for Laravel
Franz Liedke 12 years ago
parent
commit
a28698ed36

+ 2 - 6
laravel/tests/cases/asset.test.php

@@ -184,13 +184,11 @@ class AssetTest extends PHPUnit_Framework_TestCase {
 	 */
 	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 @@ class AssetTest extends PHPUnit_Framework_TestCase {
 	 */
 	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'));
 	}
 
 	/**

+ 132 - 40
laravel/tests/cases/auth.test.php

@@ -1,5 +1,7 @@
 <?php
 
+use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;
+
 use Laravel\Str;
 use Laravel\Auth;
 use Laravel\Cookie;
@@ -35,6 +37,33 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 		Config::set('database.default', 'mysql');
 	}
 
+	/**
+	 * Set one of the $_SERVER variables.
+	 *
+	 * @param string  $key
+	 * @param string  $value
+	 */
+	protected function setServerVar($key, $value)
+	{
+		$_SERVER[$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 Auth::user method.
 	 *
@@ -54,7 +83,9 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testCheckMethodReturnsTrueWhenUserIsSet()
 	{
-		$this->assertTrue(AuthUserReturnsDummy::check());
+		$auth = new AuthUserReturnsDummy;
+
+		$this->assertTrue($auth->check());
 	}
 
 	/**
@@ -64,7 +95,9 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testCheckMethodReturnsFalseWhenNoUserIsSet()
 	{
-		$this->assertFalse(AuthUserReturnsNull::check());
+		$auth = new AuthUserReturnsNull;
+
+		$this->assertFalse($auth->check());
 	}
 
 	/**
@@ -74,7 +107,9 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testGuestReturnsTrueWhenNoUserIsSet()
 	{
-		$this->assertTrue(AuthUserReturnsNull::guest());
+		$auth = new AuthUserReturnsNull;
+
+		$this->assertTrue($auth->guest());
 	}
 
 	/**
@@ -84,7 +119,9 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	 */
 	public function testGuestReturnsFalseWhenUserIsSet()
 	{
-		$this->assertFalse(AuthUserReturnsDummy::guest());
+		$auth = new AuthUserReturnsDummy;
+		
+		$this->assertFalse($auth->guest());
 	}
 
 	/**
@@ -107,10 +144,12 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	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 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	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 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 		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);
+
+		$auth = new AuthLoginStub;
 
-		$this->assertEquals('Taylor Otwell', AuthLoginStub::user()->name);
-		$this->assertTrue(AuthLoginStub::user() === $_SERVER['auth.login.stub']['user']);
+		$this->assertEquals('Taylor Otwell', $auth->user()->name);
+		
+		$this->assertTrue($auth->user()->id === $_SERVER['auth.login.stub']['user']);
 	}
 
 	/**
@@ -150,11 +192,11 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	 */
 	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 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	 */
 	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 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 		$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 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	{
 		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);
+
+		$this->assertTrue(isset(Cookie::$jar['laravel_auth_drivers_fluent_remember']));
 
-		$cookie = Cookie::$jar[Config::get('auth.cookie')]['value'];
+		$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 @@ class AuthTest extends PHPUnit_Framework_TestCase {
 	{
 		Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
 		
-		//$data = Session::$instance->session['data']['laravel_auth_drivers_fluent_login'] = 10;
-
-		// FIXME: Restore some of these!
-		//Config::set('auth.logout', function($user) { $_SERVER['auth.logout.stub'] = $user; });
+		$data = Session::$instance->session['data']['laravel_auth_drivers_fluent_login'] = 1;
 
-		//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 function user() { return null; }
+
+	public function retrieve($id) { return null; }
 
-	public static function user() {}
+	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; }
+
+	public function attempt($arguments = array()) 
+	{
+		return $this->login($arguments['username']); 
+	}
 
 }
 
-class AuthLoginStub extends Laravel\Auth {
+class AuthLoginStub extends Laravel\Auth\Drivers\Fluent {
 	
-	public static function login($user, $remember = false) 
+	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;
 	}
 
 }

+ 4 - 4
laravel/tests/cases/blade.test.php

@@ -30,10 +30,10 @@ class BladeTest extends PHPUnit_Framework_TestCase {
 		$blade3 = "@if (true)\nfoo\n@elseif (false)\nbar\n@endif";
 		$blade4 = "@if (true)\nfoo\n@else\nbar\n@endif";
 
-		$this->assertEquals("<?php if (true): ?>\nfoo\n<?php endif; ?>", Blade::compile_string($blade1));
-		$this->assertEquals("<?php if (count(".'$something'.") > 0): ?>\nfoo\n<?php endif; ?>", Blade::compile_string($blade2));
-		$this->assertEquals("<?php if (true): ?>\nfoo\n<?php elseif (false): ?>\nbar\n<?php endif; ?>", Blade::compile_string($blade3));
-		$this->assertEquals("<?php if (true): ?>\nfoo\n<?php else: ?>\nbar\n<?php endif; ?>", Blade::compile_string($blade4));
+		$this->assertEquals("<?php if(true): ?>\nfoo\n<?php endif; ?>", Blade::compile_string($blade1));
+		$this->assertEquals("<?php if(count(".'$something'.") > 0): ?>\nfoo\n<?php endif; ?>", Blade::compile_string($blade2));
+		$this->assertEquals("<?php if(true): ?>\nfoo\n<?php elseif(false): ?>\nbar\n<?php endif; ?>", Blade::compile_string($blade3));
+		$this->assertEquals("<?php if(true): ?>\nfoo\n<?php else: ?>\nbar\n<?php endif; ?>", Blade::compile_string($blade4));
 	}
 
 	/**

+ 2 - 4
laravel/tests/cases/bundle.test.php

@@ -147,10 +147,8 @@ class BundleTest extends PHPUnit_Framework_TestCase {
 	 */
 	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', '');
 	}

+ 10 - 7
laravel/tests/cases/controller.test.php

@@ -96,24 +96,24 @@ class ControllerTest extends PHPUnit_Framework_TestCase {
 	{
 		$_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 @@ class ControllerTest extends PHPUnit_Framework_TestCase {
 	 */
 	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);
 	}

+ 42 - 2
laravel/tests/cases/cookie.test.php

@@ -1,5 +1,7 @@
 <?php namespace Laravel;
 
+use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;
+
 /**
  * Stub the global setcookie method into the Laravel namespace.
  */
@@ -31,6 +33,33 @@ class CookieTest extends \PHPUnit_Framework_TestCase {
 		Cookie::$jar = array();
 	}
 
+	/**
+	 * Set one of the $_SERVER variables.
+	 *
+	 * @param string  $key
+	 * @param string  $value
+	 */
+	protected function setServerVar($key, $value)
+	{
+		$_SERVER[$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 Cookie::has method.
 	 *
@@ -69,12 +98,19 @@ class CookieTest extends \PHPUnit_Framework_TestCase {
 	{
 		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 @@ class CookieTest extends \PHPUnit_Framework_TestCase {
 	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']);

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

@@ -149,7 +149,7 @@ class InputTest extends PHPUnit_Framework_TestCase {
 	{
 		$this->setSession();
 
-		$input = array('name' => 'Taylor');
+		$input = array('name' => 'Taylor', 'age' => 30);
 		Request::foundation()->request->add($input);
 
 		Input::flash();

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

@@ -12,7 +12,7 @@ class RedirectTest extends PHPUnit_Framework_TestCase {
 		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 @@ class RedirectTest extends PHPUnit_Framework_TestCase {
 		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;
 	}

+ 3 - 1
laravel/tests/cases/session.test.php

@@ -373,7 +373,9 @@ class SessionTest extends PHPUnit_Framework_TestCase {
 		$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']);

+ 9 - 11
laravel/tests/cases/uri.test.php

@@ -49,7 +49,7 @@ class URITest extends PHPUnit_Framework_TestCase {
 	 */
 	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 @@ class URITest extends PHPUnit_Framework_TestCase {
 	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'),
 		);
 	}