diff --git a/docs/index.rst b/docs/index.rst index 35a40178f..419356b7d 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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'])); } } diff --git a/src/Story.php b/src/Story.php index 757180615..6b03b37a9 100644 --- a/src/Story.php +++ b/src/Story.php @@ -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 @@ -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; } diff --git a/tests/Fixtures/Stories/CategoryStory.php b/tests/Fixtures/Stories/CategoryStory.php index ea0ecf367..73d7a30ba 100644 --- a/tests/Fixtures/Stories/CategoryStory.php +++ b/tests/Fixtures/Stories/CategoryStory.php @@ -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'])); } } diff --git a/tests/Fixtures/Stories/PostStory.php b/tests/Fixtures/Stories/PostStory.php index 08f369f00..dfc746eea 100644 --- a/tests/Fixtures/Stories/PostStory.php +++ b/tests/Fixtures/Stories/PostStory.php @@ -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(), ])); diff --git a/tests/Fixtures/Stories/ServiceStory.php b/tests/Fixtures/Stories/ServiceStory.php index 6b0d575f2..f511f9432 100644 --- a/tests/Fixtures/Stories/ServiceStory.php +++ b/tests/Fixtures/Stories/ServiceStory.php @@ -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])); } } diff --git a/tests/Fixtures/Stories/TagStory.php b/tests/Fixtures/Stories/TagStory.php index 18f433ccb..affb6ecbb 100644 --- a/tests/Fixtures/Stories/TagStory.php +++ b/tests/Fixtures/Stories/TagStory.php @@ -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'])); } } diff --git a/tests/Functional/StoryTest.php b/tests/Functional/StoryTest.php index abfb08466..b390cb894 100644 --- a/tests/Functional/StoryTest.php +++ b/tests/Functional/StoryTest.php @@ -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; @@ -15,7 +16,7 @@ */ final class StoryTest extends KernelTestCase { - use Factories, ResetDatabase; + use ExpectDeprecationTrait, Factories, ResetDatabase; protected function setUp(): void { @@ -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'); + } }