Import testing bundle to laravel/tests directory.

This commit is contained in:
Franz Liedke
2012-07-13 00:14:03 +02:00
parent 1879e6575a
commit a7c211339a
78 changed files with 6335 additions and 12 deletions

View File

@@ -0,0 +1,262 @@
<?php
class AssetTest extends PHPUnit_Framework_TestCase {
/**
* Initialize the test environment.
*/
public function setUp()
{
Config::$items = array();
Config::$cache = array();
Asset::$containers = array();
}
/**
* Test the Asset::container method.
*
* @group laravel
*/
public function testContainersCanBeCreated()
{
$container = Asset::container('foo');
$this->assertTrue($container === Asset::container('foo'));
$this->assertInstanceOf('\\Laravel\\Asset_Container', $container);
}
/**
* Test the Asset::container method for default container creation.
*
* @group laravel
*/
public function testDefaultContainerCreatedByDefault()
{
$this->assertEquals('default', Asset::container()->name);
}
/**
* Test the Asset::__callStatic method.
*
* @group laravel
*/
public function testContainerMethodsCanBeDynamicallyCalled()
{
Asset::style('common', 'common.css');
$this->assertEquals('common.css', Asset::container()->assets['style']['common']['source']);
}
/**
* Test the Asset_Container constructor.
*
* @group laravel
*/
public function testNameIsSetOnAssetContainerConstruction()
{
$container = $this->getContainer();
$this->assertEquals('foo', $container->name);
}
/**
* Test the Asset_Container::add method.
*
* @group laravel
*/
public function testAddMethodProperlySniffsAssetType()
{
$container = $this->getContainer();
$container->add('jquery', 'jquery.js');
$container->add('common', 'common.css');
$this->assertEquals('jquery.js', $container->assets['script']['jquery']['source']);
$this->assertEquals('common.css', $container->assets['style']['common']['source']);
}
/**
* Test the Asset_Container::style method.
*
* @group laravel
*/
public function testStyleMethodProperlyRegistersAnAsset()
{
$container = $this->getContainer();
$container->style('common', 'common.css');
$this->assertEquals('common.css', $container->assets['style']['common']['source']);
}
/**
* Test the Asset_Container::style method sets media attribute.
*
* @group laravel
*/
public function testStyleMethodProperlySetsMediaAttributeIfNotSet()
{
$container = $this->getContainer();
$container->style('common', 'common.css');
$this->assertEquals('all', $container->assets['style']['common']['attributes']['media']);
}
/**
* Test the Asset_Container::style method sets media attribute.
*
* @group laravel
*/
public function testStyleMethodProperlyIgnoresMediaAttributeIfSet()
{
$container = $this->getContainer();
$container->style('common', 'common.css', array(), array('media' => 'print'));
$this->assertEquals('print', $container->assets['style']['common']['attributes']['media']);
}
/**
* Test the Asset_Container::script method.
*
* @group laravel
*/
public function testScriptMethodProperlyRegistersAnAsset()
{
$container = $this->getContainer();
$container->script('jquery', 'jquery.js');
$this->assertEquals('jquery.js', $container->assets['script']['jquery']['source']);
}
/**
* Test the Asset_Container::add method properly sets dependencies.
*
* @group laravel
*/
public function testAddMethodProperlySetsDependencies()
{
$container = $this->getContainer();
$container->add('common', 'common.css', 'jquery');
$container->add('jquery', 'jquery.js', array('jquery-ui'));
$this->assertEquals(array('jquery'), $container->assets['style']['common']['dependencies']);
$this->assertEquals(array('jquery-ui'), $container->assets['script']['jquery']['dependencies']);
}
/**
* Test the Asset_Container::add method properly sets attributes.
*
* @group laravel
*/
public function testAddMethodProperlySetsAttributes()
{
$container = $this->getContainer();
$container->add('common', 'common.css', array(), array('media' => 'print'));
$container->add('jquery', 'jquery.js', array(), array('defer'));
$this->assertEquals(array('media' => 'print'), $container->assets['style']['common']['attributes']);
$this->assertEquals(array('defer'), $container->assets['script']['jquery']['attributes']);
}
/**
* Test the Asset_Container::bundle method.
*
* @group laravel
*/
public function testBundleMethodCorrectlySetsTheAssetBundle()
{
$container = $this->getContainer();
$container->bundle('eloquent');
$this->assertEquals('eloquent', $container->bundle);
}
/**
* Test the Asset_Container::path method.
*
* @group laravel
*/
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'));
}
/**
* Test the Asset_Container::path method.
*
* @group laravel
*/
public function testPathMethodReturnsCorrectPathForAnApplicationAsset()
{
Config::set('application.url', 'http://localhost');
$container = $this->getContainer();
$this->assertEquals('http://localhost/foo.jpg', $container->path('foo.jpg'));
}
/**
* Test the Asset_Container::scripts method.
*
* @group laravel
*/
public function testScriptsCanBeRetrieved()
{
$container = $this->getContainer();
$container->script('dojo', 'dojo.js', array('jquery-ui'));
$container->script('jquery', 'jquery.js', array('jquery-ui', 'dojo'));
$container->script('jquery-ui', 'jquery-ui.js');
$scripts = $container->scripts();
$this->assertTrue(strpos($scripts, 'jquery.js') > 0);
$this->assertTrue(strpos($scripts, 'jquery.js') > strpos($scripts, 'jquery-ui.js'));
$this->assertTrue(strpos($scripts, 'dojo.js') > strpos($scripts, 'jquery-ui.js'));
}
/**
* Test the Asset_Container::styles method.
*
* @group laravel
*/
public function testStylesCanBeRetrieved()
{
$container = $this->getContainer();
$container->style('dojo', 'dojo.css', array('jquery-ui'), array('media' => 'print'));
$container->style('jquery', 'jquery.css', array('jquery-ui', 'dojo'));
$container->style('jquery-ui', 'jquery-ui.css');
$styles = $container->styles();
$this->assertTrue(strpos($styles, 'jquery.css') > 0);
$this->assertTrue(strpos($styles, 'media="print"') > 0);
$this->assertTrue(strpos($styles, 'jquery.css') > strpos($styles, 'jquery-ui.css'));
$this->assertTrue(strpos($styles, 'dojo.css') > strpos($styles, 'jquery-ui.css'));
}
/**
* Get an asset container instance.
*
* @param string $name
* @return Asset_Container
*/
private function getContainer($name = 'foo')
{
return new Laravel\Asset_Container($name);
}
}

View File

@@ -0,0 +1,259 @@
<?php
use Laravel\Str;
use Laravel\Auth;
use Laravel\Cookie;
use Laravel\Session;
use Laravel\Crypter;
use Laravel\Session\Payload;
class AuthTest extends PHPUnit_Framework_TestCase {
/**
* Setup teh test environment.
*/
public function setUp()
{
$_SERVER['auth.login.stub'] = null;
Cookie::$jar = array();
Config::$items = array();
Auth::driver()->$user = null;
Session::$instance = null;
Config::set('database.default', 'sqlite');
}
/**
* Tear down the test environment.
*/
public function tearDown()
{
$_SERVER['auth.login.stub'] = null;
Cookie::$jar = array();
Config::$items = array();
Auth::driver()->$user = null;
Session::$instance = null;
Config::set('database.default', 'mysql');
}
/**
* Test the Auth::user method.
*
* @group laravel
*/
public function testUserMethodReturnsCurrentUser()
{
Auth::$user = 'Taylor';
$this->assertEquals('Taylor', Auth::user());
}
/**
* Test the Auth::check method.
*
* @group laravel
*/
public function testCheckMethodReturnsTrueWhenUserIsSet()
{
$this->assertTrue(AuthUserReturnsDummy::check());
}
/**
* Test the Auth::check method.
*
* @group laravel
*/
public function testCheckMethodReturnsFalseWhenNoUserIsSet()
{
$this->assertFalse(AuthUserReturnsNull::check());
}
/**
* Test the Auth::guest method.
*
* @group laravel
*/
public function testGuestReturnsTrueWhenNoUserIsSet()
{
$this->assertTrue(AuthUserReturnsNull::guest());
}
/**
* Test the Auth::guest method.
*
* @group laravel
*/
public function testGuestReturnsFalseWhenUserIsSet()
{
$this->assertFalse(AuthUserReturnsDummy::guest());
}
/**
* Test the Auth::user method.
*
* @group laravel
*/
public function testUserMethodReturnsNullWhenNoUserExistsAndNoRecallerExists()
{
Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
$this->assertNull(Auth::user());
}
/**
* Test the Auth::user method.
*
* @group laravel
*/
public function testUserReturnsUserByID()
{
Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
Session::$instance->session['data'][Auth::user_key] = 1;
$this->assertEquals('Taylor Otwell', Auth::user()->name);
}
/**
* Test the Auth::user method.
*
* @group laravel
*/
public function testNullReturnedWhenUserIDNotValidInteger()
{
Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
Session::$instance->session['data'][Auth::user_key] = 'asdlkasd';
$this->assertNull(Auth::user());
}
/**
* Test the Auth::recall method.
*
* @group laravel
*/
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);
$this->assertEquals('Taylor Otwell', AuthLoginStub::user()->name);
$this->assertTrue(AuthLoginStub::user() === $_SERVER['auth.login.stub']['user']);
}
/**
* Test the Auth::attempt method.
*
* @group laravel
*/
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));
}
/**
* Test the Auth::attempt method.
*
* @group laravel
*/
public function testAttemptReturnsTrueWhenCredentialsAreCorrect()
{
$this->assertTrue(AuthLoginStub::attempt('taylor', 'password1'));
$this->assertEquals('Taylor Otwell', $_SERVER['auth.login.stub']['user']->name);
$this->assertFalse($_SERVER['auth.login.stub']['remember']);
$this->assertTrue(AuthLoginStub::attempt('taylor', 'password1', true));
$this->assertEquals('Taylor Otwell', $_SERVER['auth.login.stub']['user']->name);
$this->assertTrue($_SERVER['auth.login.stub']['remember']);
}
/**
* Test Auth::login method.
*
* @group laravel
*/
public function testLoginMethodStoresUserKeyInSession()
{
Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
$user = new StdClass;
$user->id = 10;
Auth::login($user);
$this->assertEquals(10, Session::$instance->session['data'][Auth::user_key]);
Auth::login(5);
$this->assertEquals(5, Session::$instance->session['data'][Auth::user_key]);
}
/**
* Test the Auth::login method.
*
* @group laravel
*/
public function testLoginStoresRememberCookieWhenNeeded()
{
Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
// 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, true);
$this->assertTrue(isset(Cookie::$jar[Config::get('auth.cookie')]));
$cookie = Cookie::$jar[Config::get('auth.cookie')]['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']);
}
/**
* Test the Auth::logout method.
*
* @group laravel
*/
public function testLogoutMethodLogsOutUser()
{
Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
Session::$instance->session['data'][Auth::user_key] = 10;
Config::set('auth.logout', function($user) { $_SERVER['auth.logout.stub'] = $user; });
Auth::$user = 'Taylor';
Auth::logout();
$this->assertEquals('Taylor', $_SERVER['auth.logout.stub']);
$this->assertNull(Auth::$user);
$this->assertFalse(isset(Session::$instance->session['data'][Auth::user_key]));
$this->assertTrue(Cookie::$jar[Config::get('auth.cookie')]['minutes'] < 0);
}
}
class AuthUserReturnsNull extends Laravel\Auth {
public static function user() {}
}
class AuthUserReturnsDummy extends Laravel\Auth {
public static function user() { return 'Taylor'; }
}
class AuthLoginStub extends Laravel\Auth {
public static function login($user, $remember = false)
{
$_SERVER['auth.login.stub'] = compact('user', 'remember');
}
}

View File

