Skip to content

Commit

Permalink
Better site methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Sep 7, 2024
1 parent 79835e4 commit ee4813e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,31 @@ public function count(?int $count): self
return $this->newInstance(['count' => $count]);
}

public function site(string $site): self
public function inSite(string $site): self
{
return $this->newInstance(['site' => $site]);
}

public function inRandomSite(): self
{
return $this->inSite('inRandomSite');
}

public function perSite(): self
{
return $this->inSite('perSite')->count($this->getSitesFromContentModel()->count() * ($this->count ?? 1));
}

protected function evaluateSite(): self
{
$evaluatedSite = match (true) {
$this->getSitesFromContentModel()->contains($this->site) => $this->site,
$this->site === 'random' => $this->getSitesFromContentModel()->random(),
$this->site === 'sequence' => once(fn () => new Sequence(...$this->getSitesFromContentModel()))(), /* We are using once() so that the Sequence works correctly and isn't created afresh every time this method is called. */
$this->site === 'inRandomSite' => $this->getSitesFromContentModel()->random(),
$this->site === 'perSite' => once(fn () => new Sequence(...$this->getSitesFromContentModel()))(), /* We are using once() so that the Sequence works correctly and isn't created afresh every time this method is called. */
default => $this->getDefaultSiteFromContentModel(),
};

return $this->site($evaluatedSite);
return $this->inSite($evaluatedSite);
}

public function published(bool|string $published): self
Expand Down
20 changes: 12 additions & 8 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,35 +330,39 @@ public function test_factory_can_conditionally_execute_code()

public function test_entry_can_be_created_in_site()
{
$entry = FactoryTestEntryFactory::new()->site('german')->create();
$entry = FactoryTestEntryFactory::new()->inSite('german')->create();
$this->assertSame('german', $entry->locale());

$entry = FactoryTestEntryFactory::new()->site('nonexsiting_site')->create();
$entry = FactoryTestEntryFactory::new()->inSite('nonexsiting_site')->create();
$this->assertSame($entry->sites()->first(), $entry->locale());

$entries = FactoryTestEntryFactory::times(10)->site('random')->create();
$entries = FactoryTestEntryFactory::times(10)->inRandomSite()->create();
$this->assertCount(10, $entries);
$entries->each(fn ($entry) => $this->assertContains($entry->locale(), ['default', 'german']));

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

public function test_term_can_be_created_in_site()
{
$term = FactoryTestTermFactory::new()->site('german')->create();
$term = FactoryTestTermFactory::new()->inSite('german')->create();
$this->assertNotEmpty($term->dataForLocale('default'));
$this->assertNotEmpty($term->dataForLocale('german'));

$term = FactoryTestTermFactory::new()->site('nonexsiting_site')->create();
$term = FactoryTestTermFactory::new()->inSite('nonexsiting_site')->create();
$this->assertNotEmpty($term->dataForLocale('default'));
$this->assertEmpty($term->dataForLocale('nonexsiting_site'));

$terms = FactoryTestTermFactory::times(10)->site('random')->create();
$terms = FactoryTestTermFactory::times(10)->inRandomSite()->create();
$this->assertCount(10, $terms);
$localizations = $terms->map->fileData()->flatMap(fn ($data) => data_get($data, 'localizations', []));
$this->assertNotContains($localizations, ['random']);

$terms = FactoryTestTermFactory::times(10)->site('sequence')->create();
$terms = FactoryTestTermFactory::times(5)->perSite()->create();
$localizations = $terms->map->fileData()->map(fn ($data) => data_get($data, 'localizations', []));
$this->assertEquals($localizations->count(), 10);
$this->assertEquals($localizations->filter()->count(), 5);
}

Expand Down

0 comments on commit ee4813e

Please sign in to comment.