Skip to content

Commit

Permalink
Merge branch '10.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jan 12, 2024
2 parents d8e5ec4 + dbce9d0 commit 3ae9c40
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/Illuminate/Bus/PendingBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,26 @@ protected function dispatchExistingBatch($batch)
new BatchDispatched($batch)
);
}

/**
* Dispatch the batch if the given truth test passes.
*
* @param bool|\Closure $boolean
* @return \Illuminate\Bus\Batch|null
*/
public function dispatchIf($boolean)
{
return value($boolean) ? $this->dispatch() : null;
}

/**
* Dispatch the batch unless the given truth test passes.
*
* @param bool|\Closure $boolean
* @return \Illuminate\Bus\Batch|null
*/
public function dispatchUnless($boolean)
{
return ! value($boolean) ? $this->dispatch() : null;
}
}
16 changes: 16 additions & 0 deletions src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,22 @@ public function sharedContext()
return $this->sharedContext;
}

/**
* Flush the log context on all currently resolved channels.
*
* @return $this
*/
public function withoutContext()
{
foreach ($this->channels as $channel) {
if (method_exists($channel, 'withoutContext')) {
$channel->withoutContext();
}
}

return $this;
}

/**
* Flush the shared context.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Queue/QueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ protected function registerWorker()
};

$resetScope = function () use ($app) {
if (method_exists($app['log']->driver(), 'withoutContext')) {
$app['log']->flushSharedContext();

if (method_exists($app['log'], 'withoutContext')) {
$app['log']->withoutContext();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @method static \Psr\Log\LoggerInterface driver(string|null $driver = null)
* @method static \Illuminate\Log\LogManager shareContext(array $context)
* @method static array sharedContext()
* @method static \Illuminate\Log\LogManager withoutContext()
* @method static \Illuminate\Log\LogManager flushSharedContext()
* @method static string|null getDefaultDriver()
* @method static void setDefaultDriver(string $name)
Expand All @@ -26,7 +27,6 @@
* @method static void log(mixed $level, string $message, array $context = [])
* @method static void write(string $level, \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message, array $context = [])
* @method static \Illuminate\Log\Logger withContext(array $context = [])
* @method static \Illuminate\Log\Logger withoutContext()
* @method static void listen(\Closure $callback)
* @method static \Psr\Log\LoggerInterface getLogger()
* @method static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher()
Expand Down
98 changes: 98 additions & 0 deletions tests/Bus/BusPendingBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,102 @@ public function test_batch_is_deleted_from_storage_if_exception_thrown_during_ba

$pendingBatch->dispatch();
}

public function test_batch_is_dispatched_when_dispatchif_is_true()
{
$container = new Container;

$eventDispatcher = m::mock(Dispatcher::class);
$eventDispatcher->shouldReceive('dispatch')->once();
$container->instance(Dispatcher::class, $eventDispatcher);

$job = new class
{
use Batchable;
};

$pendingBatch = new PendingBatch($container, new Collection([$job]));

$repository = m::mock(BatchRepository::class);
$repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class));
$batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class));

$container->instance(BatchRepository::class, $repository);

$result = $pendingBatch->dispatchIf(true);

$this->assertInstanceOf(Batch::class, $result);
}

public function test_batch_is_not_dispatched_when_dispatchif_is_false()
{
$container = new Container;

$eventDispatcher = m::mock(Dispatcher::class);
$eventDispatcher->shouldNotReceive('dispatch');
$container->instance(Dispatcher::class, $eventDispatcher);

$job = new class
{
use Batchable;
};

$pendingBatch = new PendingBatch($container, new Collection([$job]));

$repository = m::mock(BatchRepository::class);
$container->instance(BatchRepository::class, $repository);

$result = $pendingBatch->dispatchIf(false);

$this->assertNull($result);
}

public function test_batch_is_dispatched_when_dispatchunless_is_false()
{
$container = new Container;

$eventDispatcher = m::mock(Dispatcher::class);
$eventDispatcher->shouldReceive('dispatch')->once();
$container->instance(Dispatcher::class, $eventDispatcher);

$job = new class
{
use Batchable;
};

$pendingBatch = new PendingBatch($container, new Collection([$job]));

$repository = m::mock(BatchRepository::class);
$repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class));
$batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class));

$container->instance(BatchRepository::class, $repository);

$result = $pendingBatch->dispatchUnless(false);

$this->assertInstanceOf(Batch::class, $result);
}

public function test_batch_is_not_dispatched_when_dispatchunless_is_true()
{
$container = new Container;

$eventDispatcher = m::mock(Dispatcher::class);
$eventDispatcher->shouldNotReceive('dispatch');
$container->instance(Dispatcher::class, $eventDispatcher);

$job = new class
{
use Batchable;
};

$pendingBatch = new PendingBatch($container, new Collection([$job]));

$repository = m::mock(BatchRepository::class);
$container->instance(BatchRepository::class, $repository);

$result = $pendingBatch->dispatchUnless(true);

$this->assertNull($result);
}
}

0 comments on commit 3ae9c40

Please sign in to comment.