Skip to content

Commit 70607c5

Browse files
[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 <[email protected]>
1 parent fd07e20 commit 70607c5

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/Illuminate/Foundation/Bus/PendingDispatch.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ protected function shouldDispatch()
176176
->acquire($this->job);
177177
}
178178

179+
/**
180+
* Get the underlying job instance.
181+
*
182+
* @var mixed
183+
*/
184+
public function getJob()
185+
{
186+
return $this->job;
187+
}
188+
179189
/**
180190
* Dynamically proxy methods to the underlying job.
181191
*

tests/Bus/BusPendingDispatchTest.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Bus;
4+
5+
use Illuminate\Foundation\Bus\PendingDispatch;
6+
use Mockery as m;
7+
use PHPUnit\Framework\TestCase;
8+
use ReflectionClass;
9+
use stdClass;
10+
11+
class PendingDispatchWithoutDestructor extends PendingDispatch
12+
{
13+
public function __destruct()
14+
{
15+
// Prevent the job from being dispatched
16+
}
17+
}
18+
19+
class BusPendingDispatchTest extends TestCase
20+
{
21+
protected $job;
22+
23+
/**
24+
* @var PendingDispatchWithoutDestructor
25+
*/
26+
protected $pendingDispatch;
27+
28+
protected function setUp(): void
29+
{
30+
$this->job = m::mock(stdClass::class);
31+
$this->pendingDispatch = new PendingDispatchWithoutDestructor($this->job);
32+
33+
parent::setUp();
34+
}
35+
36+
protected function tearDown(): void
37+
{
38+
parent::tearDown();
39+
40+
m::close();
41+
}
42+
43+
public function testOnConnection()
44+
{
45+
$this->job->shouldReceive('onConnection')->once()->with('test-connection');
46+
$this->pendingDispatch->onConnection('test-connection');
47+
}
48+
49+
public function testOnQueue()
50+
{
51+
$this->job->shouldReceive('onQueue')->once()->with('test-queue');
52+
$this->pendingDispatch->onQueue('test-queue');
53+
}
54+
55+
public function testAllOnConnection()
56+
{
57+
$this->job->shouldReceive('allOnConnection')->once()->with('test-connection');
58+
$this->pendingDispatch->allOnConnection('test-connection');
59+
}
60+
61+
public function testAllOnQueue()
62+
{
63+
$this->job->shouldReceive('allOnQueue')->once()->with('test-queue');
64+
$this->pendingDispatch->allOnQueue('test-queue');
65+
}
66+
67+
public function testDelay()
68+
{
69+
$this->job->shouldReceive('delay')->once()->with(60);
70+
$this->pendingDispatch->delay(60);
71+
}
72+
73+
public function testWithoutDelay()
74+
{
75+
$this->job->shouldReceive('withoutDelay')->once();
76+
$this->pendingDispatch->withoutDelay();
77+
}
78+
79+
public function testAfterCommit()
80+
{
81+
$this->job->shouldReceive('afterCommit')->once();
82+
$this->pendingDispatch->afterCommit();
83+
}
84+
85+
public function testBeforeCommit()
86+
{
87+
$this->job->shouldReceive('beforeCommit')->once();
88+
$this->pendingDispatch->beforeCommit();
89+
}
90+
91+
public function testChain()
92+
{
93+
$chain = [new stdClass];
94+
$this->job->shouldReceive('chain')->once()->with($chain);
95+
$this->pendingDispatch->chain($chain);
96+
}
97+
98+
public function testAfterResponse()
99+
{
100+
$this->pendingDispatch->afterResponse();
101+
$this->assertTrue(
102+
(new ReflectionClass($this->pendingDispatch))->getProperty('afterResponse')->getValue($this->pendingDispatch)
103+
);
104+
}
105+
106+
public function testGetJob()
107+
{
108+
$this->assertSame($this->job, $this->pendingDispatch->getJob());
109+
}
110+
111+
public function testDynamicallyProxyMethods()
112+
{
113+
$newJob = m::mock(stdClass::class);
114+
$this->job->shouldReceive('appendToChain')->once()->with($newJob);
115+
$this->pendingDispatch->appendToChain($newJob);
116+
}
117+
}

0 commit comments

Comments
 (0)