Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e57d750

Browse files
committed
refactor: harmonize how factories are proxified in data providers (#1065)
1 parent 523cc05 commit e57d750

4 files changed

Lines changed: 17 additions & 33 deletions

File tree

src/Persistence/PersistentObjectFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ public function create(callable|array $attributes = []): object
241241
if ($configuration->inADataProvider()
242242
&& \PHP_VERSION_ID >= 80400
243243
&& $this->isPersisting()
244-
&& !$this instanceof PersistentProxyObjectFactory
245244
) {
246-
return ProxyGenerator::wrapFactoryNativeProxy($this, $attributes);
245+
return ProxyGenerator::wrapFactory($this->with($attributes));
247246
}
248247

249248
$object = parent::create($attributes);

src/Persistence/PersistentProxyObjectFactory.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ final public function create(callable|array $attributes = []): object
4444
throw new \LogicException('PersistentProxyObjectFactory can no longer be used with Symfony 8. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.7.md to get rid of Foundry\'s proxy mechanism, and upgrade to Symfony 8.');
4545
}
4646

47-
$configuration = Configuration::instance();
48-
if ($configuration->inADataProvider() && $this->isPersisting()) {
49-
return ProxyGenerator::wrapFactory($this, $attributes);
50-
}
51-
5247
return proxy(parent::create($attributes)); // @phpstan-ignore function.unresolvableReturnType
5348
}
5449

src/Persistence/ProxyGenerator.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,47 +47,37 @@ public static function wrap(object $object): Proxy
4747
return self::generateClassFor($object)::createLazyProxy(static fn() => $object); // @phpstan-ignore staticMethod.unresolvableReturnType
4848
}
4949

50-
/**
51-
* @template T of object
52-
*
53-
* @param PersistentProxyObjectFactory<T> $factory
54-
* @phpstan-param Attributes $attributes
55-
*
56-
* @return T&Proxy<T>
57-
*/
58-
public static function wrapFactory(PersistentProxyObjectFactory $factory, callable|array $attributes): Proxy
59-
{
60-
return self::generateClassFor($factory)::createLazyProxy(static function() use ($factory, $attributes) { // @phpstan-ignore staticMethod.notFound
61-
if (Configuration::instance()->inADataProvider() && $factory->isPersisting()) {
62-
throw new \LogicException('Cannot access to a persisted object from a data provider.');
63-
}
64-
65-
return self::unwrap($factory->create($attributes));
66-
});
67-
}
68-
6950
/**
7051
* @template T of object
7152
*
7253
* @param PersistentObjectFactory<T> $factory
73-
* @phpstan-param Attributes $attributes
7454
*
75-
* @return T
55+
* @return ($factory is PersistentProxyObjectFactory<T> ? T&Proxy<T> : T)
7656
*/
77-
public static function wrapFactoryNativeProxy(PersistentObjectFactory $factory, callable|array $attributes): object
57+
public static function wrapFactory(PersistentObjectFactory $factory): object
7858
{
59+
if ($factory instanceof PersistentProxyObjectFactory) {
60+
return self::generateClassFor($factory)::createLazyProxy(static function () use ($factory) { // @phpstan-ignore staticMethod.notFound
61+
if (Configuration::instance()->inADataProvider() && $factory->isPersisting()) {
62+
throw new \LogicException('Cannot access to a persisted object inside a data provider.');
63+
}
64+
65+
return ProxyGenerator::unwrap($factory->create());
66+
});
67+
}
68+
7969
if (\PHP_VERSION_ID < 80400) {
8070
throw new \LogicException('Native proxy generation requires PHP 8.4 or higher.');
8171
}
8272

8373
$reflector = new \ReflectionClass($factory::class());
8474

85-
return $reflector->newLazyProxy(static function() use ($factory, $attributes) {
75+
return $reflector->newLazyProxy(static function () use ($factory) {
8676
if (Configuration::instance()->inADataProvider() && $factory->isPersisting()) {
87-
throw new \LogicException('Cannot access to a persisted object from a data provider.');
77+
throw new \LogicException('Cannot access to a persisted object inside a data provider.');
8878
}
8979

90-
return $factory->create($attributes);
80+
return $factory->create();
9181
});
9282
}
9383

tests/Integration/DataProvider/DataProviderWithPersistentFactoryInKernelTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static function createMultipleObjectsInDataProvider(): iterable
101101
public function assert_using_getter_proxy_object_created_in_a_data_provider_throws(?\Throwable $e): void
102102
{
103103
self::assertInstanceOf(\LogicException::class, $e);
104-
self::assertStringStartsWith('Cannot access to a persisted object from a data provider.', $e->getMessage());
104+
self::assertStringStartsWith('Cannot access to a persisted object inside a data provider.', $e->getMessage());
105105
}
106106

107107
public static function useGetterOnProxyObjectCreatedInDataProvider(): iterable

0 commit comments

Comments
 (0)