Skip to content

Commit

Permalink
fix: don't try to proxify objects that are not persistable (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored Jun 20, 2024
1 parent 2039045 commit 0989c5d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ public function embeddablePropertiesFor(object $object, string $owner): ?array
}
}

public function hasPersistenceFor(object $object): bool
{
try {
return (bool) $this->strategyFor($object::class);
} catch (NoPersistenceStrategy) {
return false;
}
}

private static function canSkipSchemaReset(): bool
{
return self::$ormOnly && self::isDAMADoctrineTestBundleEnabled();
Expand Down
9 changes: 7 additions & 2 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\FactoryCollection;
use Zenstruck\Foundry\ObjectFactory;
use Zenstruck\Foundry\Persistence\Exception\NoPersistenceStrategy;
use Zenstruck\Foundry\Persistence\Exception\NotEnoughObjects;
use Zenstruck\Foundry\Persistence\Exception\RefreshObjectFailed;

Expand Down Expand Up @@ -320,9 +319,15 @@ protected function normalizeObject(object $object): object
return $object;
}

$configuration = Configuration::instance();

if (!$configuration->isPersistenceAvailable() || !$configuration->persistence()->hasPersistenceFor($object)) {
return $object;
}

try {
return proxy($object)->_refresh()->_real();
} catch (PersistenceNotAvailable|RefreshObjectFailed|NoPersistenceStrategy|VarExportLogicException) {
} catch (RefreshObjectFailed|VarExportLogicException) {
return $object;
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/Persistence/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static function generateClassFor(object $object): string
{
/** @var class-string $class */
$class = $object instanceof DoctrineProxy ? \get_parent_class($object) : $object::class;
$proxyClass = \str_replace('\\', '', $class).'Proxy';
$proxyClass = self::proxyClassNameFor($class);

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

return $proxyCode;
}

/**
* @param class-string $class
*/
public static function proxyClassNameFor(string $class): string
{
return \str_replace('\\', '', $class).'Proxy';
}
}
10 changes: 10 additions & 0 deletions tests/Integration/Persistence/GenericFactoryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Zenstruck\Foundry\Exception\PersistenceDisabled;
use Zenstruck\Foundry\Persistence\Exception\NotEnoughObjects;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
use Zenstruck\Foundry\Persistence\ProxyGenerator;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
use Zenstruck\Foundry\Tests\Fixture\Model\GenericModel;
Expand Down Expand Up @@ -530,6 +531,15 @@ public function assert_it_ca_create_object_with_dates(): void
self::assertSame($date->format(\DateTimeInterface::ATOM), $object->getDate()?->format(\DateTimeInterface::ATOM));
}

/**
* @test
*/
public function it_should_not_create_proxy_for_not_persistable_objects(): void
{
$this->factory()->create(['date' => new \DateTimeImmutable()]);
self::assertFalse(class_exists(ProxyGenerator::proxyClassNameFor(\DateTimeImmutable::class)));
}

/**
* @return class-string<GenericModel>
*/
Expand Down

0 comments on commit 0989c5d

Please sign in to comment.