@@ -0,0 +1,102 @@
<?php
class AutoloaderTest extends PHPUnit_Framework_TestCase {
/**
* Test the Autoloader::map method.
*
* @group laravel
*/
public function testMapsCanBeRegistered()
{
Autoloader::map(array(
'Foo' => path('app').'models/foo.php',
));
$this->assertEquals(path('app').'models/foo.php', Autoloader::$mappings['Foo']);
}
/**
* Test the Autoloader::alias method.
*
* @group laravel
*/
public function testAliasesCanBeRegistered()
{
Autoloader::alias('Foo\\Bar', 'Foo');
$this->assertEquals('Foo\\Bar', Autoloader::$aliases['Foo']);
}
/**
* Test the Autoloader::directories method.
*
* @group laravel
*/
public function testPsrDirectoriesCanBeRegistered()
{
Autoloader::directories(array(
path('app').'foo'.DS.'bar',
path('app').'foo'.DS.'baz'.DS.DS,
));
$this->assertTrue(in_array(path('app').'foo'.DS.'bar'.DS, Autoloader::$directories));
$this->assertTrue(in_array(path('app').'foo'.DS.'baz'.DS, Autoloader::$directories));
}
/**
* Test the Autoloader::namespaces method.
*
* @group laravel
*/
public function testNamespacesCanBeRegistered()
{
Autoloader::namespaces(array(
'Autoloader_1' => path('bundle').'autoload'.DS.'models',
'Autoloader_2' => path('bundle').'autoload'.DS.'libraries'.DS.DS,
));
$this->assertEquals(path('bundle').'autoload'.DS.'models'.DS, Autoloader::$namespaces['Autoloader_1\\']);
$this->assertEquals(path('bundle').'autoload'.DS.'libraries'.DS, Autoloader::$namespaces['Autoloader_2\\']);
}
/**
* Test the loading of PSR-0 models and libraries.
*
* @group laravel
*/
public function testPsrLibrariesAndModelsCanBeLoaded()
{
$this->assertInstanceOf('User', new User);
$this->assertInstanceOf('Repositories\\User', new Repositories\User);
}
/**
* Test the loading of hard-coded classes.
*
* @group laravel
*/
public function testHardcodedClassesCanBeLoaded()
{
Autoloader::map(array(
'Autoloader_HardCoded' => path('app').'models'.DS.'autoloader.php',
));
$this->assertInstanceOf('Autoloader_HardCoded', new Autoloader_HardCoded);
}
/**
* Test the loading of classes mapped by namespaces.
*
* @group laravel
*/
public function testClassesMappedByNamespaceCanBeLoaded()
{
Autoloader::namespaces(array(
'Dashboard' => path('bundle').'dashboard'.DS.'models',
));
$this->assertInstanceOf('Dashboard\\Repository', new Dashboard\Repository);
}
}

View File

@@ -0,0 +1,63 @@
<?php
use Laravel\Blade;
class BladeTest extends PHPUnit_Framework_TestCase {
/**
* Test the compilation of echo statements.
*
* @group laravel
*/
public function testEchosAreConvertedProperly()
{
$blade1 = '{{$a}}';
$blade2 = '{{e($a)}}';
$this->assertEquals('<?php echo $a; ?>', Blade::compile_string($blade1));
$this->assertEquals('<?php echo e($a); ?>', Blade::compile_string($blade2));
}
/**
* Test the compilation of control structures.
*
* @group laravel
*/
public function testControlStructuresAreCreatedCorrectly()
{
$blade1 = "@if (true)\nfoo\n@endif";
$blade2 = "@if (count(".'$something'.") > 0)\nfoo\n@endif";
$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));
}
/**
* Test the compilation of yield statements.
*
* @group laravel
*/
public function testYieldsAreCompiledCorrectly()
{
$blade = "@yield('something')";
$this->assertEquals("<?php echo \\Laravel\\Section::yield('something'); ?>", Blade::compile_string($blade));
}
/**
* Test the compilation of section statements.
*
* @group laravel
*/
public function testSectionsAreCompiledCorrectly()
{
$blade = "@section('something')\nfoo\n@endsection";
$this->assertEquals("<?php \\Laravel\\Section::start('something'); ?>\nfoo\n<?php \\Laravel\\Section::stop(); ?>", Blade::compile_string($blade));
}
}

View File

@@ -0,0 +1,253 @@
<?php
class BundleTest extends PHPUnit_Framework_TestCase {
/**
* Setup the test environment.
*/
public function setUp()
{
Bundle::$started = array();
Bundle::$elements = array();
unset(Bundle::$bundles['foo']);
}
/**
* Tear down the test environment.
*/
public function tearDown()
{
Bundle::$started = array();
Bundle::$elements = array();
unset(Bundle::$bundles['foo']);
}
/**
* Test Bundle::register method.
*
* @group laravel
*/
public function testRegisterMethodCorrectlyRegistersBundle()
{
Bundle::register('foo-baz', array('handles' => 'foo-baz'));
$this->assertEquals('foo-baz', Bundle::$bundles['foo-baz']['handles']);
$this->assertFalse(Bundle::$bundles['foo-baz']['auto']);
Bundle::register('foo-bar', array());
$this->assertFalse(Bundle::$bundles['foo-baz']['auto']);
$this->assertNull(Bundle::$bundles['foo-bar']['handles']);
unset(Bundle::$bundles['foo-baz']);
unset(Bundle::$bundles['foo-bar']);
}
/**
* Test the Bundle::start method.
*
* @group laravel
*/
public function testStartMethodStartsBundle()
{
$_SERVER['bundle.dummy.start'] = 0;
$_SERVER['bundle.dummy.routes'] = 0;
$_SERVER['started.dummy'] = false;
Event::listen('laravel.started: dummy', function()
{
$_SERVER['started.dummy'] = true;
});
Bundle::register('dummy');
Bundle::start('dummy');
$this->assertTrue($_SERVER['started.dummy']);
$this->assertEquals(1, $_SERVER['bundle.dummy.start']);
$this->assertEquals(1, $_SERVER['bundle.dummy.routes']);
Bundle::start('dummy');
$this->assertEquals(1, $_SERVER['bundle.dummy.start']);
$this->assertEquals(1, $_SERVER['bundle.dummy.routes']);
}
/**
* Test Bundle::handles method.
*
* @group laravel
*/
public function testHandlesMethodReturnsBundleThatHandlesURI()
{
Bundle::register('foo', array('handles' => 'foo-bar'));
$this->assertEquals('foo', Bundle::handles('foo-bar/admin'));
unset(Bundle::$bundles['foo']);
}
/**
* Test the Bundle::exist method.
*
* @group laravel
*/
public function testExistMethodIndicatesIfBundleExist()
{
$this->assertTrue(Bundle::exists('dashboard'));
$this->assertFalse(Bundle::exists('foo'));
}
/**
* Test the Bundle::started method.
*
* @group laravel
*/
public function testStartedMethodIndicatesIfBundleIsStarted()
{
Bundle::register('dummy');
Bundle::start('dummy');
$this->assertTrue(Bundle::started('dummy'));
}
/**
* Test the Bundle::prefix method.
*
* @group laravel
*/
public function testPrefixMethodReturnsCorrectPrefix()
{
$this->assertEquals('dummy::', Bundle::prefix('dummy'));
$this->assertEquals('', Bundle::prefix(DEFAULT_BUNDLE));
}
/**
* Test the Bundle::class_prefix method.
*
* @group laravel
*/
public function testClassPrefixMethodReturnsProperClassPrefixForBundle()
{
$this->assertEquals('Dummy_', Bundle::class_prefix('dummy'));
$this->assertEquals('', Bundle::class_prefix(DEFAULT_BUNDLE));
}
/**
* Test the Bundle::path method.
*
* @group laravel
*/
public function testPathMethodReturnsCorrectPath()
{
$this->assertEquals(path('app'), Bundle::path(null));
$this->assertEquals(path('app'), Bundle::path(DEFAULT_BUNDLE));
$this->assertEquals(path('bundle').'dashboard'.DS, Bundle::path('dashboard'));
}
/**
* Test the Bundle::asset method.
*
* @group laravel
*/
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));
Config::set('application.url', '');
}
/**
* Test the Bundle::name method.
*
* @group laravel
*/
public function testBundleNameCanBeRetrievedFromIdentifier()
{
$this->assertEquals(DEFAULT_BUNDLE, Bundle::name('something'));
$this->assertEquals(DEFAULT_BUNDLE, Bundle::name('something.else'));
$this->assertEquals('bundle', Bundle::name('bundle::something.else'));
}
/**
* Test the Bundle::element method.
*
* @group laravel
*/
public function testElementCanBeRetrievedFromIdentifier()
{
$this->assertEquals('something', Bundle::element('something'));
$this->assertEquals('something.else', Bundle::element('something.else'));
$this->assertEquals('something.else', Bundle::element('bundle::something.else'));
}
/**
* Test the Bundle::identifier method.
*
* @group laravel
*/
public function testIdentifierCanBeConstructed()
{
$this->assertEquals('something.else', Bundle::identifier(DEFAULT_BUNDLE, 'something.else'));
$this->assertEquals('dashboard::something', Bundle::identifier('dashboard', 'something'));
$this->assertEquals('dashboard::something.else', Bundle::identifier('dashboard', 'something.else'));
}
/**
* Test the Bundle::resolve method.
*
* @group laravel
*/
public function testBundleNamesCanBeResolved()
{
$this->assertEquals(DEFAULT_BUNDLE, Bundle::resolve('foo'));
$this->assertEquals('dashboard', Bundle::resolve('dashboard'));
}
/**
* Test the Bundle::parse method.
*
* @group laravel
*/
public function testParseMethodReturnsElementAndIdentifier()
{
$this->assertEquals(array('application', 'something'), Bundle::parse('something'));
$this->assertEquals(array('application', 'something.else'), Bundle::parse('something.else'));
$this->assertEquals(array('dashboard', 'something'), Bundle::parse('dashboard::something'));
$this->assertEquals(array('dashboard', 'something.else'), Bundle::parse('dashboard::something.else'));
}
/**
* Test the Bundle::get method.
*
* @group laravel
*/
public function testOptionMethodReturnsBundleOption()
{
$this->assertFalse(Bundle::option('dashboard', 'auto'));
$this->assertEquals('dashboard', Bundle::option('dashboard', 'location'));
}
/**
* Test the Bundle::all method.
*
* @group laravel
*/
public function testAllMethodReturnsBundleArray()
{
Bundle::register('foo');
$this->assertEquals(Bundle::$bundles, Bundle::all());
unset(Bundle::$bundles['foo']);
}
/**
* Test the Bundle::names method.
*
* @group laravel
*/
public function testNamesMethodReturnsBundleNames()
{
Bundle::register('foo');
$this->assertEquals(array('dashboard', 'dummy', 'foo'), Bundle::names());
unset(Bundle::$bundles['foo']);
}
}

View File

@@ -0,0 +1,79 @@
<?php
class ConfigTest extends PHPUnit_Framework_TestCase {
/**
* Tear down the testing environment.
*/
public function tearDown()
{
Config::$items = array();
Config::$cache = array();
}
/**
* Test the Config::get method.
*
* @group laravel
*/
public function testItemsCanBeRetrievedFromConfigFiles()
{
$this->assertEquals('UTF-8', Config::get('application.encoding'));
$this->assertEquals('mysql', Config::get('database.connections.mysql.driver'));
$this->assertEquals('dashboard', Config::get('dashboard::meta.bundle'));
}
/**
* Test the Config::has method.
*
* @group laravel
*/
public function testHasMethodIndicatesIfConfigItemExists()
{
$this->assertFalse(Config::has('application.foo'));
$this->assertTrue(Config::has('application.encoding'));
}
/**
* Test the Config::set method.
*
* @group laravel
*/
public function testConfigItemsCanBeSet()
{
Config::set('application.encoding', 'foo');
Config::set('dashboard::meta.bundle', 'bar');
$this->assertEquals('foo', Config::get('application.encoding'));
$this->assertEquals('bar', Config::get('dashboard::meta.bundle'));
}
/**
* Test that environment configurations are loaded correctly.
*
* @group laravel
*/
public function testEnvironmentConfigsOverrideNormalConfigurations()
{
$_SERVER['LARAVEL_ENV'] = 'local';
$this->assertEquals('sqlite', Config::get('database.default'));
unset($_SERVER['LARAVEL_ENV']);
}
/**
* Test that items can be set after the entire file has already been loaded.
*
* @group laravel
*/
public function testItemsCanBeSetAfterEntireFileIsLoaded()
{
Config::get('application');
Config::set('application.key', 'taylor');
$application = Config::get('application');
$this->assertEquals('taylor', $application['key']);
}
}

View File

