From 0f80faa6c6027ae8c4b404378fa41b372011cd87 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Mon, 2 Dec 2024 20:00:00 +0100 Subject: [PATCH] feat: allow service story in WithStory attribute --- src/PHPUnit/BuildStoryOnTestPrepared.php | 1 + tests/Fixture/Stories/ServiceStory.php | 36 +++++++++++++++++++ tests/Fixture/TestKernel.php | 2 ++ .../WithStory/WithStoryOnMethodTest.php | 10 ++++++ 4 files changed, 49 insertions(+) create mode 100644 tests/Fixture/Stories/ServiceStory.php diff --git a/src/PHPUnit/BuildStoryOnTestPrepared.php b/src/PHPUnit/BuildStoryOnTestPrepared.php index 67bf0e47..c93dd8bf 100644 --- a/src/PHPUnit/BuildStoryOnTestPrepared.php +++ b/src/PHPUnit/BuildStoryOnTestPrepared.php @@ -16,6 +16,7 @@ use PHPUnit\Event; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Zenstruck\Foundry\Attribute\WithStory; +use Zenstruck\Foundry\Exception\CannotUseServiceStory; /** * @internal diff --git a/tests/Fixture/Stories/ServiceStory.php b/tests/Fixture/Stories/ServiceStory.php new file mode 100644 index 00000000..943652f8 --- /dev/null +++ b/tests/Fixture/Stories/ServiceStory.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Fixture\Stories; + +use Symfony\Component\HttpKernel\KernelInterface; +use Symfony\Component\Routing\RouterInterface; +use Zenstruck\Foundry\Story; +use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory; + +/** + * @author Nicolas PHILIPPE + */ + final class ServiceStory extends Story +{ + public function __construct( + private readonly RouterInterface $router + ) { + } + + public function build(): void + { + $this->addState( + 'foo', + GenericEntityFactory::createOne(['prop1' => $this->router->getContext()->getHost()]) + ); + } +} diff --git a/tests/Fixture/TestKernel.php b/tests/Fixture/TestKernel.php index 63e61850..589e903e 100644 --- a/tests/Fixture/TestKernel.php +++ b/tests/Fixture/TestKernel.php @@ -28,6 +28,7 @@ use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; use Zenstruck\Foundry\Tests\Fixture\Stories\GlobalInvokableService; use Zenstruck\Foundry\Tests\Fixture\Stories\GlobalStory; +use Zenstruck\Foundry\Tests\Fixture\Stories\ServiceStory; use Zenstruck\Foundry\ZenstruckFoundryBundle; /** @@ -162,6 +163,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load $c->register(GlobalInvokableService::class); $c->register(ArrayFactory::class)->setAutowired(true)->setAutoconfigured(true); $c->register(Object1Factory::class)->setAutowired(true)->setAutoconfigured(true); + $c->register(ServiceStory::class)->setAutowired(true)->setAutoconfigured(true); } protected function configureRoutes(RoutingConfigurator $routes): void diff --git a/tests/Integration/Attribute/WithStory/WithStoryOnMethodTest.php b/tests/Integration/Attribute/WithStory/WithStoryOnMethodTest.php index 18fdb5a4..96ef92d0 100644 --- a/tests/Integration/Attribute/WithStory/WithStoryOnMethodTest.php +++ b/tests/Integration/Attribute/WithStory/WithStoryOnMethodTest.php @@ -14,6 +14,7 @@ use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory; use Zenstruck\Foundry\Tests\Fixture\Stories\EntityPoolStory; use Zenstruck\Foundry\Tests\Fixture\Stories\EntityStory; +use Zenstruck\Foundry\Tests\Fixture\Stories\ServiceStory; use Zenstruck\Foundry\Tests\Integration\RequiresORM; /** @@ -47,4 +48,13 @@ public function can_use_multiple_story_in_attribute(): void { GenericEntityFactory::assert()->count(5); } + + /** + * @test + */ + #[WithStory(ServiceStory::class)] + public function can_use_service_story(): void + { + $this->assertSame('localhost', ServiceStory::get('foo')->getProp1()); + } }