From 3d1bc5891f667a057dcae8fdfdf4aa469832ebb9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 3 Jul 2014 09:43:12 +0200 Subject: [PATCH] First tests for User model. --- app/config/testing/database.php | 11 +++++ app/models/User.php | 6 +-- app/tests/controllers/HomeControllerTest.php | 23 ++++++++++ app/tests/models/UserTest.php | 45 ++++++++++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 app/config/testing/database.php create mode 100644 app/tests/controllers/HomeControllerTest.php create mode 100644 app/tests/models/UserTest.php diff --git a/app/config/testing/database.php b/app/config/testing/database.php new file mode 100644 index 0000000000..8448619ce0 --- /dev/null +++ b/app/config/testing/database.php @@ -0,0 +1,11 @@ + 'sqlite', + 'connections' => [ + 'sqlite' => [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '' + ], + ] +]; \ No newline at end of file diff --git a/app/models/User.php b/app/models/User.php index 1dd21b1fa9..f6f3ba91d2 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -22,7 +22,7 @@ class User extends Elegant implements UserInterface, RemindableInterface = [ 'email' => 'required|email|unique:users,email', 'migrated' => 'required|numeric|between:0,1', - 'password' => 'between:60,60', + 'password' => 'required|between:60,60', 'verification' => 'between:32,32', ]; @@ -31,10 +31,10 @@ class User extends Elegant implements UserInterface, RemindableInterface * * @var array */ - protected $hidden = array('password', 'remember_token'); + protected $hidden = array('remember_token'); public function accounts() { return $this->hasMany('Account'); } -} +} \ No newline at end of file diff --git a/app/tests/controllers/HomeControllerTest.php b/app/tests/controllers/HomeControllerTest.php new file mode 100644 index 0000000000..7caab01cd0 --- /dev/null +++ b/app/tests/controllers/HomeControllerTest.php @@ -0,0 +1,23 @@ +with('index'); + Auth::shouldReceive('check')->andReturn(true); + + // call + $this->call('GET', '/'); + + // test + $this->assertResponseOk(); + } + +} \ No newline at end of file diff --git a/app/tests/models/UserTest.php b/app/tests/models/UserTest.php new file mode 100644 index 0000000000..493a1428aa --- /dev/null +++ b/app/tests/models/UserTest.php @@ -0,0 +1,45 @@ +prepareForTests(); + } + + /** + * Migrate the database + */ + private function prepareForTests() + { + Artisan::call('migrate'); + } + + /** + * Username is required + */ + public function testUsernameIsRequired() + { + // Create a new User + $user = new User; + $user->migrated = 0; + $user->password = Str::random(60); + + // User should not save + $this->assertFalse($user->isValid()); + + // Save the errors + $errors = $user->validator->messages()->all(); +// // There should be 1 error + $this->assertCount(1, $errors); + +// // The username error should be set + $this->assertEquals($errors[0], "The email field is required."); + } + +} \ No newline at end of file