Skip to content

Commit

Permalink
[11.x] Add getJob() method to PendingDispatch class + Introduced …
Browse files Browse the repository at this point in the history
…tests (#53951)

* Add `getJob()` method to `PendingDispatch` class + tests

* Fix styling

* Update PendingDispatch.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
pascalbaljet and taylorotwell authored Dec 17, 2024
1 parent fd07e20 commit 70607c5
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Foundation/Bus/PendingDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
117 changes: 117 additions & 0 deletions tests/Bus/BusPendingDispatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Illuminate\Tests\Bus;

use Illuminate\Foundation\Bus\PendingDispatch;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;

class PendingDispatchWithoutDestructor extends PendingDispatch
{
public function __destruct()
{
// Prevent the job from being dispatched
}
}

class BusPendingDispatchTest extends TestCase
{
protected $job;

/**
* @var PendingDispatchWithoutDestructor
*/
protected $pendingDispatch;

protected function setUp(): void
{
$this->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);
}
}

0 comments on commit 70607c5

Please sign in to comment.