Skip to content

Commit 3ae9c40

Browse files
committed
Merge branch '10.x'
2 parents d8e5ec4 + dbce9d0 commit 3ae9c40

File tree

5 files changed

+140
-2
lines changed

5 files changed

+140
-2
lines changed

src/Illuminate/Bus/PendingBatch.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,26 @@ protected function dispatchExistingBatch($batch)
344344
new BatchDispatched($batch)
345345
);
346346
}
347+
348+
/**
349+
* Dispatch the batch if the given truth test passes.
350+
*
351+
* @param bool|\Closure $boolean
352+
* @return \Illuminate\Bus\Batch|null
353+
*/
354+
public function dispatchIf($boolean)
355+
{
356+
return value($boolean) ? $this->dispatch() : null;
357+
}
358+
359+
/**
360+
* Dispatch the batch unless the given truth test passes.
361+
*
362+
* @param bool|\Closure $boolean
363+
* @return \Illuminate\Bus\Batch|null
364+
*/
365+
public function dispatchUnless($boolean)
366+
{
367+
return ! value($boolean) ? $this->dispatch() : null;
368+
}
347369
}

src/Illuminate/Log/LogManager.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,22 @@ public function sharedContext()
501501
return $this->sharedContext;
502502
}
503503

504+
/**
505+
* Flush the log context on all currently resolved channels.
506+
*
507+
* @return $this
508+
*/
509+
public function withoutContext()
510+
{
511+
foreach ($this->channels as $channel) {
512+
if (method_exists($channel, 'withoutContext')) {
513+
$channel->withoutContext();
514+
}
515+
}
516+
517+
return $this;
518+
}
519+
504520
/**
505521
* Flush the shared context.
506522
*

src/Illuminate/Queue/QueueServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ protected function registerWorker()
198198
};
199199

200200
$resetScope = function () use ($app) {
201-
if (method_exists($app['log']->driver(), 'withoutContext')) {
201+
$app['log']->flushSharedContext();
202+
203+
if (method_exists($app['log'], 'withoutContext')) {
202204
$app['log']->withoutContext();
203205
}
204206

src/Illuminate/Support/Facades/Log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @method static \Psr\Log\LoggerInterface driver(string|null $driver = null)
1010
* @method static \Illuminate\Log\LogManager shareContext(array $context)
1111
* @method static array sharedContext()
12+
* @method static \Illuminate\Log\LogManager withoutContext()
1213
* @method static \Illuminate\Log\LogManager flushSharedContext()
1314
* @method static string|null getDefaultDriver()
1415
* @method static void setDefaultDriver(string $name)
@@ -26,7 +27,6 @@
2627
* @method static void log(mixed $level, string $message, array $context = [])
2728
* @method static void write(string $level, \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message, array $context = [])
2829
* @method static \Illuminate\Log\Logger withContext(array $context = [])
29-
* @method static \Illuminate\Log\Logger withoutContext()
3030
* @method static void listen(\Closure $callback)
3131
* @method static \Psr\Log\LoggerInterface getLogger()
3232
* @method static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher()

tests/Bus/BusPendingBatchTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,102 @@ public function test_batch_is_deleted_from_storage_if_exception_thrown_during_ba
8888

8989
$pendingBatch->dispatch();
9090
}
91+
92+
public function test_batch_is_dispatched_when_dispatchif_is_true()
93+
{
94+
$container = new Container;
95+
96+
$eventDispatcher = m::mock(Dispatcher::class);
97+
$eventDispatcher->shouldReceive('dispatch')->once();
98+
$container->instance(Dispatcher::class, $eventDispatcher);
99+
100+
$job = new class
101+
{
102+
use Batchable;
103+
};
104+
105+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
106+
107+
$repository = m::mock(BatchRepository::class);
108+
$repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class));
109+
$batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class));
110+
111+
$container->instance(BatchRepository::class, $repository);
112+
113+
$result = $pendingBatch->dispatchIf(true);
114+
115+
$this->assertInstanceOf(Batch::class, $result);
116+
}
117+
118+
public function test_batch_is_not_dispatched_when_dispatchif_is_false()
119+
{
120+
$container = new Container;
121+
122+
$eventDispatcher = m::mock(Dispatcher::class);
123+
$eventDispatcher->shouldNotReceive('dispatch');
124+
$container->instance(Dispatcher::class, $eventDispatcher);
125+
126+
$job = new class
127+
{
128+
use Batchable;
129+
};
130+
131+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
132+
133+
$repository = m::mock(BatchRepository::class);
134+
$container->instance(BatchRepository::class, $repository);
135+
136+
$result = $pendingBatch->dispatchIf(false);
137+
138+
$this->assertNull($result);
139+
}
140+
141+
public function test_batch_is_dispatched_when_dispatchunless_is_false()
142+
{
143+
$container = new Container;
144+
145+
$eventDispatcher = m::mock(Dispatcher::class);
146+
$eventDispatcher->shouldReceive('dispatch')->once();
147+
$container->instance(Dispatcher::class, $eventDispatcher);
148+
149+
$job = new class
150+
{
151+
use Batchable;
152+
};
153+
154+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
155+
156+
$repository = m::mock(BatchRepository::class);
157+
$repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class));
158+
$batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class));
159+
160+
$container->instance(BatchRepository::class, $repository);
161+
162+
$result = $pendingBatch->dispatchUnless(false);
163+
164+
$this->assertInstanceOf(Batch::class, $result);
165+
}
166+
167+
public function test_batch_is_not_dispatched_when_dispatchunless_is_true()
168+
{
169+
$container = new Container;
170+
171+
$eventDispatcher = m::mock(Dispatcher::class);
172+
$eventDispatcher->shouldNotReceive('dispatch');
173+
$container->instance(Dispatcher::class, $eventDispatcher);
174+
175+
$job = new class
176+
{
177+
use Batchable;
178+
};
179+
180+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
181+
182+
$repository = m::mock(BatchRepository::class);
183+
$container->instance(BatchRepository::class, $repository);
184+
185+
$result = $pendingBatch->dispatchUnless(true);
186+
187+
$this->assertNull($result);
188+
}
91189
}

0 commit comments

Comments
 (0)