Browse Source

Set user model, table migration and model factory

pull/3/head
Nafies Luthfi 9 years ago
parent
commit
07bc27fb35
  1. 30
      app/User.php
  2. 0
      artisan
  3. 20
      database/factories/ModelFactory.php
  4. 15
      database/migrations/2014_10_12_000000_create_users_table.php
  5. 2
      phpunit.xml
  6. 23
      tests/Feature/ExampleTest.php
  7. 20
      tests/Unit/ExampleTest.php
  8. 49
      tests/Unit/PersonRelationsTest.php

30
app/User.php

@ -15,7 +15,11 @@ class User extends Authenticatable
* @var array * @var array
*/ */
protected $fillable = [ 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 = [ protected $hidden = [
'password', 'remember_token', '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;
}
} }

0
artisan

20
database/factories/ModelFactory.php

@ -1,5 +1,7 @@
<?php <?php
use App\User;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Model Factories | Model Factories
@ -12,13 +14,17 @@
*/ */
/** @var \Illuminate\Database\Eloquent\Factory $factory */ /** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
$factory->define(User::class, function (Faker\Generator $faker) {
return [ 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];
});

15
database/migrations/2014_10_12_000000_create_users_table.php

@ -15,9 +15,18 @@ class CreateUsersTable extends Migration
{ {
Schema::create('users', function (Blueprint $table) { Schema::create('users', function (Blueprint $table) {
$table->increments('id'); $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->rememberToken();
$table->timestamps(); $table->timestamps();
}); });

2
phpunit.xml

@ -27,5 +27,7 @@
<env name="CACHE_DRIVER" value="array"/> <env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/> <env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/> <env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php> </php>
</phpunit> </phpunit>

23
tests/Feature/ExampleTest.php

@ -1,23 +0,0 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}

20
tests/Unit/ExampleTest.php

@ -1,20 +0,0 @@
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}

49
tests/Unit/PersonRelationsTest.php

@ -0,0 +1,49 @@
<?php
namespace Tests\Unit;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class PersonRelationsTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function create_user_model_with_factory()
{
$person = factory(User::class)->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,
]);
}
}
Loading…
Cancel
Save