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

Skip to content

Commit f91c2fa

Browse files
committed
fix: fixes after deprecating Factories trait (#1066)
1 parent c6a06e5 commit f91c2fa

12 files changed

Lines changed: 186 additions & 54 deletions

src/ObjectFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Zenstruck\Foundry;
1313

14+
use Zenstruck\Assert;
1415
use Zenstruck\Foundry\Object\Event\AfterInstantiate;
1516
use Zenstruck\Foundry\Object\Event\BeforeInstantiate;
17+
use Zenstruck\Foundry\Object\Hydrator;
1618
use Zenstruck\Foundry\Object\Instantiator;
1719
use Zenstruck\Foundry\Persistence\ProxyGenerator;
1820

@@ -85,6 +87,15 @@ final public function instantiateWith(callable $instantiator): static
8587
return $clone;
8688
}
8789

90+
/**
91+
* @internal
92+
* @phpstan-return InstantiatorCallable
93+
*/
94+
final public function instantiator(): callable
95+
{
96+
return $this->instantiator ?? Configuration::instance()->instantiator;
97+
}
98+
8899
/**
89100
* @phpstan-param callable(Parameters, class-string<T>, static):Parameters $callback
90101
*/

src/Persistence/PersistentObjectFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Persistence\ObjectRepository;
1515
use Symfony\Component\VarExporter\Exception\LogicException as VarExportLogicException;
16+
use Zenstruck\Assert;
1617
use Zenstruck\Foundry\Configuration;
1718
use Zenstruck\Foundry\Exception\FoundryNotBooted;
1819
use Zenstruck\Foundry\Exception\PersistenceDisabled;
@@ -240,7 +241,7 @@ public function create(callable|array $attributes = []): object
240241

241242
if ($configuration->inADataProvider()
242243
&& \PHP_VERSION_ID >= 80400
243-
&& $this->isPersisting()
244+
&& ($this->isPersisting() || $configuration->isInMemoryEnabled())
244245
) {
245246
return ProxyGenerator::wrapFactory($this->with($attributes));
246247
}

src/Persistence/ProxyGenerator.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,18 @@ public static function wrapFactory(PersistentObjectFactory $factory): object
7676
throw new \LogicException('Cannot access to a persisted object inside a data provider.');
7777
}
7878

79-
$createdObject = $factory->withoutPersisting()->create();
80-
81-
Hydrator::hydrateFromOtherObject($ghost, $createdObject);
82-
83-
if ($factory->isPersisting()) {
84-
Configuration::instance()->persistence()->save($ghost);
85-
}
79+
$instantiator = $factory->instantiator();
80+
81+
$factory
82+
// small hack to instantiate into the ghost object
83+
->instantiateWith(
84+
static function (array $parameters, string $class) use ($instantiator, $ghost): object {
85+
$object = $instantiator($parameters, $class);
86+
Hydrator::hydrateFromOtherObject($ghost, $object);
87+
88+
return $ghost;
89+
}
90+
)->create();
8691
});
8792
}
8893

tests/Fixture/Entity/EdgeCases/EntityWithReadonly/EntityWithReadonly.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class EntityWithReadonly
2121
#[ORM\Id]
2222
#[ORM\GeneratedValue]
2323
#[ORM\Column(type: 'integer')]
24-
public int $id;
24+
public ?int $id = null;
2525

2626
public function __construct(
2727
#[ORM\Column()]

tests/Integration/DataProvider/DataProviderWithInMemoryTest.php

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,28 @@ public static function provideContactFactory(): iterable
8989
}
9090
}
9191

92-
// todo: fixMe !
93-
// #[Test]
94-
// #[DataProvider('provideContact')]
95-
// #[AsInMemoryTest]
96-
// public function it_can_create_in_memory_objects_in_data_provider(?Contact $contact = null): void
97-
// {
98-
// self::assertInstanceOf(Contact::class, $contact);
99-
//
100-
// if (TestKernel::canUseLegacyProxy()) {
101-
// self::assertSame([ProxyGenerator::unwrap($contact)], $this->contactRepository->_all());
102-
// } else {
103-
// self::assertSame([$contact], $this->contactRepository->_all());
104-
// }
105-
//
106-
// self::assertSame(0, $this->entityManager->getRepository(Contact::class)->count());
107-
// }
108-
//
109-
// public static function provideContact(): iterable
110-
// {
111-
// if (!TestKernel::canUseLegacyProxy()) {
112-
// yield [];
113-
// return;
114-
// }
115-
//
116-
// yield [ProxyContactFactory::createOne()];
117-
// }
92+
#[Test]
93+
#[DataProvider('provideContact')]
94+
#[AsInMemoryTest]
95+
public function it_can_create_in_memory_objects_in_data_provider(?Contact $contact = null): void
96+
{
97+
self::assertInstanceOf(Contact::class, $contact);
98+
99+
if (TestKernel::canUseLegacyProxy()) {
100+
self::assertSame([ProxyGenerator::unwrap($contact)], $this->contactRepository->_all());
101+
} else {
102+
self::assertSame([$contact], $this->contactRepository->_all());
103+
}
104+
105+
self::assertSame(0, $this->entityManager->getRepository(Contact::class)->count());
106+
}
107+
108+
public static function provideContact(): iterable
109+
{
110+
yield [ContactFactory::createOne()];
111+
112+
if (TestKernel::canUseLegacyProxy()) {
113+
yield [ProxyContactFactory::createOne()];
114+
}
115+
}
118116
}

