Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Added class and method to create cross joined sequences for factories #40542

Merged
merged 4 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Eloquent/Factories/CrossJoinSequence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Illuminate\Database\Eloquent\Factories;

use Illuminate\Support\Arr;

class CrossJoinSequence extends Sequence
{
/**
* Create a new cross join sequence instance.
*
* @param array $sequences
* @return void
*/
public function __construct(...$sequences)
{
$crossJoined = array_map(
function ($a) {
return array_merge(...$a);
},
Arr::crossJoin(...$sequences),
);

parent::__construct(...$crossJoined);
}
}
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,17 @@ public function sequence(...$sequence)
return $this->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.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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\\');
Expand Down