Skip to content

Commit

Permalink
add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Indemnity83 committed Mar 11, 2018
1 parent d15cfe3 commit 591d219
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions tests/Integration/Database/EloquentFactoryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ protected function getEnvironmentSetUp($app)
];
});

$factory->define(FactoryBuildableProfile::class, function (Generator $faker) {
return [
'user_id' => function () {
return factory(FactoryBuildableUser::class)->create()->id;
}
];
});

$factory->after(FactoryBuildableUser::class, 'make', function (FactoryBuildableUser $user, Generator $faker) {
$profile =factory(FactoryBuildableProfile::class)->make(['user_id' => $user->id]);
$user->setRelation('profile', $profile);
});

$factory->define(FactoryBuildableTeam::class, function (Generator $faker) {
return [
'name' => $faker->name,
'owner_id' => function () {
return factory(FactoryBuildableUser::class)->create()->id;
},
];
});

$factory->after(FactoryBuildableTeam::class, function (FactoryBuildableTeam $team, Generator $faker) {
$team->users()->attach($team->owner);
});

$factory->define(FactoryBuildableServer::class, function (Generator $faker) {
return [
'name' => $faker->name,
Expand Down Expand Up @@ -72,6 +98,23 @@ public function setUp()
$table->string('email');
});

Schema::create('profiles', function ($table) {
$table->increments('id');
$table->unsignedInteger('user_id');
});

Schema::create('teams', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('owner_id');
});

Schema::create('team_users', function ($table) {
$table->increments('id');
$table->unsignedInteger('team_id');
$table->unsignedInteger('user_id');
});

Schema::connection('alternative-connection')->create('users', function ($table) {
$table->increments('id');
$table->string('name');
Expand Down Expand Up @@ -183,6 +226,14 @@ public function creating_models_on_custom_connection()
$this->assertTrue($user->is($dbUser));
}

/** @test **/
public function creating_models_with_after_callback()
{
$team = factory(FactoryBuildableTeam::class)->create();

$this->assertTrue($team->users->contains($team->owner));
}

/** @test */
public function making_models_with_a_custom_connection()
{
Expand All @@ -192,6 +243,14 @@ public function making_models_with_a_custom_connection()

$this->assertEquals('alternative-connection', $user->getConnectionName());
}

/** @test **/
public function making_models_with_after_callback()
{
$user = factory(FactoryBuildableUser::class)->make();

$this->assertNotNull($user->profile);
}
}

class FactoryBuildableUser extends Model
Expand All @@ -204,6 +263,45 @@ public function servers()
{
return $this->hasMany(FactoryBuildableServer::class, 'user_id');
}

public function profile()
{
return $this->hasOne(FactoryBuildableProfile::class, 'user_id');
}
}

class FactoryBuildableProfile extends Model
{
public $table = 'profiles';
public $timestamps = false;
protected $guarded = ['id'];

public function user()
{
return $this->belongsTo(FactoryBuildableUser::class, 'user_id');
}
}

class FactoryBuildableTeam extends Model
{
public $table = 'teams';
public $timestamps = false;
protected $guarded = ['id'];

public function owner()
{
return $this->belongsTo(FactoryBuildableUser::class, 'owner_id');
}

public function users()
{
return $this->belongsToMany(
FactoryBuildableUser::class,
'team_users',
'team_id',
'user_id'
);
}
}

class FactoryBuildableServer extends Model
Expand Down

0 comments on commit 591d219

Please sign in to comment.