Skip to content

Commit

Permalink
[8.x] Added custom methods proxy support for jobs ::dispatch() (#34781)
Browse files Browse the repository at this point in the history
* Added custom methods proxy

* Fixed return

* Update PendingDispatch.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
rennokki and taylorotwell committed Oct 12, 2020
1 parent 9c04734 commit 158c43c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Bus/PendingDispatch.php
Expand Up @@ -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.
*
Expand Down
57 changes: 57 additions & 0 deletions tests/Integration/Queue/JobDispatchingTest.php
@@ -0,0 +1,57 @@
<?php

namespace Illuminate\Tests\Integration\Queue;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Orchestra\Testbench\TestCase;

/**
* @group integration
*/
class JobDispatchingTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->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;
}
}

0 comments on commit 158c43c

Please sign in to comment.