Skip to content

Commit

Permalink
Add additional type checks for calling factories with a closure
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon committed Feb 10, 2022
1 parent 368fabc commit e22ec12
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions types/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public function definition()
}
}

$factory = UserFactory::new();
assertType('UserFactory', $factory);
assertType('UserFactory', $factory = UserFactory::new());
assertType('UserFactory', UserFactory::new(['string' => 'string']));
assertType('UserFactory', UserFactory::new(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

assertType('array<string, mixed>', $factory->definition());

Expand All @@ -39,16 +43,28 @@ public function definition()

assertType('array<int|string, mixed>', $factory->raw());
assertType('array<int|string, mixed>', $factory->raw(['string' => 'string']));
assertType('array<int|string, mixed>', $factory->raw(function ($attributes) {
// assert('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('User', $factory->createOne());
// assertType('User', $factory->createOne(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne());
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('User', $factory->createOneQuietly());
// assertType('User', $factory->createOneQuietly(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly());
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('Illuminate\Database\Eloquent\Collection<int, User>', $factory->createMany([['string' => 'string']]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany(
Expand All @@ -68,6 +84,10 @@ public function definition()
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->create([
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->create(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->createQuietly());
// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->createQuietly([
Expand Down Expand Up @@ -95,6 +115,10 @@ public function definition()
assertType('Illuminate\Database\Eloquent\Model', $factory->makeOne([
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Model', $factory->makeOne(function ($attributes) {
// assert('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->make());
// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->make([
Expand All @@ -104,6 +128,10 @@ public function definition()
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->make([
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->make(function ($attributes) {
// assert('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

assertType('UserFactory', $factory->state(['string' => 'string']));
assertType('UserFactory', $factory->state(function () {
Expand Down

0 comments on commit e22ec12

Please sign in to comment.