@@ -0,0 +1,264 @@
<?php
class ControllerTest extends PHPUnit_Framework_TestCase {
/**
* Setup the testing environment.
*/
public function setUp()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
}
/**
* Tear down the testing environment.
*/
public function tearDown()
{
unset(Filter::$filters['test-all-before']);
unset(Filter::$filters['test-all-after']);
unset(Filter::$filters['test-profile-before']);
unset($_SERVER['REQUEST_METHOD']);
}
/**
* Test the Controller::call method.
*
* @group laravel
*/
public function testBasicControllerActionCanBeCalled()
{
$this->assertEquals('action_index', Controller::call('auth@index')->content);
$this->assertEquals('Admin_Panel_Index', Controller::call('admin.panel@index')->content);
$this->assertEquals('Taylor', Controller::call('auth@profile', array('Taylor'))->content);
$this->assertEquals('Dashboard_Panel_Index', Controller::call('dashboard::panel@index')->content);
}
/**
* Test basic controller filters are called.
*
* @group laravel
*/
public function testAssignedBeforeFiltersAreRun()
{
$_SERVER['test-all-after'] = false;
$_SERVER['test-all-before'] = false;
Controller::call('filter@index');
$this->assertTrue($_SERVER['test-all-after']);
$this->assertTrue($_SERVER['test-all-before']);
}
/**
* Test that "only" filters only apply to their assigned methods.
*
* @group laravel
*/
public function testOnlyFiltersOnlyApplyToTheirAssignedMethods()
{
$_SERVER['test-profile-before'] = false;
Controller::call('filter@index');
$this->assertFalse($_SERVER['test-profile-before']);
Controller::call('filter@profile');
$this->assertTrue($_SERVER['test-profile-before']);
}
/**
* Test that "except" filters only apply to the excluded methods.
*
* @group laravel
*/
public function testExceptFiltersOnlyApplyToTheExlucdedMethods()
{
$_SERVER['test-except'] = false;
Controller::call('filter@index');
Controller::call('filter@profile');
$this->assertFalse($_SERVER['test-except']);
Controller::call('filter@show');
$this->assertTrue($_SERVER['test-except']);
}
/**
* Test that filters can be constrained by the request method.
*
* @group laravel
*/
public function testFiltersCanBeConstrainedByRequestMethod()
{
$_SERVER['test-on-post'] = false;
$_SERVER['REQUEST_METHOD'] = 'GET';
Controller::call('filter@index');
$this->assertFalse($_SERVER['test-on-post']);
$_SERVER['REQUEST_METHOD'] = 'POST';
Controller::call('filter@index');
$this->assertTrue($_SERVER['test-on-post']);
$_SERVER['test-on-get-put'] = false;
$_SERVER['REQUEST_METHOD'] = 'POST';
Controller::call('filter@index');
$this->assertFalse($_SERVER['test-on-get-put']);
$_SERVER['REQUEST_METHOD'] = 'PUT';
Controller::call('filter@index');
$this->assertTrue($_SERVER['test-on-get-put']);
}
public function testGlobalBeforeFilterIsNotCalledByController()
{
$_SERVER['before'] = false;
$_SERVER['after'] = false;
Controller::call('auth@index');
$this->assertFalse($_SERVER['before']);
$this->assertFalse($_SERVER['after']);
}
/**
* Test that before filters can override the controller response.
*
* @group laravel
*/
public function testBeforeFiltersCanOverrideResponses()
{
$this->assertEquals('Filtered!', Controller::call('filter@login')->content);
}
/**
* Test that after filters do not affect the response.
*
* @group laravel
*/
public function testAfterFiltersDoNotAffectControllerResponse()
{
$this->assertEquals('action_logout', Controller::call('filter@logout')->content);
}
/**
* Test that filter parameters are passed to the filter.
*
* @group laravel
*/
public function testFilterParametersArePassedToTheFilter()
{
$this->assertEquals('12', Controller::call('filter@edit')->content);
}
/**
* Test that multiple filters can be assigned to a single method.
*
* @group laravel
*/
public function testMultipleFiltersCanBeAssignedToAnAction()
{
$_SERVER['test-multi-1'] = false;
$_SERVER['test-multi-2'] = false;
Controller::call('filter@save');
$this->assertTrue($_SERVER['test-multi-1']);
$this->assertTrue($_SERVER['test-multi-2']);
}
/**
* Test Restful controllers respond by request method.
*
* @group laravel
*/
public function testRestfulControllersRespondWithRestfulMethods()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->assertEquals('get_index', Controller::call('restful@index')->content);
$_SERVER['REQUEST_METHOD'] = 'PUT';
$this->assertEquals(404, Controller::call('restful@index')->status);
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->assertEquals('post_index', Controller::call('restful@index')->content);
}
/**
* Test that the template is returned by template controllers.
*
* @group laravel
*/
public function testTemplateControllersReturnTheTemplate()
{
$response = Controller::call('template.basic@index');
$home = file_get_contents(path('app').'views/home/index.php');
$this->assertEquals($home, $response->content);
}
/**
* Test that controller templates can be named views.
*
* @group laravel
*/
public function testControllerTemplatesCanBeNamedViews()
{
View::name('home.index', 'home');
$response = Controller::call('template.named@index');
$home = file_get_contents(path('app').'views/home/index.php');
$this->assertEquals($home, $response->content);
View::$names = array();
}
/**
* Test that the "layout" method is called on the controller.
*
* @group laravel
*/
public function testTheTemplateCanBeOverriden()
{
$this->assertEquals('Layout', Controller::call('template.override@index')->content);
}
/**
* Test the Controller::resolve method.
*
* @group laravel
*/
public function testResolveMethodChecksTheIoCContainer()
{
IoC::register('controller: home', function()
{
require_once path('app').'controllers/home.php';
$controller = new Home_Controller;
$controller->foo = 'bar';
return $controller;
});
$controller = Controller::resolve(DEFAULT_BUNDLE, 'home');
$this->assertEquals('bar', $controller->foo);
}
}

View File

@@ -0,0 +1,94 @@
<?php namespace Laravel;
/**
* Stub the global setcookie method into the Laravel namespace.
*/
function setcookie($name, $value, $time, $path, $domain, $secure)
{
$_SERVER['cookie.stub'][$name] = compact('name', 'value', 'time', 'path', 'domain', 'secure');
}
function headers_sent()
{
return $_SERVER['function.headers_sent'];
}
class CookieTest extends \PHPUnit_Framework_TestCase {
/**
* Setup the test environment.
*/
public function setUp()
{
Cookie::$jar = array();
}
/**
* Tear down the test environment.
*/
public function tearDown()
{
Cookie::$jar = array();
}
/**
* Test Cookie::has method.
*
* @group laravel
*/
public function testHasMethodIndicatesIfCookieInSet()
{
Cookie::$jar['foo'] = array('value' => 'bar');
$this->assertTrue(Cookie::has('foo'));
$this->assertFalse(Cookie::has('bar'));
Cookie::put('baz', 'foo');
$this->assertTrue(Cookie::has('baz'));
}
/**
* Test the Cookie::get method.
*
* @group laravel
*/
public function testGetMethodCanReturnValueOfCookies()
{
Cookie::$jar['foo'] = array('value' => 'bar');
$this->assertEquals('bar', Cookie::get('foo'));
Cookie::put('bar', 'baz');
$this->assertEquals('baz', Cookie::get('bar'));
}
/**
* Test Cookie::forever method.
*
* @group laravel
*/
public function testForeverShouldUseATonOfMinutes()
{
Cookie::forever('foo', 'bar');
$this->assertEquals('bar', Cookie::$jar['foo']['value']);
$this->assertEquals(525600, Cookie::$jar['foo']['minutes']);
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']);
}
/**
* Test the Cookie::forget method.
*
* @group laravel
*/
public function testForgetSetsCookieWithExpiration()
{
Cookie::forget('bar', 'path', 'domain', true);
$this->assertEquals(-2000, Cookie::$jar['bar']['minutes']);
$this->assertEquals('path', Cookie::$jar['bar']['path']);
$this->assertEquals('domain', Cookie::$jar['bar']['domain']);
$this->assertTrue(Cookie::$jar['bar']['secure']);
}
}

View File

@@ -0,0 +1,74 @@
<?php
class DatabaseTest extends PHPUnit_Framework_TestCase {
/**
* Set up the test environment.
*/
public function setUp()
{
DB::$connections = array();
}
/**
* Tear down the test environment.
*/
public function tearDown()
{
DB::$connections = array();
}
/**
* Test the DB::connection method.
*
* @group laravel
*/
public function testConnectionMethodReturnsConnection()
{
$connection = DatabaseConnectStub::connection();
$this->assertTrue(isset(DB::$connections[Config::get('database.default')]));
$connection = DatabaseConnectStub::connection('mysql');
$this->assertTrue(isset(DB::$connections['mysql']));
$this->assertEquals(DB::$connections['mysql']->pdo->laravel_config, Config::get('database.connections.mysql'));
}
/**
* Test the DB::profile method.
*
* @group laravel
*/
public function testProfileMethodReturnsQueries()
{
Laravel\Database\Connection::$queries = array('Taylor');
$this->assertEquals(array('Taylor'), DB::profile());
Laravel\Database\Connection::$queries = array();
}
/**
* Test the __callStatic method.
*
* @group laravel
*/
public function testConnectionMethodsCanBeCalledStaticly()
{
$this->assertEquals('sqlite', DB::driver());
}
}
class DatabaseConnectStub extends Laravel\Database {
protected static function connect($config) { return new PDOStub($config); }
}
class PDOStub extends PDO {
public $laravel_config;
public function __construct($config) { $this->laravel_config = $config; }
public function foo() { return 'foo'; }
}

View File

@@ -0,0 +1,43 @@
<?php
class EventTest extends PHPUnit_Framework_TestCase {
/**
* Tear down the testing environment.
*/
public function tearDown()
{
unset(Event::$events['test.event']);
}
/**
* Test basic event firing.
*
* @group laravel
*/
public function testListenersAreFiredForEvents()
{
Event::listen('test.event', function() { return 1; });
Event::listen('test.event', function() { return 2; });
$responses = Event::fire('test.event');
$this->assertEquals(1, $responses[0]);
$this->assertEquals(2, $responses[1]);
}
/**
* Test parameters can be passed to event listeners.
*
* @group laravel
*/
public function testParametersCanBePassedToEvents()
{
Event::listen('test.event', function($var) { return $var; });
$responses = Event::fire('test.event', array('Taylor'));
$this->assertEquals('Taylor', $responses[0]);
}
}

View File

@@ -0,0 +1,50 @@
<?php
use Laravel\Fluent;
class FluentTest extends PHPUnit_Framework_TestCase {
/**
* Test the Fluent constructor.
*
* @group laravel
*/
public function testAttributesAreSetByConstructor()
{
$array = array('name' => 'Taylor', 'age' => 25);
$fluent = new Fluent($array);
$this->assertEquals($array, $fluent->attributes);
}
/**
* Test the Fluent::get method.
*
* @group laravel
*/
public function testGetMethodReturnsAttribute()
{
$fluent = new Fluent(array('name' => 'Taylor'));
$this->assertEquals('Taylor', $fluent->get('name'));
$this->assertEquals('Default', $fluent->get('foo', 'Default'));
$this->assertEquals('Taylor', $fluent->name);
$this->assertNull($fluent->foo);
}
public function testMagicMethodsCanBeUsedToSetAttributes()
{
$fluent = new Fluent;
$fluent->name = 'Taylor';
$fluent->developer();
$fluent->age(25);
$this->assertEquals('Taylor', $fluent->name);
$this->assertTrue($fluent->developer);
$this->assertEquals(25, $fluent->age);
$this->assertInstanceOf('Laravel\\Fluent', $fluent->programmer());
}
}

View File

@@ -0,0 +1,37 @@
<?php
class HashTest extends PHPUnit_Framework_TestCase {
/**
* Test the Hash::make method.
*
* @group laravel
*/
public function testHashProducesValidBcryptHash()
{
$this->assertTrue(strlen(Hash::make('taylor')) == 60);
}
/**
* Test the Hash::check method.
*
* @group laravel
*/
public function testHashCheckFailsWhenNotMatching()
{
$hash = Hash::make('taylor');
$this->assertFalse(Hash::check('foo', $hash));
}
/**
* Test the Hash::check method.
*
* @group laravel
*/
public function testHashCheckPassesWhenMatches()
{
$this->assertTrue(Hash::check('taylor', Hash::make('taylor')));
}
}

View File

