Skip to content

Commit 0989c5d

Browse files
authored
fix: don't try to proxify objects that are not persistable (#646)
1 parent 2039045 commit 0989c5d

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/Persistence/PersistenceManager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,15 @@ public function embeddablePropertiesFor(object $object, string $owner): ?array
362362
}
363363
}
364364

365+
public function hasPersistenceFor(object $object): bool
366+
{
367+
try {
368+
return (bool) $this->strategyFor($object::class);
369+
} catch (NoPersistenceStrategy) {
370+
return false;
371+
}
372+
}
373+
365374
private static function canSkipSchemaReset(): bool
366375
{
367376
return self::$ormOnly && self::isDAMADoctrineTestBundleEnabled();

src/Persistence/PersistentObjectFactory.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Zenstruck\Foundry\Factory;
2020
use Zenstruck\Foundry\FactoryCollection;
2121
use Zenstruck\Foundry\ObjectFactory;
22-
use Zenstruck\Foundry\Persistence\Exception\NoPersistenceStrategy;
2322
use Zenstruck\Foundry\Persistence\Exception\NotEnoughObjects;
2423
use Zenstruck\Foundry\Persistence\Exception\RefreshObjectFailed;
2524

@@ -320,9 +319,15 @@ protected function normalizeObject(object $object): object
320319
return $object;
321320
}
322321

322+
$configuration = Configuration::instance();
323+
324+
if (!$configuration->isPersistenceAvailable() || !$configuration->persistence()->hasPersistenceFor($object)) {
325+
return $object;
326+
}
327+
323328
try {
324329
return proxy($object)->_refresh()->_real();
325-
} catch (PersistenceNotAvailable|RefreshObjectFailed|NoPersistenceStrategy|VarExportLogicException) {
330+
} catch (RefreshObjectFailed|VarExportLogicException) {
326331
return $object;
327332
}
328333
}

src/Persistence/ProxyGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private static function generateClassFor(object $object): string
7878
{
7979
/** @var class-string $class */
8080
$class = $object instanceof DoctrineProxy ? \get_parent_class($object) : $object::class;
81-
$proxyClass = \str_replace('\\', '', $class).'Proxy';
81+
$proxyClass = self::proxyClassNameFor($class);
8282

8383
/** @var class-string<LazyObjectInterface&Proxy<T>&T> $proxyClass */
8484
if (\class_exists($proxyClass, autoload: false)) {
@@ -143,4 +143,12 @@ public function __unserialize(\$data): void
143143

144144
return $proxyCode;
145145
}
146+
147+
/**
148+
* @param class-string $class
149+
*/
150+
public static function proxyClassNameFor(string $class): string
151+
{
152+
return \str_replace('\\', '', $class).'Proxy';
153+
}
146154
}

tests/Integration/Persistence/GenericFactoryTestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Zenstruck\Foundry\Exception\PersistenceDisabled;
1717
use Zenstruck\Foundry\Persistence\Exception\NotEnoughObjects;
1818
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
19+
use Zenstruck\Foundry\Persistence\ProxyGenerator;
1920
use Zenstruck\Foundry\Test\Factories;
2021
use Zenstruck\Foundry\Test\ResetDatabase;
2122
use Zenstruck\Foundry\Tests\Fixture\Model\GenericModel;
@@ -530,6 +531,15 @@ public function assert_it_ca_create_object_with_dates(): void
530531
self::assertSame($date->format(\DateTimeInterface::ATOM), $object->getDate()?->format(\DateTimeInterface::ATOM));
531532
}
532533

534+
/**
535+
* @test
536+
*/
537+
public function it_should_not_create_proxy_for_not_persistable_objects(): void
538+
{
539+
$this->factory()->create(['date' => new \DateTimeImmutable()]);
540+
self::assertFalse(class_exists(ProxyGenerator::proxyClassNameFor(\DateTimeImmutable::class)));
541+
}
542+
533543
/**
534544
* @return class-string<GenericModel>
535545
*/

0 commit comments

Comments
 (0)