From 07bc27fb35294f919e7b3c476f111b5b7d667f33 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 26 Jun 2017 10:31:57 +0800 Subject: [PATCH] Set user model, table migration and model factory --- app/User.php | 30 ++++++++++++- artisan | 0 database/factories/ModelFactory.php | 20 +++++---- .../2014_10_12_000000_create_users_table.php | 15 +++++-- phpunit.xml | 2 + tests/Feature/ExampleTest.php | 23 ---------- tests/Unit/ExampleTest.php | 20 --------- tests/Unit/PersonRelationsTest.php | 49 ++++++++++++++++++++++ 8 files changed, 105 insertions(+), 54 deletions(-) mode change 100644 => 100755 artisan delete mode 100644 tests/Feature/ExampleTest.php delete mode 100644 tests/Unit/ExampleTest.php create mode 100644 tests/Unit/PersonRelationsTest.php diff --git a/app/User.php b/app/User.php index bfd96a6..2d916f8 100644 --- a/app/User.php +++ b/app/User.php @@ -15,7 +15,11 @@ class User extends Authenticatable * @var array */ protected $fillable = [ - 'name', 'email', 'password', + 'nickname', 'gender_id', 'name', + 'email', 'password', + 'address', 'phone', + 'dof', 'dod', + 'father_id', 'mother_id', 'parent_id', ]; /** @@ -26,4 +30,28 @@ class User extends Authenticatable protected $hidden = [ 'password', 'remember_token', ]; + + public function setFather(User $father) + { + if ($father->gender_id === 1) { + $this->father_id = $father->id; + $this->save(); + + return $father; + } + + return false; + } + + public function setMother(User $mother) + { + if ($mother->gender_id === 2) { + $this->mother_id = $mother->id; + $this->save(); + + return $mother; + } + + return false; + } } diff --git a/artisan b/artisan old mode 100644 new mode 100755 diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 7926c79..8c3290a 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -1,5 +1,7 @@ define(App\User::class, function (Faker\Generator $faker) { - static $password; - +$factory->define(User::class, function (Faker\Generator $faker) { return [ - 'name' => $faker->name, - 'email' => $faker->unique()->safeEmail, - 'password' => $password ?: $password = bcrypt('secret'), - 'remember_token' => str_random(10), + 'nickname' => $faker->name, + 'gender_id' => rand(1, 2), ]; }); + +$factory->state(User::class, 'male', function (Faker\Generator $faker) { + return ['gender_id' => 1]; +}); + +$factory->state(User::class, 'female', function (Faker\Generator $faker) { + return ['gender_id' => 2]; +}); \ No newline at end of file diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 689cbee..1173cd0 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -15,9 +15,18 @@ class CreateUsersTable extends Migration { Schema::create('users', function (Blueprint $table) { $table->increments('id'); - $table->string('name'); - $table->string('email')->unique(); - $table->string('password'); + $table->string('nickname'); + $table->string('name')->nullable(); + $table->boolean('gender_id')->unsigned(); + $table->unsignedInteger('father_id')->nullable(); + $table->unsignedInteger('mother_id')->nullable(); + $table->unsignedInteger('parent_id')->nullable(); + $table->date('dof')->nullable(); + $table->date('dod')->nullable(); + $table->string('email')->unique()->nullable(); + $table->string('password')->nullable(); + $table->string('address')->nullable(); + $table->string('phone')->nullable(); $table->rememberToken(); $table->timestamps(); }); diff --git a/phpunit.xml b/phpunit.xml index 9ecda83..f5f6354 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -27,5 +27,7 @@ + + diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 486dc27..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,23 +0,0 @@ -get('/'); - - $response->assertStatus(200); - } -} diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index 5663bb4..0000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,20 +0,0 @@ -assertTrue(true); - } -} diff --git a/tests/Unit/PersonRelationsTest.php b/tests/Unit/PersonRelationsTest.php new file mode 100644 index 0000000..ca35120 --- /dev/null +++ b/tests/Unit/PersonRelationsTest.php @@ -0,0 +1,49 @@ +create(); + + $this->assertDatabaseHas('users', [ + 'nickname' => $person->nickname, + 'gender_id' => $person->gender_id, + ]); + } + + /** @test */ + public function person_can_have_a_father() + { + $person = factory(User::class)->create(); + $father = factory(User::class)->states('male')->create(); + $person->setFather($father); + + $this->assertDatabaseHas('users', [ + 'id' => $person->id, + 'father_id' => $father->id, + ]); + } + + /** @test */ + public function person_can_have_a_mother() + { + $person = factory(User::class)->create(); + $mother = factory(User::class)->states('female')->create(); + $person->setMother($mother); + + $this->assertDatabaseHas('users', [ + 'id' => $person->id, + 'mother_id' => $mother->id, + ]); + } +}