@@ -0,0 +1,174 @@
<?php
class InputTest extends PHPUnit_Framework_TestCase {
/**
* Setup the testing environment.
*/
public function setUp()
{
Config::set('application.key', 'foo');
}
/**
* Tear down the testing environemnt.
*/
public function tearDown()
{
// @todo clear httpfoundation request data
Config::set('application.key', '');
Session::$instance = null;
}
/**
* Test the Input::all method.
*
* @group laravel
*/
public function testAllMethodReturnsInputAndFiles()
{
Request::foundation()->request->add(array('name' => 'Taylor'));
$_FILES = array('age' => 25);
$this->assertEquals(Input::all(), array('name' => 'Taylor', 'age' => 25));
}
/**
* Test the Input::has method.
*
* @group laravel
*/
public function testHasMethodIndicatesTheExistenceOfInput()
{
$this->assertFalse(Input::has('foo'));
Request::foundation()->request->add(array('name' => 'Taylor'));
$this->assertTrue(Input::has('name'));
}
/**
* Test the Input::get method.
*
* @group laravel
*/
public function testGetMethodReturnsInputValue()
{
Request::foundation()->request->add(array('name' => 'Taylor'));
$this->assertEquals('Taylor', Input::get('name'));
$this->assertEquals('Default', Input::get('foo', 'Default'));
}
/**
* Test the Input::only method.
*
* @group laravel
*/
public function testOnlyMethodReturnsSubsetOfInput()
{
Request::foundation()->request->add(array('name' => 'Taylor', 'age' => 25));
$this->assertEquals(array('name' => 'Taylor'), Input::only(array('name')));
}
/**
* Test the Input::except method.
*
* @group laravel
*/
public function testExceptMethodReturnsSubsetOfInput()
{
Request::foundation()->request->add(array('name' => 'Taylor', 'age' => 25));
$this->assertEquals(array('age' => 25), Input::except(array('name')));
}
/**
* Test the Input::old method.
*
* @group laravel
*/
public function testOldInputCanBeRetrievedFromSession()
{
$this->setSession();
Session::$instance->session['data']['laravel_old_input'] = array('name' => 'Taylor');
$this->assertNull(Input::old('foo'));
$this->assertTrue(Input::had('name'));
$this->assertFalse(Input::had('foo'));
$this->assertEquals('Taylor', Input::old('name'));
}
/**
* Test the Input::file method.
*
* @group laravel
*/
public function testFileMethodReturnsFromFileArray()
{
$_FILES['foo'] = array('name' => 'Taylor', 'size' => 100);
$this->assertEquals('Taylor', Input::file('foo.name'));
$this->assertEquals(array('name' => 'Taylor', 'size' => 100), Input::file('foo'));
}
/**
* Test the Input::flash method.
*
* @group laravel
*/
public function testFlashMethodFlashesInputToSession()
{
$this->setSession();
$input = array('name' => 'Taylor', 'age' => 25);
Request::foundation()->request->add($input);
Input::flash();
$this->assertEquals($input, Session::$instance->session['data'][':new:']['laravel_old_input']);
Input::flash('only', array('name'));
$this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['laravel_old_input']);
Input::flash('except', array('name'));
$this->assertEquals(array('age' => 25), Session::$instance->session['data'][':new:']['laravel_old_input']);
}
/**
* Test the Input::flush method.
*
* @group laravel
*/
public function testFlushMethodClearsFlashedInput()
{
$this->setSession();
$input = array('name' => 'Taylor');
Request::foundation()->request->add($input);
Input::flash();
$this->assertEquals($input, Session::$instance->session['data'][':new:']['laravel_old_input']);
Input::flush();
$this->assertEquals(array(), Session::$instance->session['data'][':new:']['laravel_old_input']);
}
/**
* Set the session payload instance.
*/
protected function setSession()
{
$driver = $this->getMock('Laravel\\Session\\Drivers\\Driver');
Session::$instance = new Laravel\Session\Payload($driver);
}
}

View File

@@ -0,0 +1,74 @@
<?php
class IoCTest extends PHPUnit_Framework_TestCase {
/**
* Test IoC::register and IoC::resolve.
*
* @group laravel
*/
public function testRegisteredClassCanBeResolved()
{
IoC::register('foo', function()
{
return 'Taylor';
});
$this->assertEquals('Taylor', IoC::resolve('foo'));
}
/**
* Test that singletons are created once.
*
* @group laravel
*/
public function testSingletonsAreCreatedOnce()
{
IoC::singleton('foo', function()
{
return new StdClass;
});
$object = IoC::resolve('foo');
$this->assertTrue($object === IoC::resolve('foo'));
}
/**
* Test the IoC::instance method.
*
* @group laravel
*/
public function testInstancesAreReturnedBySingleton()
{
$object = new StdClass;
IoC::instance('bar', $object);
$this->assertTrue($object === IoC::resolve('bar'));
}
/**
* Test the IoC::registered method.
*/
public function testRegisteredMethodIndicatesIfRegistered()
{
IoC::register('foo', function() {});
$this->assertTrue(IoC::registered('foo'));
$this->assertFalse(IoC::registered('baz'));
}
/**
* Test the IoC::controller method.
*
* @group laravel
*/
public function testControllerMethodRegistersAController()
{
IoC::register('controller: ioc.test', function() {});
$this->assertTrue(IoC::registered('controller: ioc.test'));
}
}

View File

@@ -0,0 +1,68 @@
<?php
class LangTest extends PHPUnit_Framework_TestCase {
/**
* Test the Lang::line method.
*
* @group laravel
*/
public function testGetMethodCanGetFromDefaultLanguage()
{
$validation = require path('app').'language/en/validation.php';
$this->assertEquals($validation['required'], Lang::line('validation.required')->get());
$this->assertEquals('Taylor', Lang::line('validation.foo')->get(null, 'Taylor'));
}
/**
* Test the Lang::line method.
*
* @group laravel
*/
public function testGetMethodCanGetLinesForAGivenLanguage()
{
$validation = require path('app').'language/sp/validation.php';
$this->assertEquals($validation['required'], Lang::line('validation.required')->get('sp'));
}
/**
* Test the __toString method.
*
* @group laravel
*/
public function testLineCanBeCastAsString()
{
$validation = require path('app').'language/en/validation.php';
$this->assertEquals($validation['required'], (string) Lang::line('validation.required'));
}
/**
* Test that string replacements are made on lines.
*
* @group laravel
*/
public function testReplacementsAreMadeOnLines()
{
$validation = require path('app').'language/en/validation.php';
$line = str_replace(':attribute', 'e-mail', $validation['required']);
$this->assertEquals($line, Lang::line('validation.required', array('attribute' => 'e-mail'))->get());
}
/**
* Test the Lang::has method.
*
* @group laravel
*/
public function testHasMethodIndicatesIfLangaugeLineExists()
{
$this->assertTrue(Lang::has('validation'));
$this->assertTrue(Lang::has('validation.required'));
$this->assertFalse(Lang::has('validation.foo'));
}
}

View File

@@ -0,0 +1,115 @@
<?php
class MessagesTest extends PHPUnit_Framework_TestCase {
/**
* The Messages instance.
*
* @var Messages
*/
public $messages;
/**
* Setup the test environment.
*/
public function setUp()
{
$this->messages = new Laravel\Messages;
}
/**
* Test the Messages::add method.
*
* @group laravel
*/
public function testAddingMessagesDoesNotCreateDuplicateMessages()
{
$this->messages->add('email', 'test');
$this->messages->add('email', 'test');
$this->assertCount(1, $this->messages->messages);
}
/**
* Test the Messages::add method.
*
* @group laravel
*/
public function testAddMethodPutsMessageInMessagesArray()
{
$this->messages->add('email', 'test');
$this->assertArrayHasKey('email', $this->messages->messages);
$this->assertEquals('test', $this->messages->messages['email'][0]);
}
/**
* Test the Messages::has method.
*
* @group laravel
*/
public function testHasMethodReturnsTrue()
{
$this->messages->add('email', 'test');
$this->assertTrue($this->messages->has('email'));
}
/**
* Test the Messages::has method.
*
* @group laravel
*/
public function testHasMethodReturnsFalse()
{
$this->assertFalse($this->messages->has('something'));
}
/**
* Test the Messages::first method.
*
* @group laravel
*/
public function testFirstMethodReturnsSingleString()
{
$this->messages->add('email', 'test');
$this->assertEquals('test', $this->messages->first('email'));
$this->assertEquals('', $this->messages->first('something'));
}
/**
* Test the Messages::get method.
*
* @group laravel
*/
public function testGetMethodReturnsAllMessagesForAttribute()
{
$messages = array('email' => array('something', 'else'));
$this->messages->messages = $messages;
$this->assertEquals(array('something', 'else'), $this->messages->get('email'));
}
/**
* Test the Messages::all method.
*
* @group laravel
*/
public function testAllMethodReturnsAllErrorMessages()
{
$messages = array('email' => array('something', 'else'), 'name' => array('foo'));
$this->messages->messages = $messages;
$this->assertEquals(array('something', 'else', 'foo'), $this->messages->all());
}
/**
* Test the Messages::get method.
*
* @group laravel
*/
public function testMessagesRespectFormat()
{
$this->messages->add('email', 'test');
$this->assertEquals('<p>test</p>', $this->messages->first('email', '<p>:message</p>'));
$this->assertEquals(array('<p>test</p>'), $this->messages->get('email', '<p>:message</p>'));
$this->assertEquals(array('<p>test</p>'), $this->messages->all('<p>:message</p>'));
}
}

View File

@@ -0,0 +1,48 @@
<?php
class QueryTest extends PHPUnit_Framework_TestCase {
/**
* Test the "find" method.
*
* @group laravel
*/
public function testFindMethodCanReturnByID()
{
$this->assertEquals('taylor@example.com', $this->query()->find(1)->email);
}
/**
* Test the select method.
*
* @group laravel
*/
public function testSelectMethodLimitsColumns()
{
$result = $this->query()->select(array('email'))->first();
$this->assertTrue(isset($result->email));
$this->assertFalse(isset($result->name));
}
/**
* Test the raw_where method.
*
* @group laravel
*/
public function testRawWhereCanBeUsed()
{
}
/**
* Get the query instance for the test case.
*
* @return Query
*/
protected function query()
{
return DB::table('query_test');
}
}

View File

@@ -0,0 +1,143 @@
<?php
use Laravel\Routing\Router;
class RedirectTest extends PHPUnit_Framework_TestCase {
/**
* Setup the test environment.
*/
public function setUp()
{
Config::set('session.driver', 'foo');
Router::$routes = array();
Router::$names = array();
Config::set('application.url', 'http://localhost');
Config::set('application.index', '');
}
/**
* Destroy the test environment.
*/
public function tearDown()
{
// @todo clear httpfoundation request data
Config::set('session.driver', '');
Router::$routes = array();
Router::$names = array();
Config::set('application.url', '');
Config::set('application.index', 'index.php');
Session::$instance = null;
}
/**
* Test the Redirect::to method.
*
* @group laravel
*/
public function testSimpleRedirectSetsCorrectHeaders()
{
$redirect = Redirect::to('user/profile');
$this->assertEquals(302, $redirect->status);
$this->assertEquals('http://localhost/user/profile', $redirect->headers['location']);
$redirect = Redirect::to('user/profile', 301, true);
$this->assertEquals(301, $redirect->status);
$this->assertEquals('https://localhost/user/profile', $redirect->headers['location']);
$redirect = Redirect::to_secure('user/profile', 301);
$this->assertEquals(301, $redirect->status);
$this->assertEquals('https://localhost/user/profile', $redirect->headers['location']);
}
/**
* Test the Redirect::to_route method.
*
* @group laravel
*/
public function testRedirectsCanBeGeneratedForNamedRoutes()
{
Route::get('redirect', array('as' => 'redirect'));
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']);
}
/**
* Test the Redirect::with method.
*
* @group laravel
*/
public function testWithMethodFlashesItemToSession()
{
$this->setSession();
$redirect = Redirect::to('')->with('name', 'Taylor');
$this->assertEquals('Taylor', Session::$instance->session['data'][':new:']['name']);
}
/**
* Test the Redirect::with_input function.
*
* @group laravel
*/
public function testWithInputMethodFlashesInputToTheSession()
{
$this->setSession();
$input = array('name' => 'Taylor', 'age' => 25);
Request::foundation()->request->add($input);
$redirect = Redirect::to('')->with_input();
$this->assertEquals($input, Session::$instance->session['data'][':new:']['laravel_old_input']);
$redirect = Redirect::to('')->with_input('only', array('name'));
$this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['laravel_old_input']);
$redirect = Redirect::to('')->with_input('except', array('name'));
$this->assertEquals(array('age' => 25), Session::$instance->session['data'][':new:']['laravel_old_input']);
}
/**
* Test the Redirect::with_errors method.
*
* @group laravel
*/
public function testWithErrorsFlashesErrorsToTheSession()
{
$this->setSession();
Redirect::to('')->with_errors(array('name' => 'Taylor'));
$this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['errors']);
$validator = Validator::make(array(), array());
$validator->errors = array('name' => 'Taylor');
Redirect::to('')->with_errors($validator);
$this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['errors']);
}
/**
* Set the session payload instance.
*/
protected function setSession()
{
$driver = $this->getMock('Laravel\\Session\\Drivers\\Driver');
Session::$instance = new Laravel\Session\Payload($driver);
}
}

View File

