diff --git a/src/Illuminate/Database/Eloquent/Factories/CrossJoinSequence.php b/src/Illuminate/Database/Eloquent/Factories/CrossJoinSequence.php new file mode 100644 index 000000000000..b0efbd0c805b --- /dev/null +++ b/src/Illuminate/Database/Eloquent/Factories/CrossJoinSequence.php @@ -0,0 +1,26 @@ +state(new Sequence(...$sequence)); } + /** + * Add a new cross joined sequenced state transformation to the model definition. + * + * @param array $sequence + * @return static + */ + public function crossJoinSequence(...$sequence) + { + return $this->state(new CrossJoinSequence(...$sequence)); + } + /** * Define a child relationship for the model. * diff --git a/tests/Database/DatabaseEloquentFactoryTest.php b/tests/Database/DatabaseEloquentFactoryTest.php index f30276d06ffd..3b7e28217b49 100644 --- a/tests/Database/DatabaseEloquentFactoryTest.php +++ b/tests/Database/DatabaseEloquentFactoryTest.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Factories\CrossJoinSequence; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\Sequence; @@ -413,6 +414,45 @@ public function test_sequences() $this->assertSame('index: 1', $users[1]->name); } + public function test_cross_join_sequences() + { + $assert = function ($users) { + $assertions = [ + ['first_name' => 'Thomas', 'last_name' => 'Anderson'], + ['first_name' => 'Thomas', 'last_name' => 'Smith'], + ['first_name' => 'Agent', 'last_name' => 'Anderson'], + ['first_name' => 'Agent', 'last_name' => 'Smith'], + ]; + + foreach ($assertions as $key => $assertion) { + $this->assertSame( + $assertion, + $users[$key]->only('first_name', 'last_name'), + ); + } + }; + + $usersByClass = FactoryTestUserFactory::times(4) + ->state( + new CrossJoinSequence( + [['first_name' => 'Thomas'], ['first_name' => 'Agent']], + [['last_name' => 'Anderson'], ['last_name' => 'Smith']], + ), + ) + ->make(); + + $assert($usersByClass); + + $usersByMethod = FactoryTestUserFactory::times(4) + ->crossJoinSequence( + [['first_name' => 'Thomas'], ['first_name' => 'Agent']], + [['last_name' => 'Anderson'], ['last_name' => 'Smith']], + ) + ->make(); + + $assert($usersByMethod); + } + public function test_resolve_nested_model_factories() { Factory::useNamespace('Factories\\');