Skip to content

Commit

Permalink
[1.x] stories using simple objects
Browse files Browse the repository at this point in the history
a wip to fix using stories with objects that can't be persisted

This is intended to be legacy code that shouldn't be merged / released in 2.x.

fixes: zenstruck#573
  • Loading branch information
jrushlow committed Mar 30, 2024
1 parent d38568f commit fb5010d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Factory
/** @var callable[] */
private array $afterPersist = [];

private bool $shouldPersist = true;

/**
* @param class-string<TObject> $class
*/
Expand Down Expand Up @@ -156,10 +158,10 @@ public function create(
}

if (!$this->isPersisting(calledInternally: true)) {
return $noProxy ? $object : new ProxyObject($object);
return $noProxy ? $object : new ProxyObject($object, $this->shouldPersist);
}

$proxy = new ProxyObject($object);
$proxy = new ProxyObject($object, $this->shouldPersist);

if ($this->cascadePersist && !$postPersistCallbacks) {
return $proxy;
Expand Down Expand Up @@ -228,6 +230,7 @@ public function withoutPersisting(

$cloned = clone $this;
$cloned->persist = false;
$cloned->shouldPersist = false;

return $cloned;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ final class Proxy implements \Stringable, ProxyBase
public function __construct(
/** @param TProxiedObject $object */
private object $object,

private bool $shouldPersist = true,
) {
if ((new \ReflectionClass($object::class))->isFinal()) {
trigger_deprecation(
Expand Down Expand Up @@ -174,6 +176,10 @@ public function save(): static

public function _save(): static
{
if (!$this->shouldPersist) {
return $this;
}

$this->objectManager()->persist($this->object);

if (Factory::configuration()->isFlushingEnabled()) {
Expand Down
29 changes: 29 additions & 0 deletions tests/Fixtures/Factories/LegacyObjectFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Zenstruck\Foundry\Tests\Fixtures\Factories;

use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Tests\Fixtures\Object\SomeObject;

/**
* @author Jesse Rushlow <jr@rushlow.dev>
*/
class LegacyObjectFactory extends ModelFactory
{
protected static function getClass(): string
{
return SomeObject::class;
}

protected function getDefaults(): array
{
return [
'propertyWithoutType' => self::faker()->word(),
];
}

protected function initialize(): self
{
return $this->withoutPersisting();
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/Stories/LegacyObjectStory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Zenstruck\Foundry\Tests\Fixtures\Stories;

use Zenstruck\Foundry\Story;
use Zenstruck\Foundry\Tests\Fixtures\Factories\LegacyObjectFactory;
use Zenstruck\Foundry\Tests\Fixtures\Object\SomeObject;

/**
* @author Jesse Rushlow <jr@rushlow.dev>
*
* @method static SomeObject simpleObject()
*/
class LegacyObjectStory extends Story
{
public function build(): void
{
$this->addState('simpleObject', LegacyObjectFactory::createOne());
}
}
12 changes: 12 additions & 0 deletions tests/Functional/StoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory;
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory;
use Zenstruck\Foundry\Tests\Fixtures\Object\SomeObject;
use Zenstruck\Foundry\Tests\Fixtures\Stories\CategoryPoolStory;
use Zenstruck\Foundry\Tests\Fixtures\Stories\CategoryStory;
use Zenstruck\Foundry\Tests\Fixtures\Stories\LegacyObjectStory;
use Zenstruck\Foundry\Tests\Fixtures\Stories\PostStory;
use Zenstruck\Foundry\Tests\Fixtures\Stories\ServiceStory;
use Zenstruck\Foundry\Tests\Fixtures\Stories\StoryWhichReadsItsOwnPool;
Expand Down Expand Up @@ -237,4 +239,14 @@ public function story_can_access_its_own_pool(): void

self::assertContains($item->_real()->getName(), ['php', 'symfony']);
}

/**
* @test
*/
public function story_does_not_persist_legacy_object(): void
{
LegacyObjectStory::load();

self::assertInstanceOf(Proxy::class, LegacyObjectStory::simpleObject());
}
}

0 comments on commit fb5010d

Please sign in to comment.