@@ -0,0 +1,147 @@
<?php
class SessionPayloadTokenStub {
public function token() { return 'Taylor'; }
}
class RequestTest extends PHPUnit_Framework_TestCase {
/**
* Tear down the test environment.
*/
public function tearDown()
{
$_POST = array();
$_SERVER = array();
Request::$route = null;
Session::$instance = null;
}
/**
* Test the Request::method method.
*
* @group laravel
*/
public function testMethodReturnsTheHTTPRequestMethod()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->assertEquals('POST', Request::method());
$_POST[Request::spoofer] = 'PUT';
$this->assertEquals('PUT', Request::method());
}
/**
* Test the Request::server method.
*
* @group laravel
*/
public function testServerMethodReturnsFromServerArray()
{
$_SERVER = array('TEST' => 'something', 'USER' => array('NAME' => 'taylor'));
$this->assertEquals('something', Request::server('test'));
$this->assertEquals('taylor', Request::server('user.name'));
}
/**
* Test the Request::ip method.
*
* @group laravel
*/
public function testIPMethodReturnsClientIPAddress()
{
$_SERVER['REMOTE_ADDR'] = 'something';
$this->assertEquals('something', Request::ip());
$_SERVER['HTTP_CLIENT_IP'] = 'something';
$this->assertEquals('something', Request::ip());
$_SERVER['HTTP_X_FORWARDED_FOR'] = 'something';
$this->assertEquals('something', Request::ip());
$_SERVER = array();
$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.
*
* @group laravel
*/
public function testSecureMethodsIndicatesIfHTTPS()
{
$_SERVER['HTTPS'] = 'on';
$this->assertTrue(Request::secure());
$_SERVER['HTTPS'] = 'off';
$this->assertFalse(Request::secure());
}
/**
* Test the Request::ajax method.
*
* @group laravel
*/
public function testAjaxMethodIndicatesWhenAjax()
{
$this->assertFalse(Request::ajax());
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
$this->assertTrue(Request::ajax());
}
/**
* Test the Request::forged method.
*
* @group laravel
*/
public function testForgedMethodIndicatesIfRequestWasForged()
{
Session::$instance = new SessionPayloadTokenStub;
$input = array(Session::csrf_token => 'Foo');
Request::foundation()->request->add($input);
$this->assertTrue(Request::forged());
$input = array(Session::csrf_token => 'Taylor');
Request::foundation()->request->add($input);
$this->assertFalse(Request::forged());
}
/**
* Test the Request::route method.
*
* @group laravel
*/
public function testRouteMethodReturnsStaticRoute()
{
Request::$route = 'Taylor';
$this->assertEquals('Taylor', Request::route());
}
}

View File

@@ -0,0 +1,88 @@
<?php
class ResponseTest extends PHPUnit_Framework_TestCase {
/**
* Test the Response::make method.
*
* @group laravel
*/
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);
}
/**
* Test the Response::view method.
*
* @group laravel
*/
public function testViewMethodSetsContentToView()
{
$response = Response::view('home.index', array('name' => 'Taylor'));
$this->assertEquals('home.index', $response->content->view);
$this->assertEquals('Taylor', $response->content->data['name']);
}
/**
* Test the Response::error method.
*
* @group laravel
*/
public function testErrorMethodSetsContentToErrorView()
{
$response = Response::error('404', array('name' => 'Taylor'));
$this->assertEquals(404, $response->status);
$this->assertEquals('error.404', $response->content->view);
$this->assertEquals('Taylor', $response->content->data['name']);
}
/**
* Test the Response::prepare method.
*
* @group laravel
*/
public function testPrepareMethodCreatesAResponseInstanceFromGivenValue()
{
$response = Response::prepare('Taylor');
$this->assertInstanceOf('Laravel\\Response', $response);
$this->assertEquals('Taylor', $response->content);
$response = Response::prepare(new Response('Taylor'));
$this->assertInstanceOf('Laravel\\Response', $response);
$this->assertEquals('Taylor', $response->content);
}
/**
* Test the Response::header method.
*
* @group laravel
*/
public function testHeaderMethodSetsValueInHeaderArray()
{
$response = Response::make('')->header('foo', 'bar');
$this->assertEquals('bar', $response->headers['foo']);
}
/**
* Test the Response::status method.
*
* @group laravel
*/
public function testStatusMethodSetsStatusCode()
{
$response = Response::make('')->status(404);
$this->assertEquals(404, $response->status);
}
}

View File

@@ -0,0 +1,179 @@
<?php
use Laravel\Routing\Route;
class RouteTest extends PHPUnit_Framework_TestCase {
/**
* Tear down the testing environment.
*/
public static function tearDownAfterClass()
{
unset($_SERVER['REQUEST_METHOD']);
unset(Filter::$filters['test-after']);
unset(Filter::$filters['test-before']);
unset(Filter::$filters['test-params']);
unset(Filter::$filters['test-multi-1']);
unset(Filter::$filters['test-multi-2']);
}
/**
* Destroy the testing environment.
*/
public function tearDown()
{
Request::$route = null;
}
/**
* Tests the Route::is method.
*
* @group laravel
*/
public function testIsMethodIndicatesIfTheRouteHasAGivenName()
{
$route = new Route('GET', '/', array('as' => 'profile'));
$this->assertTrue($route->is('profile'));
$this->assertFalse($route->is('something'));
}
/**
* Test the basic execution of a route.
*
* @group laravel
*/
public function testBasicRoutesCanBeExecutedProperly()
{
$route = new Route('GET', '', array(function() { return 'Route!'; }));
$this->assertEquals('Route!', $route->call()->content);
$this->assertInstanceOf('Laravel\\Response', $route->call());
}
/**
* Test that route parameters are passed into the handlers.
*
* @group laravel
*/
public function testRouteParametersArePassedIntoTheHandler()
{
$route = new Route('GET', '', array(function($var) { return $var; }), array('Taylor'));
$this->assertEquals('Taylor', $route->call()->content);
$this->assertInstanceOf('Laravel\\Response', $route->call());
}
/**
* Test that calling a route calls the global before and after filters.
*
* @group laravel
*/
public function testCallingARouteCallsTheBeforeAndAfterFilters()
{
$route = new Route('GET', '', array(function() { return 'Hi!'; }));
$_SERVER['before'] = false;
$_SERVER['after'] = false;
$route->call();
$this->assertTrue($_SERVER['before']);
$this->assertTrue($_SERVER['after']);
}
/**
* Test that before filters override the route response.
*
* @group laravel
*/
public function testBeforeFiltersOverrideTheRouteResponse()
{
Filter::register('test-before', function()
{
return 'Filtered!';
});
$route = new Route('GET', '', array('before' => 'test-before', function() {
return 'Route!';
}));
$this->assertEquals('Filtered!', $route->call()->content);
}
/**
* Test that after filters do not affect the route response.
*
* @group laravel
*/
public function testAfterFilterDoesNotAffectTheResponse()
{
$_SERVER['test-after'] = false;
Filter::register('test-after', function()
{
$_SERVER['test-after'] = true;
return 'Filtered!';
});
$route = new Route('GET', '', array('after' => 'test-after', function()
{
return 'Route!';
}));
$this->assertEquals('Route!', $route->call()->content);
$this->assertTrue($_SERVER['test-after']);
}
/**
* Test that the route calls the appropriate controller method when delegating.
*
* @group laravel
*/
public function testControllerActionCalledWhenDelegating()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$route = new Route('GET', '', array('uses' => 'auth@index'));
$this->assertEquals('action_index', $route->call()->content);
}
/**
* Test that filter parameters are passed to the filter.
*
* @group laravel
*/
public function testFilterParametersArePassedToFilter()
{
Filter::register('test-params', function($var1, $var2)
{
return $var1.$var2;
});
$route = new Route('GET', '', array('before' => 'test-params:1,2'));
$this->assertEquals('12', $route->call()->content);
}
/**
* Test that multiple filters can be assigned to a route.
*
* @group laravel
*/
public function testMultipleFiltersCanBeAssignedToARoute()
{
$_SERVER['test-multi-1'] = false;
$_SERVER['test-multi-2'] = false;
Filter::register('test-multi-1', function() { $_SERVER['test-multi-1'] = true; });
Filter::register('test-multi-2', function() { $_SERVER['test-multi-2'] = true; });
$route = new Route('GET', '', array('before' => 'test-multi-1|test-multi-2'));
$route->call();
$this->assertTrue($_SERVER['test-multi-1']);
$this->assertTrue($_SERVER['test-multi-2']);
}
}

View File

@@ -0,0 +1,160 @@
<?php
use Laravel\Routing\Router;
class RoutingTest extends PHPUnit_Framework_TestCase {
/**
* Destroy the testing environment.
*/
public function setUp()
{
Bundle::$started = array();
Bundle::$routed = array();
Router::$names = array();
Router::$routes = array();
}
/**
* Destroy the testing environment.
*/
public function tearDown()
{
Bundle::$started = array();
Bundle::$routed = array();
Router::$names = array();
Router::$routes = array();
}
/**
* Test the Router::find method.
*
* @group laravel
*/
public function testNamedRoutesCanBeLocatedByTheRouter()
{
Route::get('/', array('as' => 'home'));
Route::get('dashboard', array('as' => 'dashboard'));
$home = Router::find('home');
$dashboard = Router::find('dashboard');
$this->assertTrue(isset($home['/']));
$this->assertTrue(isset($dashboard['dashboard']));
}
/**
* Test the basic routing mechanism.
*
* @group laravel
*/
public function testBasicRouteCanBeRouted()
{
Route::get('/', function() {});
Route::get('home, main', function() {});
$this->assertEquals('/', Router::route('GET', '/')->uri);
$this->assertEquals('home', Router::route('GET', 'home')->uri);
$this->assertEquals('main', Router::route('GET', 'main')->uri);
}
/**
* Test that the router can handle basic wildcards.
*
* @group laravel
*/
public function testWildcardRoutesCanBeRouted()
{
Route::get('user/(:num)', function() {});
Route::get('profile/(:any)/(:num)', function() {});
$this->assertNull(Router::route('GET', 'user/1.5'));
$this->assertNull(Router::route('GET', 'user/taylor'));
$this->assertEquals(array(25), Router::route('GET', 'user/25')->parameters);
$this->assertEquals('user/(:num)', Router::route('GET', 'user/1')->uri);
$this->assertNull(Router::route('GET', 'profile/1/otwell'));
$this->assertNull(Router::route('POST', 'profile/taylor/1'));
$this->assertNull(Router::route('GET', 'profile/taylor/otwell'));
$this->assertNull(Router::route('GET', 'profile/taylor/1/otwell'));
$this->assertEquals(array('taylor', 25), Router::route('GET', 'profile/taylor/25')->parameters);
$this->assertEquals('profile/(:any)/(:num)', Router::route('GET', 'profile/taylor/1')->uri);
}
/**
* Test that optional wildcards can be routed.
*
* @group laravel
*/
public function testOptionalWildcardsCanBeRouted()
{
Route::get('user/(:num?)', function() {});
Route::get('profile/(:any)/(:any?)', function() {});
$this->assertNull(Router::route('GET', 'user/taylor'));
$this->assertEquals('user/(:num?)', Router::route('GET', 'user')->uri);
$this->assertEquals(array(25), Router::route('GET', 'user/25')->parameters);
$this->assertEquals('user/(:num?)', Router::route('GET', 'user/1')->uri);
$this->assertNull(Router::route('GET', 'profile/taylor/otwell/test'));
$this->assertEquals('profile/(:any)/(:any?)', Router::route('GET', 'profile/taylor')->uri);
$this->assertEquals('profile/(:any)/(:any?)', Router::route('GET', 'profile/taylor/25')->uri);
$this->assertEquals('profile/(:any)/(:any?)', Router::route('GET', 'profile/taylor/otwell')->uri);
$this->assertEquals(array('taylor', 'otwell'), Router::route('GET', 'profile/taylor/otwell')->parameters);
}
/**
* Test that basic controller routing is working.
*
* @group laravel
*/
public function testBasicRouteToControllerIsRouted()
{
$this->assertEquals('auth@(:1)', Router::route('GET', 'auth')->action['uses']);
$this->assertEquals('home@(:1)', Router::route('GET', 'home/index')->action['uses']);
$this->assertEquals('home@(:1)', Router::route('GET', 'home/profile')->action['uses']);
$this->assertEquals('admin.panel@(:1)', Router::route('GET', 'admin/panel')->action['uses']);
$this->assertEquals('admin.panel@(:1)', Router::route('GET', 'admin/panel/show')->action['uses']);
}
/**
* Test basic bundle route resolution.
*
* @group laravel
*/
public function testRoutesToBundlesCanBeResolved()
{
$this->assertNull(Router::route('GET', 'dashboard/foo'));
$this->assertEquals('dashboard', Router::route('GET', 'dashboard')->uri);
}
/**
* Test bundle controller route resolution.
*
* @group laravel
*/
public function testBundleControllersCanBeResolved()
{
$this->assertEquals('dashboard::panel@(:1)', Router::route('GET', 'dashboard/panel')->action['uses']);
$this->assertEquals('dashboard::panel@(:1)', Router::route('GET', 'dashboard/panel/show')->action['uses']);
}
/**
* Test foreign characters can be used in routes.
*
* @group laravel
*/
public function testForeignCharsInRoutes()
{
Route::get(urlencode('مدرس_رياضيات').'/(:any)', function() {});
Route::get(urlencode('مدرس_رياضيات'), function() {});
Route::get(urlencode('ÇœŪ'), function() {});
Route::get(urlencode('私は料理が大好き'), function() {});
$this->assertEquals(array(urlencode('مدرس_رياضيات')), Router::route('GET', urlencode('مدرس_رياضيات').'/'.urlencode('مدرس_رياضيات'))->parameters);
$this->assertEquals(urlencode('مدرس_رياضيات'), Router::route('GET', urlencode('مدرس_رياضيات'))->uri);
$this->assertEquals(urlencode('ÇœŪ'), Router::route('GET', urlencode('ÇœŪ'))->uri);
$this->assertEquals(urlencode('私は料理が大好き'), Router::route('GET', urlencode('私は料理が大好き'))->uri);
}
}