tests/Integration/DataProvider/DataProviderWithPersistentDocumentFactoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
1818
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
1919
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
20+
use Zenstruck\Foundry\Tests\Fixture\Document\DocumentWithReadonly;
2021
use Zenstruck\Foundry\Tests\Fixture\Factories\Document\GenericDocumentFactory;
22+
use Zenstruck\Foundry\Tests\Fixture\Model\Embeddable;
2123
use Zenstruck\Foundry\Tests\Integration\RequiresMongo;
24+
use function Zenstruck\Foundry\Persistence\persistent_factory;
2225

2326
/**
2427
* @author Nicolas PHILIPPE <[email protected]>
@@ -36,4 +39,16 @@ protected static function factory(): PersistentObjectFactory
3639
{
3740
return GenericDocumentFactory::new();
3841
}
42+
43+
/**
44+
* @return PersistentObjectFactory<DocumentWithReadonly>
45+
*/
46+
protected static function objectWithReadonlyFactory(): PersistentObjectFactory
47+
{
48+
return persistent_factory(DocumentWithReadonly::class, [
49+
'prop' => 1,
50+
'embedded' => new Embeddable('value1'),
51+
'date' => new \DateTimeImmutable(),
52+
]);
53+
}
3954
}

tests/Integration/DataProvider/DataProviderWithPersistentEntityFactoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
1818
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
1919
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
20+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\EntityWithReadonly\EntityWithReadonly;
2021
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory;
22+
use Zenstruck\Foundry\Tests\Fixture\Model\Embeddable;
2123
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
24+
use function Zenstruck\Foundry\Persistence\persistent_factory;
2225

2326
/**
2427
* @author Nicolas PHILIPPE <[email protected]>
@@ -36,4 +39,16 @@ protected static function factory(): PersistentObjectFactory
3639
{
3740
return GenericEntityFactory::new();
3841
}
42+
43+
/**
44+
* @return PersistentObjectFactory<EntityWithReadonly>
45+
*/
46+
protected static function objectWithReadonlyFactory(): PersistentObjectFactory
47+
{
48+
return persistent_factory(EntityWithReadonly::class, [
49+
'prop' => 1,
50+
'embedded' => new Embeddable('value1'),
51+
'date' => new \DateTimeImmutable(),
52+
]);
53+
}
3954
}

tests/Integration/DataProvider/DataProviderWithPersistentFactoryTestCase.php

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
1818
use Zenstruck\Foundry\Persistence\ProxyGenerator;
1919
use Zenstruck\Foundry\Test\ResetDatabase;
20+
use Zenstruck\Foundry\Tests\Fixture\Document\DocumentWithReadonly;
21+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\EntityWithReadonly\EntityWithReadonly;
2022
use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory;
2123
use Zenstruck\Foundry\Tests\Fixture\Model\GenericModel;
2224

@@ -208,28 +210,45 @@ public static function useGetterOnObjectCreatedInDataProvider(): iterable
208210
];
209211
}
210212

