From 38a596f4f346d4f2dd466a2975dc92f584f46d9f Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Wed, 11 Sep 2024 11:47:27 -0400 Subject: [PATCH] Rename method and accept argument --- src/Factories/Concerns/WithSites.php | 4 ++-- tests/FactoryTest.php | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Factories/Concerns/WithSites.php b/src/Factories/Concerns/WithSites.php index 956867d..6e22329 100644 --- a/src/Factories/Concerns/WithSites.php +++ b/src/Factories/Concerns/WithSites.php @@ -22,11 +22,11 @@ public function inRandomSite(): self ]); } - public function perSite(): self + public function inEachSite(?int $count = null): self { $sites = $this->getSitesFromContentModel()->map(fn ($site) => ['site' => $site]); - return $this->sequence(...$sites)->count(($this->count ?? 1) * $sites->count()); + return $this->sequence(...$sites)->count(($count ?? $this->count ?? 1) * $sites->count()); } protected function getSitesFromContentModel(): Collection diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index d42bb5c..8838fd4 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -342,7 +342,11 @@ public function test_entry_can_be_created_in_site() $this->assertCount(10, $entries); $entries->each(fn ($entry) => $this->assertContains($entry->locale(), ['default', 'german'])); - $entries = FactoryTestEntryFactory::times(5)->perSite()->create(); + $entries = FactoryTestEntryFactory::times(5)->inEachSite()->create(); + $this->assertCount(10, $entries); + $entries->each(fn ($entry, $index) => $this->assertSame($index % 2 === 0 ? 'default' : 'german', $entry->locale())); + + $entries = FactoryTestEntryFactory::new()->inEachSite(5)->create(); $this->assertCount(10, $entries); $entries->each(fn ($entry, $index) => $this->assertSame($index % 2 === 0 ? 'default' : 'german', $entry->locale())); } @@ -362,7 +366,12 @@ public function test_term_can_be_created_in_site() $localizations = $terms->map->fileData()->flatMap(fn ($data) => data_get($data, 'localizations', [])); $this->assertNotContains($localizations, ['random']); - $terms = FactoryTestTermFactory::times(5)->perSite()->create(); + $terms = FactoryTestTermFactory::times(5)->inEachSite()->create(); + $localizations = $terms->map->fileData()->map(fn ($data) => data_get($data, 'localizations', [])); + $this->assertEquals($localizations->count(), 10); + $this->assertEquals($localizations->filter()->count(), 5); + + $terms = FactoryTestTermFactory::new()->inEachSite(5)->create(); $localizations = $terms->map->fileData()->map(fn ($data) => data_get($data, 'localizations', [])); $this->assertEquals($localizations->count(), 10); $this->assertEquals($localizations->filter()->count(), 5);