From 5662c78d4fc29968f22992dc81d2ac2c6a623ab1 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 10 Oct 2020 12:47:16 +0300 Subject: [PATCH 1/3] Added custom methods proxy --- .../Foundation/Bus/PendingDispatch.php | 12 ++++ .../Integration/Queue/JobDispatchingTest.php | 57 +++++++++++++++++++ 2 files changed, 69 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..8ba873bfe397 100644 --- a/src/Illuminate/Foundation/Bus/PendingDispatch.php +++ b/src/Illuminate/Foundation/Bus/PendingDispatch.php @@ -121,6 +121,18 @@ public function afterResponse() return $this; } + /** + * Call methods within the job. + * + * @param string $method + * @param array $parameters + * @return void + */ + public function __call($method, $parameters) + { + $this->job->{$method}(...$parameters); + } + /** * 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; + } +} From 67373b20ed39596a65c654348f179119bf3d539d Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 10 Oct 2020 14:48:39 +0300 Subject: [PATCH 2/3] Fixed return --- src/Illuminate/Foundation/Bus/PendingDispatch.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Bus/PendingDispatch.php b/src/Illuminate/Foundation/Bus/PendingDispatch.php index 8ba873bfe397..b510506ae2fd 100644 --- a/src/Illuminate/Foundation/Bus/PendingDispatch.php +++ b/src/Illuminate/Foundation/Bus/PendingDispatch.php @@ -126,11 +126,13 @@ public function afterResponse() * * @param string $method * @param array $parameters - * @return void + * @return $this */ public function __call($method, $parameters) { $this->job->{$method}(...$parameters); + + return $this; } /** From 8796d4290884a38ca556f381c1d8206314ce51e4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 12 Oct 2020 09:27:39 -0500 Subject: [PATCH 3/3] Update PendingDispatch.php --- src/Illuminate/Foundation/Bus/PendingDispatch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Bus/PendingDispatch.php b/src/Illuminate/Foundation/Bus/PendingDispatch.php index b510506ae2fd..74e37b269522 100644 --- a/src/Illuminate/Foundation/Bus/PendingDispatch.php +++ b/src/Illuminate/Foundation/Bus/PendingDispatch.php @@ -122,7 +122,7 @@ public function afterResponse() } /** - * Call methods within the job. + * Dynamically proxy methods to the underlying job. * * @param string $method * @param array $parameters