I just created a factory in order to run some tests:
CategoryFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
*/
class CategoryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
'company_id' => 1,
'name' => fake()->name(),
'parent_id' => null
];
}
}
And added it to the model:
Category.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Scopes\CompanyScope;
class Category extends Model
{
//
use SoftDeletes, HasFactory;
protected $fillable = [
'company_id',
'name',
'parent_id'
];
public function __construct() {
parent::__construct();
if(isset(auth()->user()->company_id)) {
$this->attributes['company_id'] = auth()->user()->company_id;
}
}
protected static function boot()
{
parent::boot();
static::addGlobalScope(new CompanyScope);
}
My test file looks like this:
public function test_category_edit_can_be_redered(): void
{
$user = User::factory()->create();
$category = Category::factory()->create();
$response = $this->actingAs($user)->get('/devices/categories/1/edit');
$response->assertStatus(200);
}
But when I try to run the test, I get the following error:
General error: 1364 Field 'company_id' doesn't have a default value (Connection: mysql, SQL: insert into `categories` (`updated_at`, `created_at`) values (2023-08-08 01:55:43, 2023-08-08 01:55:43))
It looks like the definition in the factory is not being applied to the create method... Any idea what I am doing wrong?