From ee4813e41a0956ed64be7b758f2f17ca4caf5560 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Sat, 7 Sep 2024 13:48:17 -0400 Subject: [PATCH] Better site methods --- src/Factories/Factory.php | 18 ++++++++++++++---- tests/FactoryTest.php | 20 ++++++++++++-------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Factories/Factory.php b/src/Factories/Factory.php index 9292960..f483448 100644 --- a/src/Factories/Factory.php +++ b/src/Factories/Factory.php @@ -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 diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 2c5aafe..93f834d 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -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); }