Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Added custom methods proxy support for jobs ::dispatch() #34781

Merged
merged 3 commits into from Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}