211-
// todo: FixMe! this is a known bug, currently the afterPersist callback is not called when creating objects in data provider
212-
//
213-
// #[Test]
214-
// #[DataProvider('createOneObjectInDataProviderWithAfterPersistCallback')]
215-
// public function assert_after_persist_callbacks_are_triggered(?GenericModel $providedData): void
216-
// {
217-
// static::factory()::assert()->count(1);
218-
//
219-
// self::assertSame('after persist callback', $providedData->getProp1());
220-
// }
221-
//
222-
// public static function createOneObjectInDataProviderWithAfterPersistCallback(): iterable
223-
// {
224-
// yield [
225-
// static::factory()
226-
// ->afterPersist(fn(GenericModel $object) => $object->setProp1('after persist callback'))
227-
// ->create()
228-
// ];
229-
// }
213+
#[Test]
214+
#[DataProvider('createOneObjectInDataProviderWithAfterPersistCallback')]
215+
public function assert_after_persist_callbacks_are_triggered(?GenericModel $providedData): void
216+
{
217+
static::factory()::assert()->count(1);
218+
219+
self::assertSame('after persist callback', $providedData?->getProp1());
220+
}
221+
222+
public static function createOneObjectInDataProviderWithAfterPersistCallback(): iterable
223+
{
224+
yield [
225+
static::factory()
226+
->afterPersist(fn(GenericModel $object) => $object->setProp1('after persist callback'))
227+
->create()
228+
];
229+
}
230+
231+
#[Test]
232+
#[DataProvider('createObjectWithReadonlyProperties')]
233+
public function assert_it_can_create_objects_with_readonly_properties(DocumentWithReadonly|EntityWithReadonly|null $providedData): void
234+
{
235+
static::objectWithReadonlyFactory()::assert()->count(1);
236+
237+
self::assertSame(1, $providedData?->prop);
238+
}
239+
240+
public static function createObjectWithReadonlyProperties(): iterable
241+
{
242+
yield [static::objectWithReadonlyFactory()->create()];
243+
}
230244

231245
/**
232246
* @return PersistentObjectFactory<GenericModel>
233247
*/
234248
abstract protected static function factory(): PersistentObjectFactory;
249+
250+
/**
251+
* @return PersistentObjectFactory<DocumentWithReadonly|EntityWithReadonly>
252+
*/
253+
abstract protected static function objectWithReadonlyFactory(): PersistentObjectFactory;
235254
}

tests/Integration/DataProvider/DataProviderWithProxyPersistentDocumentFactoryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
use PHPUnit\Framework\Attributes\RequiresMethod;
1616
use PHPUnit\Framework\Attributes\RequiresPhpunit;
1717
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
18+
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
19+
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
1820
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
21+
use Zenstruck\Foundry\Tests\Fixture\Document\DocumentWithReadonly;
22+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\EntityWithReadonly\EntityWithReadonly;
1923
use Zenstruck\Foundry\Tests\Fixture\Factories\Document\GenericProxyDocumentFactory;
24+
use Zenstruck\Foundry\Tests\Fixture\Model\Embeddable;
2025
use Zenstruck\Foundry\Tests\Integration\RequiresMongo;
26+
use function Zenstruck\Foundry\Persistence\proxy_factory;
2127

2228
/**
2329
* @author Nicolas PHILIPPE <[email protected]>
@@ -35,4 +41,16 @@ protected static function factory(): GenericProxyDocumentFactory
3541
{
3642
return GenericProxyDocumentFactory::new();
3743
}
44+
45+
/**
46+
* @return PersistentProxyObjectFactory<DocumentWithReadonly>
47+
*/
48+
protected static function objectWithReadonlyFactory(): PersistentObjectFactory
49+
{
50+
return proxy_factory(DocumentWithReadonly::class, [
51+
'prop' => 1,
52+
'embedded' => new Embeddable('value1'),
53+
'date' => new \DateTimeImmutable(),
54+
]);
55+
}
3856
}

tests/Integration/DataProvider/DataProviderWithProxyPersistentEntityFactoryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@
1919
use PHPUnit\Framework\Attributes\RequiresPhpunit;
2020
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
2121
use PHPUnit\Framework\Attributes\Test;
22+
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
23+
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
2224
use Zenstruck\Foundry\Persistence\Proxy;
2325
use Zenstruck\Foundry\Persistence\ProxyGenerator;
2426
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
27+
use Zenstruck\Foundry\Tests\Fixture\Document\DocumentWithReadonly;
28+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\EntityWithReadonly\EntityWithReadonly;
2529
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory;
2630
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericProxyEntityFactory;
31+
use Zenstruck\Foundry\Tests\Fixture\Model\Embeddable;
2732
use Zenstruck\Foundry\Tests\Fixture\Model\GenericModel;
2833
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
34+
use function Zenstruck\Foundry\Persistence\persistent_factory;
35+
use function Zenstruck\Foundry\Persistence\proxy_factory;
2936

3037
/**
3138
* @author Nicolas PHILIPPE <[email protected]>
@@ -86,4 +93,16 @@ protected static function factory(): GenericProxyEntityFactory
8693
{
8794
return GenericProxyEntityFactory::new();
8895
}
96+
97+
/**
98+
* @return PersistentProxyObjectFactory<EntityWithReadonly>
99+
*/
100+
protected static function objectWithReadonlyFactory(): PersistentObjectFactory
101+
{
102+
return proxy_factory(EntityWithReadonly::class, [
103+
'prop' => 1,
104+
'embedded' => new Embeddable('value1'),
105+
'date' => new \DateTimeImmutable(),
106+
]);
107+
}
89108
}

0 commit comments

Comments
 (0)