From ebffcb55b67316b48e24718852fa708757918996 Mon Sep 17 00:00:00 2001 From: William Allen Date: Sun, 16 Aug 2026 17:32:24 -0400 Subject: [PATCH] Create `TargetFactory` This PR continues our ongoing effort to use Eloquent factories to create testing data. --- app/Models/Target.php | 5 ++ database/factories/TargetFactory.php | 27 +++++++++++ .../Pages/BuildSidebarComponentTest.php | 7 +-- tests/Browser/Pages/BuildTargetsPageTest.php | 8 +--- tests/Feature/GraphQL/BuildTypeTest.php | 6 +-- tests/Feature/GraphQL/FilterTest.php | 14 +++--- tests/Feature/GraphQL/LabelTypeTest.php | 7 +-- tests/Feature/GraphQL/TargetTypeTest.php | 47 +++++++------------ 8 files changed, 65 insertions(+), 56 deletions(-) create mode 100644 database/factories/TargetFactory.php diff --git a/app/Models/Target.php b/app/Models/Target.php index 364cfc5181..7a3195dbac 100644 --- a/app/Models/Target.php +++ b/app/Models/Target.php @@ -3,7 +3,9 @@ namespace App\Models; use App\Enums\TargetType; +use Database\Factories\TargetFactory; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -19,6 +21,9 @@ */ class Target extends Model { + /** @use HasFactory */ + use HasFactory; + protected $table = 'targets'; public $timestamps = false; diff --git a/database/factories/TargetFactory.php b/database/factories/TargetFactory.php new file mode 100644 index 0000000000..a9dd25b463 --- /dev/null +++ b/database/factories/TargetFactory.php @@ -0,0 +1,27 @@ + + */ +class TargetFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => Str::uuid()->toString(), + 'type' => TargetType::UNKNOWN, + ]; + } +} diff --git a/tests/Browser/Pages/BuildSidebarComponentTest.php b/tests/Browser/Pages/BuildSidebarComponentTest.php index a06aeb9dd7..af97b30a09 100644 --- a/tests/Browser/Pages/BuildSidebarComponentTest.php +++ b/tests/Browser/Pages/BuildSidebarComponentTest.php @@ -3,7 +3,6 @@ namespace Tests\Browser\Pages; use App\Enums\BuildCommandType; -use App\Enums\TargetType; use App\Models\Build; use App\Models\BuildUpdate; use App\Models\CoverageFile; @@ -12,6 +11,7 @@ use App\Models\Project; use App\Models\Site; use App\Models\SiteInformation; +use App\Models\Target; use App\Models\UploadFile; use App\Models\User; use App\Services\SiteService; @@ -393,10 +393,7 @@ public function testTargetsItem(): void $this->browse(function (Browser $browser) use ($build): void { $this->assertDisabled($browser, "/builds/{$build->id}", '@sidebar-targets'); - $build->targets()->create([ - 'name' => Str::uuid()->toString(), - 'type' => TargetType::UNKNOWN, - ]); + Target::factory()->for($build)->create(); $this->assertNotDisabled($browser, "/builds/{$build->id}", '@sidebar-targets', "/builds/{$build->id}/targets"); }); diff --git a/tests/Browser/Pages/BuildTargetsPageTest.php b/tests/Browser/Pages/BuildTargetsPageTest.php index 0cc0f12cd6..db7a303dfc 100644 --- a/tests/Browser/Pages/BuildTargetsPageTest.php +++ b/tests/Browser/Pages/BuildTargetsPageTest.php @@ -52,8 +52,7 @@ public function tearDown(): void private function addTarget(TargetType $type = TargetType::UNKNOWN): Target { - return $this->build->targets()->create([ - 'name' => Str::uuid()->toString(), + return Target::factory()->for($this->build)->create([ 'type' => $type, ]); } @@ -125,10 +124,7 @@ public function testFilters(): void public function testTargetTablePagination(): void { - $targets = collect(); - for ($i = 0; $i < 120; $i++) { - $targets[] = $this->addTarget(); - } + $targets = Target::factory()->count(120)->for($this->build)->create(); $targets = $targets->sortByDesc('name'); diff --git a/tests/Feature/GraphQL/BuildTypeTest.php b/tests/Feature/GraphQL/BuildTypeTest.php index 5f5aa3acaf..dc64e01d49 100644 --- a/tests/Feature/GraphQL/BuildTypeTest.php +++ b/tests/Feature/GraphQL/BuildTypeTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\GraphQL; use App\Enums\BuildCommandType; +use App\Enums\TargetType; use App\Models\Build; use App\Models\BuildCommand; use App\Models\CoverageFile; @@ -319,9 +320,8 @@ public function testTargetRelationship(): void ]); /** @var Target $target */ - $target = $build->targets()->create([ - 'name' => Str::uuid()->toString(), - 'type' => 'EXECUTABLE', + $target = Target::factory()->for($build)->create([ + 'type' => TargetType::EXECUTABLE, ]); $this->graphQL(' diff --git a/tests/Feature/GraphQL/FilterTest.php b/tests/Feature/GraphQL/FilterTest.php index 3fb8f84bda..9e0e53ab76 100644 --- a/tests/Feature/GraphQL/FilterTest.php +++ b/tests/Feature/GraphQL/FilterTest.php @@ -2,10 +2,10 @@ namespace Tests\Feature\GraphQL; -use App\Enums\TargetType; use App\Models\Build; use App\Models\Project; use App\Models\Site; +use App\Models\Target; use App\Models\Test; use App\Models\TestOutput; use App\Models\User; @@ -982,22 +982,22 @@ public function testFilterByRelationshipsOfRelationships(): void { $build1uuid = Str::uuid()->toString(); $target1name = Str::uuid()->toString(); - $this->projects['public1']->builds()->create([ + $build1 = $this->projects['public1']->builds()->create([ 'name' => 'build1', 'uuid' => $build1uuid, - ])->targets()->create([ + ]); + Target::factory()->for($build1)->create([ 'name' => $target1name, - 'type' => TargetType::UNKNOWN, ]); $build2uuid = Str::uuid()->toString(); $target2name = Str::uuid()->toString(); - $this->projects['public2']->builds()->create([ + $build2 = $this->projects['public2']->builds()->create([ 'name' => 'build1', 'uuid' => $build2uuid, - ])->targets()->create([ + ]); + Target::factory()->for($build2)->create([ 'name' => $target2name, - 'type' => TargetType::UNKNOWN, ]); $this->actingAs($this->users['admin'])->graphQL(' diff --git a/tests/Feature/GraphQL/LabelTypeTest.php b/tests/Feature/GraphQL/LabelTypeTest.php index 2df8dec6ed..ebd8de50ab 100644 --- a/tests/Feature/GraphQL/LabelTypeTest.php +++ b/tests/Feature/GraphQL/LabelTypeTest.php @@ -2,9 +2,9 @@ namespace Tests\Feature\GraphQL; -use App\Enums\TargetType; use App\Models\Label; use App\Models\Project; +use App\Models\Target; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Str; use Tests\TestCase; @@ -142,10 +142,7 @@ public function testTargetRelationship(): void 'uuid' => Str::uuid()->toString(), ]); - $target = $build->targets()->create([ - 'name' => Str::uuid()->toString(), - 'type' => TargetType::UNKNOWN, - ]); + $target = Target::factory()->for($build)->create(); $this->labels['label1'] = $target->labels()->save(Label::factory()->make()); diff --git a/tests/Feature/GraphQL/TargetTypeTest.php b/tests/Feature/GraphQL/TargetTypeTest.php index 79d9b95414..278cf7fd5a 100644 --- a/tests/Feature/GraphQL/TargetTypeTest.php +++ b/tests/Feature/GraphQL/TargetTypeTest.php @@ -46,21 +46,18 @@ public function testTypeEnumValues(): void ]); // Create one target of each type - $targets = []; - foreach (TargetType::cases() as $target_type) { - $targets[] = [ - 'name' => Str::uuid()->toString(), - 'type' => $target_type, - ]; - } - $build->targets()->createMany($targets); + $targets = Target::factory() + ->count(count(TargetType::cases())) + ->sequence(...array_map(fn (TargetType $type) => ['type' => $type], TargetType::cases())) + ->for($build) + ->create(); $target_node_list = []; foreach ($targets as $target) { $target_node_list[] = [ 'node' => [ - 'name' => $target['name'], - 'type' => $target['type']->name, + 'name' => $target->name, + 'type' => $target->type->name, ], ]; } @@ -99,16 +96,10 @@ public function testFilterByName(): void 'uuid' => Str::uuid()->toString(), ]); - $build->targets()->createMany([ - [ - 'name' => 'name1', - 'type' => TargetType::UNKNOWN, - ], - [ - 'name' => 'name2', - 'type' => TargetType::UNKNOWN, - ], - ]); + Target::factory()->count(2)->sequence( + ['name' => 'name1'], + ['name' => 'name2'], + )->for($build)->create(); $this->graphQL(' query build($id: ID) { @@ -170,14 +161,11 @@ public function testFilterByType(string $type): void ]); // Create one target of each type - $targets = []; - foreach (TargetType::cases() as $target_type) { - $targets[] = [ - 'name' => Str::uuid()->toString(), - 'type' => $target_type, - ]; - } - $build->targets()->createMany($targets); + Target::factory() + ->count(count(TargetType::cases())) + ->sequence(...array_map(fn (TargetType $type) => ['type' => $type], TargetType::cases())) + ->for($build) + ->create(); $this->graphQL(' query build($id: ID, $type: TargetType) { @@ -226,9 +214,8 @@ public function testBuildCommandRelationship(): void ]); /** @var Target $target */ - $target = $build->targets()->create([ + $target = Target::factory()->for($build)->create([ 'name' => 'name1', - 'type' => TargetType::UNKNOWN, ]); /** @var BuildCommand $command */