Skip to content

Commit 3eebbf9

Browse files
authored
Have flush_after() return the callback's return (#691)
1 parent 33d5870 commit 3eebbf9

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

docs/index.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,16 @@ once. To do this, wrap the operations in a ``flush_after()`` callback:
10031003
TagFactory::createMany(200); // instantiated/persisted but not flushed
10041004
}); // single flush
10051005

1006+
The ``flush_after()`` function forwards the callback’s return, in case you need to use the objects in your tests:
1007+
1008+
::
1009+
1010+
use function Zenstruck\Foundry\Persistence\flush_after;
1011+
1012+
[$category, $tag] = flush_after(fn() => [
1013+
CategoryFactory::createOne(),
1014+
TagFactory::createOne(),
1015+
]);
10061016

10071017
Not-persisted objects factory
10081018
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/Persistence/PersistenceManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ public function save(object $object): object
174174
}
175175

176176
/**
177-
* @param callable():void $callback
177+
* @param callable():mixed $callback
178178
*/
179-
public function flushAfter(callable $callback): void
179+
public function flushAfter(callable $callback): mixed
180180
{
181181
$this->flush = false;
182182

183-
$callback();
183+
$result = $callback();
184184

185185
$this->flush = true;
186186

@@ -189,6 +189,8 @@ public function flushAfter(callable $callback): void
189189
$this->flush($om);
190190
}
191191
}
192+
193+
return $result;
192194
}
193195

194196
public function flush(ObjectManager $om): void

src/Persistence/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ function delete(object $object): object
149149
}
150150

151151
/**
152-
* @param callable():void $callback
152+
* @param callable():mixed $callback
153153
*/
154-
function flush_after(callable $callback): void
154+
function flush_after(callable $callback): mixed
155155
{
156-
Configuration::instance()->persistence()->flushAfter($callback);
156+
return Configuration::instance()->persistence()->flushAfter($callback);
157157
}
158158

159159
/**

tests/Integration/Persistence/GenericFactoryTestCase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,20 @@ public function flush_after(): void
435435
{
436436
$this->factory()::repository()->assert()->empty();
437437

438-
flush_after(function() {
438+
$object = null;
439+
$return = flush_after(function() use (&$object) {
439440
$object = $this->factory()::createOne();
440441

441442
// ensure auto-refresh does not break when in flush_after
442443
$object->getProp1();
443444

444445
$this->factory()::repository()->assert()->empty();
446+
447+
return $object;
445448
});
446449

447450
$this->factory()::repository()->assert()->count(1);
451+
self::assertSame($object, $return);
448452
}
449453

450454
/**

0 commit comments

Comments
 (0)