Skip to content

Commit

Permalink
fix: allow non object state in stories (#699)
Browse files Browse the repository at this point in the history
* fix: allow non object state in stories

fixes #697

* fix: removed no more needed argument.templateType phpstan ignore

* fix: revert phpstan ignore argument.templateType

* tests: ensure non object states can be accessed
  • Loading branch information
Brewal authored Oct 3, 2024
1 parent 6482357 commit 0f72ea5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@ final protected function getState(string $name): mixed
throw new \InvalidArgumentException(\sprintf('"%s" was not registered. Did you forget to call "%s::addState()"?', $name, static::class));
}

if (!\is_object($this->state[$name])) {
return $this->state[$name];
}

try {
$isProxy = $this->state[$name] instanceof Proxy;

$unwrappedObject = ProxyGenerator::unwrap($this->state[$name]);
Configuration::instance()->persistence()->refresh($unwrappedObject, force: true); // @phpstan-ignore argument.templateType
Configuration::instance()->persistence()->refresh($unwrappedObject, force: true);

return $isProxy ? ProxyGenerator::wrap($unwrappedObject) : $unwrappedObject; // @phpstan-ignore argument.templateType
} catch (PersistenceNotAvailable|NoPersistenceStrategy|RefreshObjectFailed) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixture/Stories/DocumentStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@
*
* @method static GenericDocument foo()
* @method static GenericDocument bar()
* @method static int int()
* @method static float float()
* @method static string string()
* @method static bool bool()
* @method static array array()
* @method static null null()
*/
final class DocumentStory extends Story
{
public function build(): void
{
$this->addState('foo', GenericDocumentFactory::createOne(['prop1' => 'foo']));
$this->addState('bar', GenericDocumentFactory::createOne(['prop1' => 'bar']));
$this->addState('int', 12);
$this->addState('float', 12.12);
$this->addState('string', 'dummyString');
$this->addState('bool', true);
$this->addState('array', [12, 'dummyString', [true, 12.12]]);
$this->addState('null', null);
}
}
12 changes: 12 additions & 0 deletions tests/Fixture/Stories/EntityStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@
*
* @method static GenericEntity foo()
* @method static GenericEntity bar()
* @method static int int()
* @method static float float()
* @method static string string()
* @method static bool bool()
* @method static array array()
* @method static null null()
*/
final class EntityStory extends Story
{
public function build(): void
{
$this->addState('foo', GenericEntityFactory::createOne(['prop1' => 'foo']), 'pool');
$this->addState('bar', GenericEntityFactory::createOne(['prop1' => 'bar']), 'pool');
$this->addState('int', 12);
$this->addState('float', 12.12);
$this->addState('string', 'dummyString');
$this->addState('bool', true);
$this->addState('array', [12, 'dummyString', [true, 12.12]]);
$this->addState('null', null);
}
}
18 changes: 18 additions & 0 deletions tests/Integration/Persistence/StoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public function can_access_story_state(string $story): void
{
$this->assertSame('foo', $story::get('foo')->getProp1());
$this->assertSame('bar', $story::get('bar')->getProp1());
$this->assertSame(12, $story::get('int'));
$this->assertSame(12.12, $story::get('float'));
$this->assertSame('dummyString', $story::get('string'));
$this->assertTrue($story::get('bool'));
$this->assertSame([12, 'dummyString', [true, 12.12]], $story::get('array'));
$this->assertNull($story::get('null'));
}

/**
Expand All @@ -132,6 +138,12 @@ public function can_access_story_state_with_magic_call(string $story): void
{
$this->assertSame('foo', $story::foo()->getProp1());
$this->assertSame('bar', $story::bar()->getProp1());
$this->assertSame(12, $story::int());
$this->assertSame(12.12, $story::float());
$this->assertSame('dummyString', $story::string());
$this->assertTrue($story::bool());
$this->assertSame([12, 'dummyString', [true, 12.12]], $story::array());
$this->assertNull($story::null());
}

/**
Expand All @@ -144,6 +156,12 @@ public function can_access_story_state_with_magic_call_on_instance(string $story
{
$this->assertSame('foo', $story::load()->foo()->getProp1());
$this->assertSame('bar', $story::load()->bar()->getProp1());
$this->assertSame(12, $story::load()->int());
$this->assertSame(12.12, $story::load()->float());
$this->assertSame('dummyString', $story::load()->string());
$this->assertTrue($story::load()->bool());
$this->assertSame([12, 'dummyString', [true, 12.12]], $story::load()->array());
$this->assertNull($story::load()->null());
}

/**
Expand Down

0 comments on commit 0f72ea5

Please sign in to comment.