Skip to content

Instantly share code, notes, and snippets.

@colindecarlo
Last active September 4, 2022 15:25
Show Gist options
  • Save colindecarlo/5aa99563cb2b2dde8cea8564232577c9 to your computer and use it in GitHub Desktop.
Save colindecarlo/5aa99563cb2b2dde8cea8564232577c9 to your computer and use it in GitHub Desktop.
Titles and Champions
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTitlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('titles', function (Blueprint $table) {
$table->increments('id');
$table->string('division');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('titles');
}
}
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateChampionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('champions', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('title_id');
$table->string('name');
$table->timestamp('reign_start');
$table->timestamp('reign_end')->nullable();
$table->timestamps();
$table->foreign('title_id')->references('id')->on('titles');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('champions');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Champion extends Model
{
public $fillable = ['name', 'reign_start', 'reign_end'];
public function scopeIsReigningChampion($query)
{
return $query->whereNull('reign_end');
}
}
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
$factory->define(App\Title::class, function (Faker\Generator $faker) {
return [
'division' => 'Heavyweight',
];
});
$factory->define(\App\Champion::class, function (Faker\Generator $faker) {
return [
'name' => $faker->unique()->name()
];
});
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Title extends Model
{
public function champions()
{
return $this->hasMany(Champion::class)->orderBy('reign_start', 'DESC');
}
public function currentChampion()
{
return $this->hasOne(Champion::class)->isReigningChampion();
}
public function addChampion($champion = null)
{
$champion = $champion instanceof Model ? $champion : new Champion($champion);
return $this->champions()->save($champion);
}
}
<?php
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class TitleTest extends TestCase
{
use DatabaseMigrations, DatabaseTransactions;
public function test_that_a_champion_can_be_added_for_a_title()
{
$title = factory(\App\Title::class)->create();
$champion = $title->addChampion(factory(\App\Champion::class)->make([
'reign_start' => Carbon::now()
]));
$this->assertEquals($title->id, $champion->title_id);
}
public function test_that_all_champions_for_a_title_can_be_retrieved()
{
$title = factory(App\Title::class)->create();
$title->addChampion(factory(\App\Champion::class)->make([
'name' => 'Kimo Leopoldo',
'reign_start' => Carbon::now()->subMonth(2),
'reign_end'=> Carbon::now()->subMonth(1)
]));
$title->addChampion(factory(\App\Champion::class)->make([
'name' => 'Royce Gracie',
'reign_start' => Carbon::now()->subMonth(1),
]));
$champions = $title->champions;
$this->assertCount(2, $champions);
}
public function test_that_the_current_champion_of_a_title_can_be_retrieved()
{
$title = factory(App\Title::class)->create();
$title->addChampion(factory(\App\Champion::class)->make([
'name' => 'Kimo Leopoldo',
'reign_start' => Carbon::now()->subMonth(2),
'reign_end'=> Carbon::now()->subMonth(1)
]));
$title->addChampion(factory(\App\Champion::class)->make([
'name' => 'Royce Gracie',
'reign_start' => Carbon::now()->subMonth(1),
]));
$currentChampion = $title->currentChampion;
$this->assertNotNull($currentChampion);
$this->assertEquals('Royce Gracie', $currentChampion->name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment