Skip to content

Commit

Permalink
[minor] deprecate Story:add() and add Story::addState() (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Mar 4, 2022
1 parent 02609a9 commit 02cd0c8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1645,10 +1645,10 @@ Another feature of *stories* is the ability for them to *remember* the objects t
{
public function build(): void
{
$this->add('php', CategoryFactory::createOne(['name' => 'php']));
$this->addState('php', CategoryFactory::createOne(['name' => 'php']));
// factories are created when added as state
$this->add('symfony', CategoryFactory::new(['name' => 'symfony']));
$this->addState('symfony', CategoryFactory::new(['name' => 'symfony']));
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ final public static function load(): self
* @return static
*/
final public function add(string $name, $state): self
{
trigger_deprecation('zenstruck\foundry', '1.17.0', 'Using Story::add() is deprecated, use Story::addState().');

return $this->addState($name, $state);
}

final public function get(string $name): Proxy
{
if (!\array_key_exists($name, $this->state)) {
throw new \InvalidArgumentException(\sprintf('"%s" was not registered. Did you forget to call "%s::add()"?', $name, static::class));
}

return $this->state[$name];
}

abstract public function build(): void;

final protected function addState(string $name, $state): self
{
if (\is_object($state)) {
// ensure factories are persisted
Expand All @@ -54,15 +72,4 @@ final public function add(string $name, $state): self

return $this;
}

final public function get(string $name): Proxy
{
if (!\array_key_exists($name, $this->state)) {
throw new \InvalidArgumentException(\sprintf('"%s" was not registered. Did you forget to call "%s::add()"?', $name, static::class));
}

return $this->state[$name];
}

abstract public function build(): void;
}
4 changes: 2 additions & 2 deletions tests/Fixtures/Stories/CategoryStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class CategoryStory extends Story
{
public function build(): void
{
$this->add('php', CategoryFactory::new()->create(['name' => 'php']));
$this->add('symfony', CategoryFactory::new()->create(['name' => 'symfony']));
$this->addState('php', CategoryFactory::new()->create(['name' => 'php']));
$this->addState('symfony', CategoryFactory::new()->create(['name' => 'symfony']));
}
}
6 changes: 3 additions & 3 deletions tests/Fixtures/Stories/PostStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ final class PostStory extends Story
{
public function build(): void
{
$this->add('postA', PostFactory::new()->create([
$this->addState('postA', PostFactory::new()->create([
'title' => 'Post A',
'category' => CategoryStory::php(),
]));

$this->add('postB', PostFactory::new()->create([
$this->addState('postB', PostFactory::new()->create([
'title' => 'Post B',
'category' => CategoryStory::php(),
])->object());

$this->add('postC', PostFactory::new([
$this->addState('postC', PostFactory::new([
'title' => 'Post C',
'category' => CategoryStory::symfony(),
]));
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Stories/ServiceStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public function __construct(Service $service)

public function build(): void
{
$this->add('post', PostFactory::new()->create(['title' => $this->service->name]));
$this->addState('post', PostFactory::new()->create(['title' => $this->service->name]));
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/Stories/TagStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class TagStory extends Story
{
public function build(): void
{
$this->add('dev', TagFactory::new()->create(['name' => 'dev']));
$this->add('design', TagFactory::new()->create(['name' => 'design']));
$this->addState('dev', TagFactory::new()->create(['name' => 'dev']));
$this->addState('design', TagFactory::new()->create(['name' => 'design']));
}
}
14 changes: 13 additions & 1 deletion tests/Functional/StoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Zenstruck\Foundry\Tests\Functional;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
Expand All @@ -15,7 +16,7 @@
*/
final class StoryTest extends KernelTestCase
{
use Factories, ResetDatabase;
use ExpectDeprecationTrait, Factories, ResetDatabase;

protected function setUp(): void
{
Expand Down Expand Up @@ -90,4 +91,15 @@ public function service_stories_cannot_be_used_without_the_bundle(): void

ServiceStory::load();
}

/**
* @test
* @group legacy
*/
public function calling_add_is_deprecated(): void
{
$this->expectDeprecation('Since zenstruck\foundry 1.17.0: Using Story::add() is deprecated, use Story::addState().');

CategoryStory::load()->add('foo', 'bar');
}
}

0 comments on commit 02cd0c8

Please sign in to comment.