Skip to content

Commit

Permalink
Simplify published state method
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Sep 9, 2024
1 parent ee4813e commit 02a577f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
9 changes: 2 additions & 7 deletions src/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 11 additions & 22 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()));
}
}

Expand Down

0 comments on commit 02a577f

Please sign in to comment.