Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Sep 11, 2024
1 parent 2972f59 commit e3b88a1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Factories/Concerns/WithSites.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ public function inSite(string $site): self

public function inRandomSite(): self
{
return $this->state(fn () => ['site' => $this->getSitesFromContentModel()->random()]);
return $this->state(fn () => [
'site' => $this->getSitesFromContentModel()->random(),
'isRandomSite' => true,
]);
}

public function perSite(): self
{
$sites = $this->getSitesFromContentModel()->map(fn ($site) => ['site' => $site]);

// TODO: This won't work if count() is used after perSite(). Can we make this work with a callback?
return $this->sequence(...$sites)->count(($this->count ?? 1) * $sites->count());
}

Expand Down
10 changes: 7 additions & 3 deletions src/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ protected function evaluateSiteStates(Collection $states): Collection
{
$evaluatedSiteStates = $states
->map(fn ($state) => (clone $state)()) /* Clone the closure so that we don't run into issues when evaluating the same closure later. Needed for sequences to work correctly. */
->filter(fn ($state) => isset($state['site']));
->filter(fn ($state) => isset($state['site']))
->map(fn ($state, $index) => array_merge(['index' => $index], $state));

if ($evaluatedSiteStates->isEmpty()) {
return $states;
Expand All @@ -190,9 +191,12 @@ protected function evaluateSiteStates(Collection $states): Collection

$this->faker = Container::getInstance()->makeWith(Generator::class, ['locale' => $site->locale()]);

// TODO: This breaks the perSite() sequence but works for inSite() and inRandomSite().
$siteState = ! isset($siteState['isRandomSite'])
? $states->get($siteState['index'])
: fn () => ['site' => $site->handle()]; /* Explicitly set the evaluated random site so that we don't get a new random site later. */

return $states->diffKeys($evaluatedSiteStates)
->push(fn () => ['site' => $site->handle()])
->push($siteState)
->values();
}

Expand Down
1 change: 0 additions & 1 deletion tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ public function test_entry_can_be_created_in_site()

$entries = FactoryTestEntryFactory::times(5)->perSite()->create();
$this->assertCount(10, $entries);
dd($entries->map->locale());
$entries->each(fn ($entry, $index) => $this->assertSame($index % 2 === 0 ? 'default' : 'german', $entry->locale()));
}

Expand Down

0 comments on commit e3b88a1

Please sign in to comment.