Archived
1
0
Fork 0

Adding Spell Model

This commit is contained in:
Henrik Hautakoski 2021-07-07 16:18:01 +02:00
parent b7ccc4a657
commit 78d5d8d50e
3 changed files with 102 additions and 0 deletions

39
app/Models/Spell.php Normal file
View file

@ -0,0 +1,39 @@
<?php
namespace App\Models;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Spell extends Model
{
use HasFactory;
public $incrementing = false;
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'slug'
];
protected static function boot() {
parent::boot();
static::creating(function ($spell) {
$spell->slug = Str::slug($spell->name);
});
}
public function recipe()
{
return $this->hasOne(Recipe::class);
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace Database\Factories;
use App\Models\Spell;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
class SpellFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Spell::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'id' => $this->faker->unique()->randomNumber(5),
'name' => $this->faker->unique()->sentence(8),
'slug' => function (array $attributes) {
return Str::slug($attributes['name']);
}
];
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace Tests\Feature\Models;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\Recipe;
use App\Models\Spell;
class SpellTest extends TestCase
{
use RefreshDatabase;
/**
* Test recipe relationship.
*
* @return void
*/
public function test_recipe_relationship()
{
$spell = Spell::factory()->create();
$recipe = Recipe::factory()
->for($spell)
->create();
$this->assertEquals($recipe->name, $spell->recipe->name);
}
}