From 70607c55c5c6d87cf6792181d5d1e7f53493f9bb Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Tue, 17 Dec 2024 22:25:11 +0100 Subject: [PATCH] [11.x] Add `getJob()` method to `PendingDispatch` class + Introduced tests (#53951) * Add `getJob()` method to `PendingDispatch` class + tests * Fix styling * Update PendingDispatch.php --------- Co-authored-by: Taylor Otwell --- .../Foundation/Bus/PendingDispatch.php | 10 ++ tests/Bus/BusPendingDispatchTest.php | 117 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 tests/Bus/BusPendingDispatchTest.php diff --git a/src/Illuminate/Foundation/Bus/PendingDispatch.php b/src/Illuminate/Foundation/Bus/PendingDispatch.php index 97a339a00946..361454214d95 100644 --- a/src/Illuminate/Foundation/Bus/PendingDispatch.php +++ b/src/Illuminate/Foundation/Bus/PendingDispatch.php @@ -176,6 +176,16 @@ protected function shouldDispatch() ->acquire($this->job); } + /** + * Get the underlying job instance. + * + * @var mixed + */ + public function getJob() + { + return $this->job; + } + /** * Dynamically proxy methods to the underlying job. * diff --git a/tests/Bus/BusPendingDispatchTest.php b/tests/Bus/BusPendingDispatchTest.php new file mode 100644 index 000000000000..99c4065cb5c5 --- /dev/null +++ b/tests/Bus/BusPendingDispatchTest.php @@ -0,0 +1,117 @@ +job = m::mock(stdClass::class); + $this->pendingDispatch = new PendingDispatchWithoutDestructor($this->job); + + parent::setUp(); + } + + protected function tearDown(): void + { + parent::tearDown(); + + m::close(); + } + + public function testOnConnection() + { + $this->job->shouldReceive('onConnection')->once()->with('test-connection'); + $this->pendingDispatch->onConnection('test-connection'); + } + + public function testOnQueue() + { + $this->job->shouldReceive('onQueue')->once()->with('test-queue'); + $this->pendingDispatch->onQueue('test-queue'); + } + + public function testAllOnConnection() + { + $this->job->shouldReceive('allOnConnection')->once()->with('test-connection'); + $this->pendingDispatch->allOnConnection('test-connection'); + } + + public function testAllOnQueue() + { + $this->job->shouldReceive('allOnQueue')->once()->with('test-queue'); + $this->pendingDispatch->allOnQueue('test-queue'); + } + + public function testDelay() + { + $this->job->shouldReceive('delay')->once()->with(60); + $this->pendingDispatch->delay(60); + } + + public function testWithoutDelay() + { + $this->job->shouldReceive('withoutDelay')->once(); + $this->pendingDispatch->withoutDelay(); + } + + public function testAfterCommit() + { + $this->job->shouldReceive('afterCommit')->once(); + $this->pendingDispatch->afterCommit(); + } + + public function testBeforeCommit() + { + $this->job->shouldReceive('beforeCommit')->once(); + $this->pendingDispatch->beforeCommit(); + } + + public function testChain() + { + $chain = [new stdClass]; + $this->job->shouldReceive('chain')->once()->with($chain); + $this->pendingDispatch->chain($chain); + } + + public function testAfterResponse() + { + $this->pendingDispatch->afterResponse(); + $this->assertTrue( + (new ReflectionClass($this->pendingDispatch))->getProperty('afterResponse')->getValue($this->pendingDispatch) + ); + } + + public function testGetJob() + { + $this->assertSame($this->job, $this->pendingDispatch->getJob()); + } + + public function testDynamicallyProxyMethods() + { + $newJob = m::mock(stdClass::class); + $this->job->shouldReceive('appendToChain')->once()->with($newJob); + $this->pendingDispatch->appendToChain($newJob); + } +}