From 02a577f4f4ee213320e893d2e821147a798ee94b Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Mon, 9 Sep 2024 11:54:29 -0400 Subject: [PATCH] Simplify published state method --- src/Factories/Factory.php | 9 ++------- tests/FactoryTest.php | 33 +++++++++++---------------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/Factories/Factory.php b/src/Factories/Factory.php index f483448..971b776 100644 --- a/src/Factories/Factory.php +++ b/src/Factories/Factory.php @@ -263,14 +263,9 @@ protected function evaluateSite(): self return $this->inSite($evaluatedSite); } - public function published(bool|string $published): self + public function unpublished(): self { - return match ($published) { - 'random' => $this->sequence(fn (Sequence $sequence) => ['published' => collect([true, false])->random()]), - 'sequence' => $this->sequence(fn (Sequence $sequence) => ['published' => true], ['published' => false]), - false, 'false' => $this->set('published', false), - default => $this->set('published', true), - }; + return $this->set('published', false); } protected function getSitesFromContentModel(): Collection diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 93f834d..4c6e73d 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -2,18 +2,19 @@ namespace Aerni\Factory\Tests; -use Aerni\Factory\Factories\Factory; -use Illuminate\Database\Eloquent\Factories\CrossJoinSequence; -use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Config; use ReflectionClass; +use Statamic\Facades\Site; +use Illuminate\Support\Collection; +use Aerni\Factory\Factories\Factory; use Statamic\Contracts\Entries\Entry; +use Illuminate\Support\Facades\Config; use Statamic\Contracts\Taxonomies\Term; -use Statamic\Facades\Collection as CollectionFacade; +use Statamic\Facades\Term as TermFacade; use Statamic\Facades\Entry as EntryFacade; -use Statamic\Facades\Site; use Statamic\Facades\Taxonomy as TaxonomyFacade; -use Statamic\Facades\Term as TermFacade; +use Illuminate\Database\Eloquent\Factories\Sequence; +use Statamic\Facades\Collection as CollectionFacade; +use Illuminate\Database\Eloquent\Factories\CrossJoinSequence; use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk; class FactoryTest extends TestCase @@ -366,25 +367,13 @@ public function test_term_can_be_created_in_site() $this->assertEquals($localizations->filter()->count(), 5); } - public function test_can_set_publish_state() + public function test_entry_can_be_unpublished() { - $entry = FactoryTestEntryFactory::new()->published(true)->create(); + $entry = FactoryTestEntryFactory::new()->create(); $this->assertSame(true, $entry->published()); - $entry = FactoryTestEntryFactory::new()->published(false)->create(); - $this->assertSame(false, $entry->published()); - - $entry = FactoryTestEntryFactory::new()->published('false')->create(); + $entry = FactoryTestEntryFactory::new()->unpublished()->create(); $this->assertSame(false, $entry->published()); - - $entry = FactoryTestEntryFactory::new()->published('random')->create(); - $this->assertContains($entry->published(), [true, false]); - - $entry = FactoryTestEntryFactory::new()->published('anything')->create(); - $this->assertSame(true, $entry->published()); - - $entries = FactoryTestEntryFactory::times(10)->published('sequence')->create(); - $entries->each(fn ($entry, $index) => $this->assertSame($index % 2 === 0 ? true : false, $entry->published())); } }