From 158c43c683138fb91f310d5f19e59b5843b747be Mon Sep 17 00:00:00 2001 From: rennokki Date: Mon, 12 Oct 2020 17:27:48 +0300 Subject: [PATCH] [8.x] Added custom methods proxy support for jobs ::dispatch() (#34781) * Added custom methods proxy * Fixed return * Update PendingDispatch.php Co-authored-by: Taylor Otwell --- .../Foundation/Bus/PendingDispatch.php | 14 +++++ .../Integration/Queue/JobDispatchingTest.php | 57 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 tests/Integration/Queue/JobDispatchingTest.php diff --git a/src/Illuminate/Foundation/Bus/PendingDispatch.php b/src/Illuminate/Foundation/Bus/PendingDispatch.php index 89329515ecd2..74e37b269522 100644 --- a/src/Illuminate/Foundation/Bus/PendingDispatch.php +++ b/src/Illuminate/Foundation/Bus/PendingDispatch.php @@ -121,6 +121,20 @@ public function afterResponse() return $this; } + /** + * Dynamically proxy methods to the underlying job. + * + * @param string $method + * @param array $parameters + * @return $this + */ + public function __call($method, $parameters) + { + $this->job->{$method}(...$parameters); + + return $this; + } + /** * Handle the object's destruction. * diff --git a/tests/Integration/Queue/JobDispatchingTest.php b/tests/Integration/Queue/JobDispatchingTest.php new file mode 100644 index 000000000000..4391c50b54d8 --- /dev/null +++ b/tests/Integration/Queue/JobDispatchingTest.php @@ -0,0 +1,57 @@ +set('app.debug', 'true'); + } + + protected function tearDown(): void + { + Job::$ran = false; + } + + public function testJobCanUseCustomMethodsAfterDispatch() + { + Job::dispatch('test')->replaceValue('new-test'); + + $this->assertTrue(Job::$ran); + $this->assertEquals('new-test', Job::$value); + } +} + +class Job implements ShouldQueue +{ + use Dispatchable, Queueable; + + public static $ran = false; + public static $usedQueue = null; + public static $usedConnection = null; + public static $value = null; + + public function __construct($value) + { + static::$value = $value; + } + + public function handle() + { + static::$ran = true; + } + + public function replaceValue($value) + { + static::$value = $value; + } +}