Skip to content

Commit

Permalink
feat: allow service story in WithStory attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Dec 2, 2024
1 parent c972482 commit 0f80faa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/PHPUnit/BuildStoryOnTestPrepared.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPUnit\Event;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Attribute\WithStory;
use Zenstruck\Foundry\Exception\CannotUseServiceStory;

/**
* @internal
Expand Down
36 changes: 36 additions & 0 deletions tests/Fixture/Stories/ServiceStory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <[email protected]>
*
* 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 <[email protected]>
*/
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()])
);
}
}
2 changes: 2 additions & 0 deletions tests/Fixture/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/Attribute/WithStory/WithStoryOnMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 0f80faa

Please sign in to comment.