View File

@@ -0,0 +1,441 @@
<?php
use Laravel\Session;
use Laravel\Session\Payload;
class DummyPayload {
public function test() { return 'Foo'; }
}
class SessionTest extends PHPUnit_Framework_TestCase {
/**
* Setup the testing environment.
*/
public function setUp()
{
Config::set('application.key', 'foo');
Session::$instance = null;
}
/**
* Tear down the testing environment.
*/
public function tearDown()
{
Config::set('application.key', '');
Session::$instance = null;
}
/**
* Test the __callStatic method.
*
* @group laravel
*/
public function testPayloadCanBeCalledStaticly()
{
Session::$instance = new DummyPayload;
$this->assertEquals('Foo', Session::test());
}
/**
* Test the Session::started method.
*
* @group laravel
*/
public function testStartedMethodIndicatesIfSessionIsStarted()
{
$this->assertFalse(Session::started());
Session::$instance = 'foo';
$this->assertTrue(Session::started());
}
/**
* Test the Payload::load method.
*
* @group laravel
*/
public function testLoadMethodCreatesNewSessionWithNullIDGiven()
{
$payload = $this->getPayload();
$payload->load(null);
$this->verifyNewSession($payload);
}
/**
* Test the Payload::load method.
*
* @group laravel
*/
public function testLoadMethodCreatesNewSessionWhenSessionIsExpired()
{
$payload = $this->getPayload();
$session = $this->getSession();
$session['last_activity'] = time() - 10000;
$payload->driver->expects($this->any())
->method('load')
->will($this->returnValue($session));
$payload->load('foo');
$this->verifyNewSession($payload);
$this->assertTrue($payload->session['id'] !== $session['id']);
}
/**
* Assert that a session is new.
*
* @param Payload $payload
* @return void
*/
protected function verifyNewSession($payload)
{
$this->assertFalse($payload->exists);
$this->assertTrue(isset($payload->session['id']));
$this->assertEquals(array(), $payload->session['data'][':new:']);
$this->assertEquals(array(), $payload->session['data'][':old:']);
$this->assertTrue(isset($payload->session['data'][Session::csrf_token]));
}
/**
* Test the Payload::load method.
*
* @group laravel
*/
public function testLoadMethodSetsValidSession()
{
$payload = $this->getPayload();
$session = $this->getSession();
$payload->driver->expects($this->any())
->method('load')
->will($this->returnValue($session));
$payload->load('foo');
$this->assertEquals($session, $payload->session);
}
/**
* Test the Payload::load method.
*
* @group laravel
*/
public function testLoadMethodSetsCSRFTokenIfDoesntExist()
{
$payload = $this->getPayload();
$session = $this->getSession();
unset($session['data']['csrf_token']);
$payload->driver->expects($this->any())
->method('load')
->will($this->returnValue($session));
$payload->load('foo');
$this->assertEquals('foo', $payload->session['id']);
$this->assertTrue(isset($payload->session['data']['csrf_token']));
}
/**
* Test the various data retrieval methods.
*
* @group laravel
*/
public function testSessionDataCanBeRetrievedProperly()
{
$payload = $this->getPayload();
$payload->session = $this->getSession();
$this->assertTrue($payload->has('name'));
$this->assertEquals('Taylor', $payload->get('name'));
$this->assertFalse($payload->has('foo'));
$this->assertEquals('Default', $payload->get('foo', 'Default'));
$this->assertTrue($payload->has('votes'));
$this->assertEquals(10, $payload->get('votes'));
$this->assertTrue($payload->has('state'));
$this->assertEquals('AR', $payload->get('state'));
}
/**
* Test the various data manipulation methods.
*
* @group laravel
*/
public function testDataCanBeSetProperly()
{
$payload = $this->getPayload();
$payload->session = $this->getSession();
// Test the "put" and "flash" methods.
$payload->put('name', 'Weldon');
$this->assertEquals('Weldon', $payload->session['data']['name']);
$payload->flash('language', 'php');
$this->assertEquals('php', $payload->session['data'][':new:']['language']);
// Test the "reflash" method.
$payload->session['data'][':new:'] = array('name' => 'Taylor');
$payload->session['data'][':old:'] = array('age' => 25);
$payload->reflash();
$this->assertEquals(array('name' => 'Taylor', 'age' => 25), $payload->session['data'][':new:']);
// Test the "keep" method.
$payload->session['data'][':new:'] = array();
$payload->keep(array('age'));
$this->assertEquals(25, $payload->session['data'][':new:']['age']);
}
/**
* Test the Payload::forget method.
*
* @group laravel
*/
public function testSessionDataCanBeForgotten()
{
$payload = $this->getPayload();
$payload->session = $this->getSession();
$this->assertTrue(isset($payload->session['data']['name']));
$payload->forget('name');
$this->assertFalse(isset($payload->session['data']['name']));
}
/**
* Test the Payload::flush method.
*
* @group laravel
*/
public function testFlushMaintainsTokenButDeletesEverythingElse()
{
$payload = $this->getPayload();
$payload->session = $this->getSession();
$this->assertTrue(isset($payload->session['data']['name']));
$payload->flush();
$this->assertFalse(isset($payload->session['data']['name']));
$this->assertEquals('bar', $payload->session['data']['csrf_token']);
$this->assertEquals(array(), $payload->session['data'][':new:']);
$this->assertEquals(array(), $payload->session['data'][':old:']);
}
/**
* Test the Payload::regenerate method.
*
* @group laravel
*/
public function testRegenerateMethodSetsNewIDAndTurnsOffExistenceIndicator()
{
$payload = $this->getPayload();
$payload->sesion = $this->getSession();
$payload->exists = true;
$payload->regenerate();
$this->assertFalse($payload->exists);
$this->assertTrue(strlen($payload->session['id']) == 40);
}
/**
* Test the Payload::token method.
*
* @group laravel
*/
public function testTokenMethodReturnsCSRFToken()
{
$payload = $this->getPayload();
$payload->session = $this->getSession();
$this->assertEquals('bar', $payload->token());
}
/**
* Test the Payload::save method.
*
* @group laravel
*/
public function testSaveMethodCorrectlyCallsDriver()
{
$payload = $this->getPayload();
$session = $this->getSession();
$payload->session = $session;
$payload->exists = true;
$config = Laravel\Config::get('session');
$expect = $session;
$expect['data'][':old:'] = $session['data'][':new:'];
$expect['data'][':new:'] = array();
$payload->driver->expects($this->once())
->method('save')
->with($this->equalTo($expect), $this->equalTo($config), $this->equalTo(true));
$payload->save();
$this->assertEquals($session['data'][':new:'], $payload->session['data'][':old:']);
}
/**
* Test the Payload::save method.
*
* @group laravel
*/
public function testSaveMethodSweepsIfSweeperAndOddsHitWithTimeGreaterThanThreshold()
{
Config::set('session.sweepage', array(100, 100));
$payload = $this->getPayload();
$payload->driver = $this->getMock('Laravel\\Session\\Drivers\\File', array('save', 'sweep'), array(null));
$payload->session = $this->getSession();
$expiration = time() - (Config::get('session.lifetime') * 60);
// Here we set the time to the expected expiration minus 5 seconds, just to
// allow plenty of room for PHP execution. In the next test, we'll do the
// same thing except add 5 seconds to check that the time is between a
// given window.
$payload->driver->expects($this->once())
->method('sweep')
->with($this->greaterThan($expiration - 5));
$payload->save();
Config::set('session.sweepage', array(2, 100));
}
/**
* Test the Payload::save method.
*
* @group laravel
*/
public function testSaveMethodSweepsIfSweeperAndOddsHitWithTimeLessThanThreshold()
{
Config::set('session.sweepage', array(100, 100));
$payload = $this->getPayload();
$payload->driver = $this->getMock('Laravel\\Session\\Drivers\\File', array('save', 'sweep'), array(null));
$payload->session = $this->getSession();
$expiration = time() - (Config::get('session.lifetime') * 60);
$payload->driver->expects($this->once())
->method('sweep')
->with($this->lessThan($expiration + 5));
$payload->save();
Config::set('session.sweepage', array(2, 100));
}
/**
* Test that the session sweeper is never called if not a sweeper.
*
* @group laravel
*/
public function testSweeperShouldntBeCalledIfDriverIsntSweeper()
{
Config::set('session.sweepage', array(100, 100));
$payload = $this->getPayload();
$payload->driver = $this->getMock('Laravel\\Session\\Drivers\\APC', array('save', 'sweep'), array(), '', false);
$payload->session = $this->getSession();
$payload->driver->expects($this->never())->method('sweep');
$payload->save();
Config::set('session.sweepage', array(2, 100));
}
/**
* Test the Payload::save method.
*
* @group laravel
*/
public function testSaveMethodSetsCookieWithCorrectValues()
{
$payload = $this->getPayload();
$payload->session = $this->getSession();
$payload->save();
$this->assertTrue(isset(Cookie::$jar[Config::get('session.cookie')]));
$cookie = Cookie::$jar[Config::get('session.cookie')];
$this->assertEquals('foo', $cookie['value']);
$this->assertEquals(Config::get('session.lifetime'), $cookie['minutes']);
$this->assertEquals(Config::get('session.domain'), $cookie['domain']);
$this->assertEquals(Config::get('session.path'), $cookie['path']);
$this->assertEquals(Config::get('session.secure'), $cookie['secure']);
}
/**
* Test the Session::activity method.
*
* @group laravel
*/
public function testActivityMethodReturnsLastActivity()
{
$payload = $this->getPayload();
$payload->session['last_activity'] = 10;
$this->assertEquals(10, $payload->activity());
}
/**
* Get a session payload instance.
*
* @return Payload
*/
protected function getPayload()
{
return new Payload($this->getMockDriver());
}
/**
* Get a mock driver instance.
*
* @return Driver
*/
protected function getMockDriver()
{
$mock = $this->getMock('Laravel\\Session\\Drivers\\Driver', array('id', 'load', 'save', 'delete'));
$mock->expects($this->any())->method('id')->will($this->returnValue(Str::random(40)));
return $mock;
}
/**
* Get a dummy session.
*
* @return array
*/
protected function getSession()
{
return array(
'id' => 'foo',
'last_activity' => time(),
'data' => array(
'name' => 'Taylor',
'age' => 25,
'csrf_token' => 'bar',
':new:' => array(
'votes' => 10,
),
':old:' => array(
'state' => 'AR',
),
));
}
}

View File

