-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
#[WithStory]
attribute (#728)
- Loading branch information
Showing
13 changed files
with
394 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Attribute; | ||
|
||
use Zenstruck\Foundry\Story; | ||
|
||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)] | ||
final class WithStory | ||
{ | ||
public function __construct( | ||
/** @var class-string<Story> $story */ | ||
public readonly string $story | ||
) | ||
{ | ||
if (!\is_subclass_of($story, Story::class)) { | ||
throw new \InvalidArgumentException(\sprintf('"%s" is not a valid story class.', $story)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\PHPUnit; | ||
|
||
use PHPUnit\Event; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Attribute\WithStory; | ||
|
||
/** | ||
* @internal | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class BuildStoryOnTestPrepared implements Event\Test\PreparedSubscriber | ||
{ | ||
public function notify(Event\Test\Prepared $event): void | ||
{ | ||
$test = $event->test(); | ||
|
||
if (!$test->isTestMethod()) { | ||
return; | ||
} | ||
|
||
/** @var Event\Code\TestMethod $test */ | ||
|
||
$reflectionClass = new \ReflectionClass($test->className()); | ||
$withStoryAttributes = [ | ||
...$this->collectWithStoryAttributesFromClassAndParents($reflectionClass), | ||
...$reflectionClass->getMethod($test->methodName())->getAttributes(WithStory::class), | ||
]; | ||
|
||
if (!$withStoryAttributes) { | ||
return; | ||
} | ||
|
||
if (!is_subclass_of($test->className(), KernelTestCase::class)) { | ||
throw new \InvalidArgumentException( | ||
\sprintf( | ||
'The test class "%s" must extend "%s" to use the "%s" attribute.', | ||
$test->className(), | ||
KernelTestCase::class, | ||
WithStory::class | ||
) | ||
); | ||
} | ||
|
||
foreach ($withStoryAttributes as $withStoryAttribute) { | ||
$withStoryAttribute->newInstance()->story::load(); | ||
} | ||
} | ||
|
||
/** | ||
* @return list<\ReflectionAttribute<WithStory>> | ||
*/ | ||
private function collectWithStoryAttributesFromClassAndParents(\ReflectionClass $class): array // @phpstan-ignore missingType.generics | ||
{ | ||
return [ | ||
...$class->getAttributes(WithStory::class), | ||
...( | ||
$class->getParentClass() | ||
? $this->collectWithStoryAttributesFromClassAndParents($class->getParentClass()) | ||
: [] | ||
) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/Integration/Attribute/WithStory/ParentClassWithStoryAttributeTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Integration\Attribute\WithStory; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Attribute\WithStory; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
use Zenstruck\Foundry\Tests\Fixture\Stories\EntityStory; | ||
use Zenstruck\Foundry\Tests\Integration\RequiresORM; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
#[WithStory(EntityStory::class)] | ||
abstract class ParentClassWithStoryAttributeTestCase extends KernelTestCase | ||
{ | ||
use Factories, ResetDatabase, RequiresORM; | ||
} |
Oops, something went wrong.