diff --git a/src/Story.php b/src/Story.php index e617edd7b..7e3c5880a 100644 --- a/src/Story.php +++ b/src/Story.php @@ -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) { diff --git a/tests/Fixture/Stories/DocumentStory.php b/tests/Fixture/Stories/DocumentStory.php index 1fac2ef41..55c2bb929 100644 --- a/tests/Fixture/Stories/DocumentStory.php +++ b/tests/Fixture/Stories/DocumentStory.php @@ -20,6 +20,12 @@ * * @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 { @@ -27,5 +33,11 @@ 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); } } diff --git a/tests/Fixture/Stories/EntityStory.php b/tests/Fixture/Stories/EntityStory.php index a83bfda97..2eebe5379 100644 --- a/tests/Fixture/Stories/EntityStory.php +++ b/tests/Fixture/Stories/EntityStory.php @@ -20,6 +20,12 @@ * * @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 { @@ -27,5 +33,11 @@ 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); } } diff --git a/tests/Integration/Persistence/StoryTest.php b/tests/Integration/Persistence/StoryTest.php index 35fdb6919..573288aa9 100644 --- a/tests/Integration/Persistence/StoryTest.php +++ b/tests/Integration/Persistence/StoryTest.php @@ -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')); } /** @@ -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()); } /** @@ -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()); } /**