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

Skip to content

Commit 30df79d

Browse files
committed
feat: auto-refresh objects from RepositoryDecorator
bot: fix cs [skip ci]
1 parent f675c37 commit 30df79d

5 files changed

Lines changed: 110 additions & 10 deletions

File tree

src/Persistence/Proxy/PersistedObjectsTracker.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ public function refresh(): void
3030
self::proxifyObjects();
3131
}
3232

33-
public function add(object $object): void
33+
public function add(object ...$objects): void
3434
{
35-
self::$buffer[] = \WeakReference::create($object);
35+
foreach ($objects as $object) {
36+
self::$buffer[] = \WeakReference::create($object);
37+
}
3638
}
3739

3840
public function reset(): void

src/Persistence/RepositoryDecorator.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public function find($id): ?object
100100
/** @var T|null $object */
101101
$object = $this->inner()->find(ProxyGenerator::unwrap($id));
102102

103+
if ($object) {
104+
Configuration::instance()->persistedObjectsTracker?->add($object);
105+
}
106+
103107
return $object;
104108
}
105109

@@ -116,7 +120,11 @@ public function findOrFail(mixed $id): object
116120
*/
117121
public function findAll(): array
118122
{
119-
return \array_values($this->inner()->findAll());
123+
$objects = \array_values($this->inner()->findAll());
124+
125+
Configuration::instance()->persistedObjectsTracker?->add(...$objects);
126+
127+
return $objects;
120128
}
121129

122130
/**
@@ -127,15 +135,25 @@ public function findAll(): array
127135
*/
128136
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
129137
{
130-
return \array_values($this->inner()->findBy($this->normalize($criteria), $orderBy, $limit, $offset));
138+
$objects = \array_values($this->inner()->findBy($this->normalize($criteria), $orderBy, $limit, $offset));
139+
140+
Configuration::instance()->persistedObjectsTracker?->add(...$objects);
141+
142+
return $objects;
131143
}
132144

133145
/**
134146
* @return T|null
135147
*/
136148
public function findOneBy(array $criteria): ?object
137149
{
138-
return $this->inner()->findOneBy($this->normalize($criteria));
150+
$object = $this->inner()->findOneBy($this->normalize($criteria));
151+
152+
if ($object) {
153+
Configuration::instance()->persistedObjectsTracker?->add($object);
154+
}
155+
156+
return $object;
139157
}
140158

141159
public function getClassName(): string

tests/Integration/Mongo/ProxyPHP84Test.php renamed to tests/Integration/Mongo/AutoRefreshTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
use Zenstruck\Foundry\Tests\Fixture\Document\GenericDocument;
1919
use Zenstruck\Foundry\Tests\Fixture\Factories\Document\GenericDocumentFactory;
2020
use Zenstruck\Foundry\Tests\Fixture\Model\GenericModel;
21-
use Zenstruck\Foundry\Tests\Integration\Persistence\ProxyPHP84TestCase;
21+
use Zenstruck\Foundry\Tests\Integration\Persistence\AutoRefreshTestCase;
2222
use Zenstruck\Foundry\Tests\Integration\RequiresMongo;
2323

