So I am working on some unit testing before I implement a new feature. I run my test and it fails with OverflowException : Maximum retries of 10000 reached without finding a unique value
This is the test I'm running.
public function test_job_factory()
{
$client = Client::factory()->create();
$accountHandler = AccountHandler::factory()->create();
$user = User::factory()->create();
$this->post('/login', [
'email' => $user->email,
'password' => 'password',
]);
$user->givePermissionTo( 'manage jobs' );
$clientContact = ClientContact::factory()->create();
$job = Job::factory()->create();
$this->assertTrue($job->id > 0);
}
The error seems to happen while creating the job itself. Tests above this test the other factories and are working.
Heres the JobFactory.php file:
<?php
namespace Database\Factories;
use App\Models\AccountHandler;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Job;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Job>
*/
class JobFactory extends Factory
{
protected $model = Job::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => $this->faker->company(),
'is_active' => 1,
'is_draft' => 0,
'client_id' => $this->faker->unique()->numberBetween(1, Client::count()),
'account_handler_id' => $this->faker->unique()->numberBetween(1, AccountHandler::count()),
'eclipse_contact_id' => $this->faker->unique()->numberBetween(1, User::count()),
'client_contact_id' => $this->faker->unique()->numberBetween(1, ClientContact::count()),
'description' => $this->faker->paragraphs(1),
];
}
}
and the migration for this (create_jobs_table.php):
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->id('number');
$table->boolean( 'is_active' )->default(true);
$table->boolean( 'is_complete' )->default(true);
$table->string('title', 64)->nullable();
$table->timestamps();
$table->foreignId('author')->nullable()->constrained()->references('id')->on('users');
$table->text('description')->nullable();
$table->foreignId('client_id')->nullable()->constrained()->references('id')->on('clients');
$table->foreignId('client_contact_id')->nullable()->constrained()->references('id')->on('client_contacts');
$table->foreignId('account_handler_id')->nullable()->constrained()->references('id')->on('account_handlers');
$table->date('expiry_date')->nullable();
$table->date('artwork_deadline')->nullable();
$table->date('proof_deadline')->nullable();
$table->integer('redirect')->nullable();
$table->boolean( 'is_draft' )->default(true);
$table->foreignId('eclipse_contact_id')->nullable()->constrained()->references('id')->on('users');
$table->foreignId('subscription_id')->nullable()->constrained()->references('id')->on('subscriptions');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('jobs');
}
};
So what is wrong and why this loop here? I have added the minimum data that is needed to create a job so no idea what I've missed.
thanks
CodePudding user response:
I will recommend to follow documentation approach for this problem. https://laravel.com/docs/9.x/database-testing#factory-relationships
Looking at your migration table, there is a big chance that your factory will not work, since you indicating the relation on SQL level, which basically will force you to have records in related table, otherwise SQL will throw you with an error..