@@ -0,0 +1,133 @@
<?php
class StrTest extends PHPUnit_Framework_TestCase {
/**
* Test the Str::encoding method.
*
* @group laravel
*/
public function testEncodingShouldReturnApplicationEncoding()
{
$this->assertEquals('UTF-8', Config::get('application.encoding'));
Config::set('application.encoding', 'foo');
$this->assertEquals('foo', Config::get('application.encoding'));
Config::set('application.encoding', 'UTF-8');
}
/**
* Test the Str::length method.
*
* @group laravel
*/
public function testStringLengthIsCorrect()
{
$this->assertEquals(6, Str::length('Taylor'));
$this->assertEquals(5, Str::length('ラドクリフ'));
}
/**
* Test the Str::lower method.
*
* @group laravel
*/
public function testStringCanBeConvertedToLowercase()
{
$this->assertEquals('taylor', Str::lower('TAYLOR'));
$this->assertEquals('άχιστη', Str::lower('ΆΧΙΣΤΗ'));
}
/**
* Test the Str::upper method.
*
* @group laravel
*/
public function testStringCanBeConvertedToUppercase()
{
$this->assertEquals('TAYLOR', Str::upper('taylor'));
$this->assertEquals('ΆΧΙΣΤΗ', Str::upper('άχιστη'));
}
/**
* Test the Str::title method.
*
* @group laravel
*/
public function testStringCanBeConvertedToTitleCase()
{
$this->assertEquals('Taylor', Str::title('taylor'));
$this->assertEquals('Άχιστη', Str::title('άχιστη'));
}
/**
* Test the Str::limit method.
*
* @group laravel
*/
public function testStringCanBeLimitedByCharacters()
{
$this->assertEquals('Tay...', Str::limit('Taylor', 3));
$this->assertEquals('Taylor', Str::limit('Taylor', 6));
$this->assertEquals('Tay___', Str::limit('Taylor', 3, '___'));
}
/**
* Test the Str::words method.
*
* @group laravel
*/
public function testStringCanBeLimitedByWords()
{
$this->assertEquals('Taylor...', Str::words('Taylor Otwell', 1));
$this->assertEquals('Taylor___', Str::words('Taylor Otwell', 1, '___'));
$this->assertEquals('Taylor Otwell', Str::words('Taylor Otwell', 3));
}
/**
* Test the Str::plural and Str::singular methods.
*
* @group laravel
*/
public function testStringsCanBeSingularOrPlural()
{
$this->assertEquals('user', Str::singular('users'));
$this->assertEquals('users', Str::plural('user'));
$this->assertEquals('User', Str::singular('Users'));
$this->assertEquals('Users', Str::plural('User'));
$this->assertEquals('user', Str::plural('user', 1));
$this->assertEquals('users', Str::plural('user', 2));
}
/**
* Test the Str::slug method.
*
* @group laravel
*/
public function testStringsCanBeSlugged()
{
$this->assertEquals('my-new-post', Str::slug('My nEw post!!!'));
$this->assertEquals('my_new_post', Str::slug('My nEw post!!!', '_'));
}
/**
* Test the Str::classify method.
*
* @group laravel
*/
public function testStringsCanBeClassified()
{
$this->assertEquals('Something_Else', Str::classify('something.else'));
$this->assertEquals('Something_Else', Str::classify('something_else'));
}
/**
* Test the Str::random method.
*
* @group laravel
*/
public function testRandomStringsCanBeGenerated()
{
$this->assertEquals(40, strlen(Str::random(40)));
}
}

View File

@@ -0,0 +1,59 @@
<?php
class URITest extends PHPUnit_Framework_TestCase {
/**
* Destroy the test environment.
*/
public function tearDown()
{
$_SERVER = array();
URI::$uri = null;
URI::$segments = array();
}
/**
* Test the URI::current method.
*
* @group laravel
* @dataProvider requestUriProvider
*/
public function testCorrectURIIsReturnedByCurrentMethod($uri, $expectation)
{
$_SERVER['REQUEST_URI'] = $uri;
$this->assertEquals($expectation, URI::current());
}
/**
* Test the URI::segment method.
*
* @group laravel
*/
public function testSegmentMethodReturnsAURISegment()
{
$_SERVER['REQUEST_URI'] = 'http://localhost/index.php/user/profile';
$this->assertEquals('user', URI::segment(1));
$this->assertEquals('profile', URI::segment(2));
}
/**
* Data provider for the URI::current test.
*/
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'),
);
}
}

View File