24-
final class ProxyPHP84Test extends ProxyPHP84TestCase
24+
final class AutoRefreshTest extends AutoRefreshTestCase
2525
{
2626
use RequiresMongo;
2727

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory;
2323
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory;
2424
use Zenstruck\Foundry\Tests\Fixture\Model\GenericModel;
25-
use Zenstruck\Foundry\Tests\Integration\Persistence\ProxyPHP84TestCase;
25+
use Zenstruck\Foundry\Tests\Integration\Persistence\AutoRefreshTestCase;
2626
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
2727

28-
final class ProxyPHP84Test extends ProxyPHP84TestCase
28+
final class AutoRefreshTest extends AutoRefreshTestCase
2929
{
3030
use RequiresORM;
3131

tests/Integration/Persistence/ProxyPHP84TestCase.php renamed to tests/Integration/Persistence/AutoRefreshTestCase.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Zenstruck\Foundry\Tests\Integration\Persistence;
1313

14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\Attributes\Depends;
1516
use PHPUnit\Framework\Attributes\RequiresEnvironmentVariable;
1617
use PHPUnit\Framework\Attributes\RequiresPhp;
@@ -38,7 +39,7 @@
3839
*/
3940
#[RequiresPhpunit('>=12')]
4041
#[RequiresEnvironmentVariable('USE_PHP_84_LAZY_OBJECTS', '1')]
41-
abstract class ProxyPHP84TestCase extends WebTestCase
42+
abstract class AutoRefreshTestCase extends WebTestCase
4243
{
4344
use Factories, ResetDatabase;
4445

@@ -195,6 +196,85 @@ public function it_can_refresh_all_objects(): void
195196
self::assertSame('foo', $object2->getProp1());
196197
}
197198

199+
/**
200+
* @test
201+
* @requires PHP >= 8.4
202+
* @dataProvider provideRepositoryMethod
203+
*/
204+
#[Test]
205+
#[RequiresPhp('>= 8.4')]
206+
#[DataProvider('provideRepositoryMethod')]
207+
public function it_can_refresh_objects_fetched_from_repository_decorator(string $methodName, array $params): void
208+
{
209+
$this->factory()->many(2)->create();
210+
211+
$objectTracker = Configuration::instance()->persistedObjectsTracker;
212+
self::assertNotNull($objectTracker);
213+
$objectTracker->reset();
214+
215+
$objects = \call_user_func([$this->factory()::repository(), $methodName], ...$params); // @phpstan-ignore argument.type
216+
if (!\is_array($objects)) {
217+
$objects = [$objects];
218+
}
219+
self::assertGreaterThan(0, \count($objects));
220+
self::assertContainsOnlyInstancesOf(GenericModel::class, $objects);
221+
222+
self::ensureKernelShutdown();
223+
224+
foreach ($objects as $object) {
225+
$this->updateObject($object);
226+
self::assertSame('default1', $object->getProp1());
227+
}
228+
229+
refresh_all();
230+
231+
foreach ($objects as $object) {
232+
self::assertSame('foo', $object->getProp1());
233+
}
234+
}
235+
236+
public static function provideRepositoryMethod(): iterable
237+
{
238+
yield ['first', ['sortBy' => 'id']];
239+
yield ['last', ['sortBy' => 'id']];
240+
yield ['find', ['id' => ['prop1' => 'default1']]];
241+
yield ['findOneBy', ['criteria' => ['prop1' => 'default1']]];
242+
yield ['findBy', ['criteria' => ['prop1' => 'default1']]];
243+
yield ['findAll', []];
244+
}
245+
246+
/**
247+
* @test
248+
* @requires PHP >= 8.4
249+
*/
250+
#[Test]
251+
#[RequiresPhp('>= 8.4')]
252+
public function it_can_refresh_object_fetched_find_and_id(): void
253+
{
254+
$id = $this->factory()->create()->id;
255+
256+
$objectTracker = Configuration::instance()->persistedObjectsTracker;
257+
self::assertNotNull($objectTracker);
258+
$objectTracker->reset();
259+
260+
self::assertNull(
261+
$this->factory()::repository()->find(42)
262+
);
263+
264+
$object = $this->factory()::repository()->find($id);
265+
self::assertInstanceOf(GenericModel::class, $object);
266+
267+
self::ensureKernelShutdown();
268+
269+
$this->updateObject($object);
270+
271+
self::assertSame('default1', $object->getProp1());
272+
273+
refresh_all();
274+
275+
self::assertSame('foo', $object->getProp1());
276+
}
277+
198278
/**
199279
* @return PersistentObjectFactory<GenericModel>
200280
*/

0 commit comments

Comments
 (0)