File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed
src/Illuminate/Foundation/Testing/Concerns
tests/Foundation/Testing/Concerns Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 5
5
use Closure ;
6
6
use Illuminate \Foundation \Mix ;
7
7
use Illuminate \Foundation \Vite ;
8
+ use Illuminate \Support \Defer \DeferredCallbackCollection ;
8
9
use Illuminate \Support \Facades \Facade ;
9
10
use Illuminate \Support \HtmlString ;
10
11
use Mockery ;
@@ -25,6 +26,13 @@ trait InteractsWithContainer
25
26
*/
26
27
protected $ originalMix ;
27
28
29
+ /**
30
+ * The original deferred callbacks collection.
31
+ *
32
+ * @var \Illuminate\Support\Defer\DeferredCallbackCollection|null
33
+ */
34
+ protected $ originalDeferredCallbacksCollection ;
35
+
28
36
/**
29
37
* Register an instance of an object in the container.
30
38
*
@@ -234,4 +242,38 @@ protected function withMix()
234
242
235
243
return $ this ;
236
244
}
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
+ }
237
279
}
Original file line number Diff line number Diff line change 4
4
5
5
use Illuminate \Foundation \Mix ;
6
6
use Illuminate \Foundation \Vite ;
7
+ use Illuminate \Support \Defer \DeferredCallbackCollection ;
7
8
use Orchestra \Testbench \TestCase ;
8
9
use stdClass ;
9
10
@@ -73,6 +74,26 @@ public function testWithMixRestoresOriginalHandlerAndReturnsInstance()
73
74
$ this ->assertSame ($ this , $ instance );
74
75
}
75
76
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
+
76
97
public function testForgetMock ()
77
98
{
78
99
$ this ->mock (InstanceStub::class)
You can’t perform that action at this time.
0 commit comments