Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
Teams have an owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Indemnity83 committed Apr 3, 2018
1 parent 7043ac4 commit 6c7926a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/Http/Controllers/Settings/TeamsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function store()
$team = Team::create([
'name' => request('name'),
'slug' => request('slug'),
'owner_id' => request()->user()->id,
]);

return response()->json(['data' => $team], 201);
Expand Down
10 changes: 10 additions & 0 deletions app/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public function users()
return $this->belongsToMany(User::class);
}

/**
* Get the team owner.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function owner()
{
return $this->hasOne(User::class, 'id', 'owner_id');
}

/**
* The modpacks that are owned by the team.
*
Expand Down
3 changes: 3 additions & 0 deletions database/factories/TeamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
return [
'name' => $faker->streetName,
'slug' => $faker->slug,
'owner_id' => function () {
return factory(\App\User::class)->create()->id;
},
];
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function up()
{
Schema::create('teams', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('owner_id');
$table->string('name');
$table->string('slug');
$table->timestamps();
Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/Settings/CreateTeamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class CreateTeamsTest extends TestCase
/** @test **/
public function create_a_team()
{
$this->actingAs(new User);
$user = factory(User::class)->create();
$this->actingAs($user);

$response = $this->postJson('/settings/teams', [
'name' => 'My Team',
Expand All @@ -35,6 +36,7 @@ public function create_a_team()
$this->assertDatabaseHas('teams', [
'name' => 'My Team',
'slug' => 'my-team',
'owner_id' => $user->id,
]);
$response->assertJsonStructure([
'data' => ['id', 'name', 'slug', 'created_at'],
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/ShowModpackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Tests\Feature;

use App\Team;
use App\User;
use App\Modpack;
use BuildFactory;
Expand Down Expand Up @@ -89,7 +90,9 @@ public function modpack_includes_list_of_users()
$userA = factory(User::class)->create();
$userB = factory(User::class)->create();
$userC = factory(User::class)->create();
$team = factory(Team::class)->create(['owner_id' => $userA->id]);
$modpack = factory(Modpack::class)->create([
'team_id' => $team->id,
'slug' => 'example-modpack',
]);

Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/TeamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public function a_team_has_users()
$users->assertNotContains($userB);
}

/** @test **/
public function a_team_has_an_owner()
{
$userA = factory(User::class)->create();
$team = factory(Team::class)->create(['owner_id' => $userA->id]);
$team->users()->attach($userA);

$this->assertTrue($team->owner->is($userA));
}

/** @test **/
public function a_team_has_many_modpacks()
{
Expand Down

0 comments on commit 6c7926a

Please sign in to comment.