Skip to content

Commit fa1f4ca

Browse files
authored
Add withoutDefer and withDefer testing helpers (#53340)
1 parent 17acf83 commit fa1f4ca

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Foundation\Mix;
77
use Illuminate\Foundation\Vite;
8+
use Illuminate\Support\Defer\DeferredCallbackCollection;
89
use Illuminate\Support\Facades\Facade;
910
use Illuminate\Support\HtmlString;
1011
use Mockery;
@@ -25,6 +26,13 @@ trait InteractsWithContainer
2526
*/
2627
protected $originalMix;
2728

29+
/**
30+
* The original deferred callbacks collection.
31+
*
32+
* @var \Illuminate\Support\Defer\DeferredCallbackCollection|null
33+
*/
34+
protected $originalDeferredCallbacksCollection;
35+
2836
/**
2937
* Register an instance of an object in the container.
3038
*
@@ -234,4 +242,38 @@ protected function withMix()
234242

235243
return $this;
236244
}
245+
246+
/**
247+
* Execute deferred functions immediately.
248+
*
249+
* @return $this
250+
*/
251+
protected function withoutDefer()
252+
{
253+
if ($this->originalDeferredCallbacksCollection == null) {
254+
$this->originalDeferredCallbacksCollection = $this->app->make(DeferredCallbackCollection::class);
255+
}
256+
257+
$this->swap(DeferredCallbackCollection::class, new class extends DeferredCallbackCollection
258+
{
259+
public function offsetSet(mixed $offset, mixed $value): void
260+
{
261+
$value();
262+
}
263+
});
264+
}
265+
266+
/**
267+
* Restore deferred functions.
268+
*
269+
* @return $this
270+
*/
271+
protected function withDefer()
272+
{
273+
if ($this->originalDeferredCallbacksCollection) {
274+
$this->app->instance(DeferredCallbackCollection::class, $this->originalDeferredCallbacksCollection);
275+
}
276+
277+
return $this;
278+
}
237279
}

tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Foundation\Mix;
66
use Illuminate\Foundation\Vite;
7+
use Illuminate\Support\Defer\DeferredCallbackCollection;
78
use Orchestra\Testbench\TestCase;
89
use stdClass;
910

@@ -73,6 +74,26 @@ public function testWithMixRestoresOriginalHandlerAndReturnsInstance()
7374
$this->assertSame($this, $instance);
7475
}
7576

77+
public function testWithoutDefer()
78+
{
79+
$called = [];
80+
defer(function () use (&$called) {
81+
$called[] = 1;
82+
});
83+
$this->assertSame([], $called);
84+
85+
$this->withoutDefer();
86+
defer(function () use (&$called) {
87+
$called[] = 2;
88+
});
89+
$this->assertSame([2], $called);
90+
91+
$this->withDefer();
92+
$this->assertSame([2], $called);
93+
$this->app[DeferredCallbackCollection::class]->invoke();
94+
$this->assertSame([2, 1], $called);
95+
}
96+
7697
public function testForgetMock()
7798
{
7899
$this->mock(InstanceStub::class)

0 commit comments

Comments
 (0)