diff --git a/src/Illuminate/Queue/CallQueuedClosure.php b/src/Illuminate/Queue/CallQueuedClosure.php index 732600ccfea1..34bdf3b85796 100644 --- a/src/Illuminate/Queue/CallQueuedClosure.php +++ b/src/Illuminate/Queue/CallQueuedClosure.php @@ -22,6 +22,13 @@ class CallQueuedClosure implements ShouldQueue */ public $closure; + /** + * The name assigned to the job. + * + * @var string|null + */ + public $name = null; + /** * The callbacks that should be executed on failure. * @@ -105,6 +112,21 @@ public function displayName() { $reflection = new ReflectionFunction($this->closure->getClosure()); - return 'Closure ('.basename($reflection->getFileName()).':'.$reflection->getStartLine().')'; + $prefix = is_null($this->name) ? '' : "{$this->name} - "; + + return $prefix.'Closure ('.basename($reflection->getFileName()).':'.$reflection->getStartLine().')'; + } + + /** + * Assign a name to the job. + * + * @param string $name + * @return $this + */ + public function name($name) + { + $this->name = $name; + + return $this; } } diff --git a/tests/Integration/Queue/JobDispatchingTest.php b/tests/Integration/Queue/JobDispatchingTest.php index eddfd83c23c1..441cb59dea97 100644 --- a/tests/Integration/Queue/JobDispatchingTest.php +++ b/tests/Integration/Queue/JobDispatchingTest.php @@ -166,6 +166,24 @@ public function testQueueMayBeNullForJobQueueingAndJobQueuedEvent() $this->assertNull($events[3]->queue); } + public function testQueuedClosureCanBeNamed() + { + Config::set('queue.default', 'database'); + $events = []; + $this->app['events']->listen(function (JobQueued $e) use (&$events) { + $events[] = $e; + }); + + dispatch(function () { + // + })->name('custom name'); + + $this->assertCount(1, $events); + $this->assertInstanceOf(JobQueued::class, $events[0]); + $this->assertSame('custom name', $events[0]->job->name); + $this->assertStringContainsString('custom name', $events[0]->job->displayName()); + } + public function testCanDisableDispatchingAfterResponse() { Job::dispatchAfterResponse('test');