@@ -0,0 +1,106 @@
<?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');
}
/**
* 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'));
}
/**
* 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')));
}
}

View File

@@ -0,0 +1,669 @@
<?php
class ValidatorTest extends PHPUnit_Framework_TestCase {
/**
* Setup the test environment.
*/
public function setUp()
{
Config::set('database.default', 'sqlite');
}
/**
* Tear down the test environment.
*/
public function tearDown()
{
Config::set('database.default', 'mysql');
$_FILES = array();
}
/**
* Test the required validation rule.
*
* @group laravel
*/
public function testRequiredRule()
{
$input = array('name' => 'Taylor Otwell');
$rules = array('name' => 'required');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['name'] = '';
$this->assertFalse(Validator::make($input, $rules)->valid());
unset($input['name']);
$this->assertFalse(Validator::make($input, $rules)->valid());
$_FILES['name']['tmp_name'] = 'foo';
$this->assertTrue(Validator::make($_FILES, $rules)->valid());
$_FILES['name']['tmp_name'] = '';
$this->assertFalse(Validator::make($_FILES, $rules)->valid());
}
/**
* Test the confirmed validation rule.
*
* @group laravel
*/
public function testTheConfirmedRule()
{
$input = array('password' => 'foo', 'password_confirmation' => 'foo');
$rules = array('password' => 'confirmed');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['password_confirmation'] = 'foo_bar';
$this->assertFalse(Validator::make($input, $rules)->valid());
unset($input['password_confirmation']);
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the different validation rule.
*
* @group laravel
*/
public function testTheDifferentRule()
{
$input = array('password' => 'foo', 'password_confirmation' => 'bar');
$rules = array('password' => 'different:password_confirmation');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['password_confirmation'] = 'foo';
$this->assertFalse(Validator::make($input, $rules)->valid());
unset($input['password_confirmation']);
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the accepted validation rule.
*
* @group laravel
*/
public function testTheAcceptedRule()
{
$input = array('terms' => '1');
$rules = array('terms' => 'accepted');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['terms'] = 'yes';
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['terms'] = '2';
$this->assertFalse(Validator::make($input, $rules)->valid());
// The accepted rule implies required, so should fail if field not present.
unset($input['terms']);
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the numeric validation rule.
*
* @group laravel
*/
public function testTheNumericRule()
{
$input = array('amount' => '1.21');
$rules = array('amount' => 'numeric');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['amount'] = '1';
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['amount'] = 1.2;
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['amount'] = '1.2a';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the integer validation rule.
*
* @group laravel
*/
public function testTheIntegerRule()
{
$input = array('amount' => '1');
$rules = array('amount' => 'integer');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['amount'] = '0';
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['amount'] = 1.2;
$this->assertFalse(Validator::make($input, $rules)->valid());
$input['amount'] = '1.2a';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the size validation rule.
*
* @group laravel
*/
public function testTheSizeRule()
{
$input = array('amount' => '1.21');
$rules = array('amount' => 'numeric|size:1.21');
$this->assertTrue(Validator::make($input, $rules)->valid());
$rules = array('amount' => 'numeric|size:1');
$this->assertFalse(Validator::make($input, $rules)->valid());
// If no numeric rule is on the field, it is treated as a string
$input = array('amount' => '111');
$rules = array('amount' => 'size:3');
$this->assertTrue(Validator::make($input, $rules)->valid());
$rules = array('amount' => 'size:4');
$this->assertFalse(Validator::make($input, $rules)->valid());
// The size rules checks kilobytes on files
$_FILES['photo']['tmp_name'] = 'foo';
$_FILES['photo']['size'] = 10240;
$rules = array('photo' => 'size:10');
$this->assertTrue(Validator::make($_FILES, $rules)->valid());
$_FILES['photo']['size'] = 14000;
$this->assertFalse(Validator::make($_FILES, $rules)->valid());
}
/**
* Test the between validation rule.
*
* @group laravel
*/
public function testTheBetweenRule()
{
$input = array('amount' => '1.21');
$rules = array('amount' => 'numeric|between:1,2');
$this->assertTrue(Validator::make($input, $rules)->valid());
$rules = array('amount' => 'numeric|between:2,3');
$this->assertFalse(Validator::make($input, $rules)->valid());
// If no numeric rule is on the field, it is treated as a string
$input = array('amount' => '111');
$rules = array('amount' => 'between:1,3');
$this->assertTrue(Validator::make($input, $rules)->valid());
$rules = array('amount' => 'between:100,111');
$this->assertFalse(Validator::make($input, $rules)->valid());
// The size rules checks kilobytes on files
$_FILES['photo']['tmp_name'] = 'foo';
$_FILES['photo']['size'] = 10240;
$rules = array('photo' => 'between:9,11');
$this->assertTrue(Validator::make($_FILES, $rules)->valid());
$_FILES['photo']['size'] = 14000;
$this->assertFalse(Validator::make($_FILES, $rules)->valid());
}
/**
* Test the between validation rule.
*
* @group laravel
*/
public function testTheMinRule()
{
$input = array('amount' => '1.21');
$rules = array('amount' => 'numeric|min:1');
$this->assertTrue(Validator::make($input, $rules)->valid());
$rules = array('amount' => 'numeric|min:2');
$this->assertFalse(Validator::make($input, $rules)->valid());
// If no numeric rule is on the field, it is treated as a string
$input = array('amount' => '01');
$rules = array('amount' => 'min:2');
$this->assertTrue(Validator::make($input, $rules)->valid());
$rules = array('amount' => 'min:3');
$this->assertFalse(Validator::make($input, $rules)->valid());
// The size rules checks kilobytes on files
$_FILES['photo']['tmp_name'] = 'foo';
$_FILES['photo']['size'] = 10240;
$rules = array('photo' => 'min:9');
$this->assertTrue(Validator::make($_FILES, $rules)->valid());
$_FILES['photo']['size'] = 8000;
$this->assertFalse(Validator::make($_FILES, $rules)->valid());
}
/**
* Test the between validation rule.
*
* @group laravel
*/
public function testTheMaxRule()
{
$input = array('amount' => '1.21');
$rules = array('amount' => 'numeric|max:2');
$this->assertTrue(Validator::make($input, $rules)->valid());
$rules = array('amount' => 'numeric|max:1');
$this->assertFalse(Validator::make($input, $rules)->valid());
// If no numeric rule is on the field, it is treated as a string
$input = array('amount' => '01');
$rules = array('amount' => 'max:3');
$this->assertTrue(Validator::make($input, $rules)->valid());
$rules = array('amount' => 'max:1');
$this->assertFalse(Validator::make($input, $rules)->valid());
// The size rules checks kilobytes on files
$_FILES['photo']['tmp_name'] = 'foo';
$_FILES['photo']['size'] = 10240;
$rules = array('photo' => 'max:11');
$this->assertTrue(Validator::make($_FILES, $rules)->valid());
$_FILES['photo']['size'] = 140000;
$this->assertFalse(Validator::make($_FILES, $rules)->valid());
}
/**
* Test the in validation rule.
*
* @group laravel
*/
public function testTheInRule()
{
$input = array('size' => 'L');
$rules = array('size' => 'in:S,M,L');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['size'] = 'XL';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the not-in validation rule.
*
* @group laravel
*/
public function testTheNotInRule()
{
$input = array('size' => 'L');
$rules = array('size' => 'not_in:S,M,L');
$this->assertFalse(Validator::make($input, $rules)->valid());
$input['size'] = 'XL';
$this->assertTrue(Validator::make($input, $rules)->valid());
}
/**
* Test the IP validation rule.
*
* @group laravel
*/
public function testTheIPRule()
{
$input = array('ip' => '192.168.1.1');
$rules = array('ip' => 'ip');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['ip'] = '192.111';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the e-mail validation rule.
*
* @group laravel
*/
public function testTheEmailRule()
{
$input = array('email' => 'example@gmail.com');
$rules = array('email' => 'email');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['email'] = 'blas-asok';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the URL validation rule.
*
* @group laravel
*/
public function testTheUrlRule()
{
$input = array('url' => 'http://www.google.com');
$rules = array('url' => 'url');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['url'] = 'blas-asok';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the active URL validation rule.
*
* @group laravel
*/
public function testTheActiveUrlRule()
{
$input = array('url' => 'http://google.com');
$rules = array('url' => 'active_url');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['url'] = 'http://asdlk-aselkaiwels.com';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the image validation rule.
*
* @group laravel
*/
public function testTheImageRule()
{
$_FILES['photo']['tmp_name'] = path('storage').'files/desert.jpg';
$rules = array('photo' => 'image');
$this->assertTrue(Validator::make($_FILES, $rules)->valid());
$_FILES['photo']['tmp_name'] = path('app').'routes.php';
$this->assertFalse(Validator::make($_FILES, $rules)->valid());
}
/**
* Test the alpha validation rule.
*
* @group laravel
*/
public function testTheAlphaRule()
{
$input = array('name' => 'TaylorOtwell');
$rules = array('name' => 'alpha');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['name'] = 'Taylor Otwell';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the alpha_num validation rule.
*
* @group laravel
*/
public function testTheAlphaNumRule()
{
$input = array('name' => 'TaylorOtwell1');
$rules = array('name' => 'alpha_num');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['name'] = 'Taylor Otwell';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the alpha_num validation rule.
*
* @group laravel
*/
public function testTheAlphaDashRule()
{
$input = array('name' => 'Taylor-Otwell_1');
$rules = array('name' => 'alpha_dash');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['name'] = 'Taylor Otwell';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test the mimes validation rule.
*
* @group laravel
*/
public function testTheMimesRule()
{
$_FILES['file']['tmp_name'] = path('app').'routes.php';
$rules = array('file' => 'mimes:php,txt');
$this->assertTrue(Validator::make($_FILES, $rules)->valid());
$rules = array('file' => 'mimes:jpg,bmp');
$this->assertFalse(Validator::make($_FILES, $rules)->valid());
$_FILES['file']['tmp_name'] = path('storage').'files/desert.jpg';
$rules['file'] = 'mimes:jpg,bmp';
$this->assertTrue(Validator::make($_FILES, $rules)->valid());
$rules['file'] = 'mimes:txt,bmp';
$this->assertFalse(Validator::make($_FILES, $rules)->valid());
}
/**
* Test the unique validation rule.
*
* @group laravel
*/
public function testUniqueRule()
{
$input = array('code' => 'ZZ');
$rules = array('code' => 'unique:validation_unique');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input = array('code' => 'AR');
$this->assertFalse(Validator::make($input, $rules)->valid());
$rules = array('code' => 'unique:validation_unique,code,AR,code');
$this->assertTrue(Validator::make($input, $rules)->valid());
}
/**
* Tests the exists validation rule.
*
* @group laravel
*/
public function testExistsRule()
{
$input = array('code' => 'TX');
$rules = array('code' => 'exists:validation_unique');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['code'] = array('TX', 'NY');
$rules = array('code' => 'exists:validation_unique,code');
$this->assertTrue(Validator::make($input, $rules)->valid());
$input['code'] = array('TX', 'XX');
$this->assertFalse(Validator::make($input, $rules)->valid());
$input['code'] = 'XX';
$this->assertFalse(Validator::make($input, $rules)->valid());
}
/**
* Test that the validator sets the correct messages.
*
* @group laravel
*/
public function testCorrectMessagesAreSet()
{
$lang = require path('app').'language/en/validation.php';
$input = array('email' => 'example-foo');
$rules = array('name' => 'required', 'email' => 'required|email');
$v = Validator::make($input, $rules);
$v->valid();
$messages = $v->errors;
$this->assertInstanceOf('Laravel\\Messages', $messages);
$this->assertEquals(str_replace(':attribute', 'name', $lang['required']), $messages->first('name'));
$this->assertEquals(str_replace(':attribute', 'email', $lang['email']), $messages->first('email'));
}
/**
* Test that custom messages are recognized.
*
* @group laravel
*/
public function testCustomMessagesAreRecognize()
{
$messages = array('required' => 'Required!');
$rules = array('name' => 'required');
$v = Validator::make(array(), $rules, $messages);
$v->valid();
$this->assertEquals('Required!', $v->errors->first('name'));
$messages['email_required'] = 'Email Required!';
$rules = array('name' => 'required', 'email' => 'required');
$v = Validator::make(array(), $rules, $messages);
$v->valid();
$this->assertEquals('Required!', $v->errors->first('name'));
$this->assertEquals('Email Required!', $v->errors->first('email'));
$rules = array('custom' => 'required');
$v = Validator::make(array(), $rules);
$v->valid();
$this->assertEquals('This field is required!', $v->errors->first('custom'));
}
/**
* Test that size replacements are made on messages.
*
* @group laravel
*/
public function testNumericSizeReplacementsAreMade()
{
$lang = require path('app').'language/en/validation.php';
$input = array('amount' => 100);
$rules = array('amount' => 'numeric|size:80');
$v = Validator::make($input, $rules);
$v->valid();
$this->assertEquals(str_replace(array(':attribute', ':size'), array('amount', '80'), $lang['size']['numeric']), $v->errors->first('amount'));
$rules = array('amount' => 'numeric|between:70,80');
$v = Validator::make($input, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':min', ':max'), array('amount', '70', '80'), $lang['between']['numeric']);
$this->assertEquals($expect, $v->errors->first('amount'));
$rules = array('amount' => 'numeric|min:120');
$v = Validator::make($input, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':min'), array('amount', '120'), $lang['min']['numeric']);
$this->assertEquals($expect, $v->errors->first('amount'));
$rules = array('amount' => 'numeric|max:20');
$v = Validator::make($input, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':max'), array('amount', '20'), $lang['max']['numeric']);
$this->assertEquals($expect, $v->errors->first('amount'));
}
/**
* Test that string size replacements are made on messages.
*
* @group laravel
*/
public function testStringSizeReplacementsAreMade()
{
$lang = require path('app').'language/en/validation.php';
$input = array('amount' => '100');
$rules = array('amount' => 'size:80');
$v = Validator::make($input, $rules);
$v->valid();
$this->assertEquals(str_replace(array(':attribute', ':size'), array('amount', '80'), $lang['size']['string']), $v->errors->first('amount'));
$rules = array('amount' => 'between:70,80');
$v = Validator::make($input, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':min', ':max'), array('amount', '70', '80'), $lang['between']['string']);
$this->assertEquals($expect, $v->errors->first('amount'));
$rules = array('amount' => 'min:120');
$v = Validator::make($input, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':min'), array('amount', '120'), $lang['min']['string']);
$this->assertEquals($expect, $v->errors->first('amount'));
$rules = array('amount' => 'max:2');
$v = Validator::make($input, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':max'), array('amount', '2'), $lang['max']['string']);
$this->assertEquals($expect, $v->errors->first('amount'));
}
/**
* Test that string size replacements are made on messages.
*
* @group laravel
*/
public function testFileSizeReplacementsAreMade()
{
$lang = require path('app').'language/en/validation.php';
$_FILES['amount']['tmp_name'] = 'foo';
$_FILES['amount']['size'] = 10000;
$rules = array('amount' => 'size:80');
$v = Validator::make($_FILES, $rules);
$v->valid();
$this->assertEquals(str_replace(array(':attribute', ':size'), array('amount', '80'), $lang['size']['file']), $v->errors->first('amount'));
$rules = array('amount' => 'between:70,80');
$v = Validator::make($_FILES, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':min', ':max'), array('amount', '70', '80'), $lang['between']['file']);
$this->assertEquals($expect, $v->errors->first('amount'));
$rules = array('amount' => 'min:120');
$v = Validator::make($_FILES, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':min'), array('amount', '120'), $lang['min']['file']);
$this->assertEquals($expect, $v->errors->first('amount'));
$rules = array('amount' => 'max:2');
$v = Validator::make($_FILES, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':max'), array('amount', '2'), $lang['max']['file']);
$this->assertEquals($expect, $v->errors->first('amount'));
}
/**
* Test that values get replaced in messages.
*
* @group laravel
*/
public function testValuesGetReplaced()
{
$lang = require path('app').'language/en/validation.php';
$_FILES['file']['tmp_name'] = path('storage').'files/desert.jpg';
$rules = array('file' => 'mimes:php,txt');
$v = Validator::make($_FILES, $rules);
$v->valid();
$expect = str_replace(array(':attribute', ':values'), array('file', 'php, txt'), $lang['mimes']);
$this->assertEquals($expect, $v->errors->first('file'));
}
/**
* Test custom attribute names are replaced.
*
* @group laravel
*/
public function testCustomAttributesAreReplaced()
{
$lang = require path('app').'language/en/validation.php';
$rules = array('test_attribute' => 'required');
$v = Validator::make(array(), $rules);
$v->valid();
$expect = str_replace(':attribute', 'attribute', $lang['required']);
$this->assertEquals($expect, $v->errors->first('test_attribute'));
}
}

View File

@@ -0,0 +1,255 @@
<?php
class ViewTest extends PHPUnit_Framework_TestCase {
/**
* Tear down the testing environment.
*/
public function tearDown()
{
View::$shared = array();
unset(Event::$events['composing: test.basic']);
}
/**
* Test the View::make method.
*
* @group laravel
*/
public function testMakeMethodReturnsAViewInstance()
{
$this->assertInstanceOf('Laravel\\View', View::make('home.index'));
}
/**
* Test the View class constructor.
*
* @group laravel
*/
public function testViewNameIsSetByConstrutor()
{
$view = new View('home.index');
$this->assertEquals('home.index', $view->view);
}
/**
* Test the View class constructor.
*
* @group laravel
*/
public function testViewIsCreatedWithCorrectPath()
{
$view = new View('home.index');
$this->assertEquals(
str_replace(DS, '/', path('app')).'views/home/index.php',
str_replace(DS, '/', $view->path)
);
}
/**
* Test the View class constructor for bundles.
*
* @group laravel
*/
public function testBundleViewIsCreatedWithCorrectPath()
{
$view = new View('laravel-tests::home.index');
$this->assertEquals(
str_replace(DS, '/', Bundle::path('laravel-tests')).'views/home/index.php',
str_replace(DS, '/', $view->path)
);
}
/**
* Test the View class constructor.
*
* @group laravel
*/
public function testDataIsSetOnViewByConstructor()
{
$view = new View('home.index', array('name' => 'Taylor'));
$this->assertEquals('Taylor', $view->data['name']);
}
/**
* Test the View::name method.
*
* @group laravel
*/
public function testNameMethodRegistersAViewName()
{
View::name('home.index', 'home');
$this->assertEquals('home.index', View::$names['home']);
}
/**
* Test the View::shared method.
*
* @group laravel
*/
public function testSharedMethodAddsDataToSharedArray()
{
View::share('comment', 'Taylor');
$this->assertEquals('Taylor', View::$shared['comment']);
}
/**
* Test the View::with method.
*
* @group laravel
*/
public function testViewDataCanBeSetUsingWithMethod()
{
$view = View::make('home.index')->with('comment', 'Taylor');
$this->assertEquals('Taylor', $view->data['comment']);
}
/**
* Test the View class constructor.
*
* @group laravel
*/
public function testEmptyMessageContainerSetOnViewWhenNoErrorsInSession()
{
$view = new View('home.index');
$this->assertInstanceOf('Laravel\\Messages', $view->data['errors']);
}
/**
* Test the View __set method.
*
* @group laravel
*/
public function testDataCanBeSetOnViewsThroughMagicMethods()
{
$view = new View('home.index');
$view->comment = 'Taylor';
$this->assertEquals('Taylor', $view->data['comment']);
}
/**
* Test the View __get method.
*
* @group laravel
*/
public function testDataCanBeRetrievedFromViewsThroughMagicMethods()
{
$view = new View('home.index');
$view->comment = 'Taylor';
$this->assertEquals('Taylor', $view->comment);
}
/**
* Test the View's ArrayAccess implementation.
*
* @group laravel
*/
public function testDataCanBeSetOnTheViewThroughArrayAccess()
{
$view = new View('home.index');
$view['comment'] = 'Taylor';
$this->assertEquals('Taylor', $view->data['comment']);
}
/**
* Test the View's ArrayAccess implementation.
*
* @group laravel
*/
public function testDataCanBeRetrievedThroughArrayAccess()
{
$view = new View('home.index');
$view['comment'] = 'Taylor';
$this->assertEquals('Taylor', $view['comment']);
}
/**
* Test the View::nest method.
*
* @group laravel
*/
public function testNestMethodSetsViewInstanceInData()
{
$view = View::make('home.index')->nest('partial', 'tests.basic');
$this->assertEquals('tests.basic', $view->data['partial']->view);
$this->assertInstanceOf('Laravel\\View', $view->data['partial']);
}
/**
* Test that the registered data is passed to the view correctly.
*
* @group laravel
*/
public function testDataIsPassedToViewCorrectly()
{
View::share('name', 'Taylor');
$view = View::make('tests.basic')->with('age', 25)->render();
$this->assertEquals('Taylor is 25', $view);
}
/**
* Test that the View class renders nested views.
*
* @group laravel
*/
public function testNestedViewsAreRendered()
{
$view = View::make('tests.basic')
->with('age', 25)
->nest('name', 'tests.nested');
$this->assertEquals('Taylor is 25', $view->render());
}
/**
* Test that the View class renders nested responses.
*
* @group laravel
*/
public function testNestedResponsesAreRendered()
{
$view = View::make('tests.basic')
->with('age', 25)
->with('name', Response::view('tests.nested'));
$this->assertEquals('Taylor is 25', $view->render());
}
/**
* Test the View class raises a composer event.
*
* @group laravel
*/
public function testComposerEventIsCalledWhenViewIsRendering()
{
View::composer('tests.basic', function($view)
{
$view->data = array('name' => 'Taylor', 'age' => 25);
});
$view = View::make('tests.basic')->render();
$this->assertEquals('Taylor is 25', $view);
}
}