Skip to content

Commit

Permalink
[8.x] Clean up custom Queue payload between tests (#36271)
Browse files Browse the repository at this point in the history
* Clean up custom Queue payload between tests

* StyleCI

* Method accepts null

* Follow DatabaseServiceProvider where register() method clear booted (#2)

models.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
deleugpn and crynobone authored Feb 15, 2021
1 parent 4042985 commit ac219bf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ protected function createStringPayload($job, $queue, $data)
/**
* Register a callback to be executed when creating job payloads.
*
* @param callable $callback
* @param callable|null $callback
* @return void
*/
public static function createPayloadUsing($callback)
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Queue/QueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class QueueServiceProvider extends ServiceProvider implements DeferrableProvider
*/
public function register()
{
Queue::createPayloadUsing(null);

$this->registerManager();
$this->registerConnection();
$this->registerWorker();
Expand Down
61 changes: 61 additions & 0 deletions tests/Integration/Queue/CustomPayloadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Illuminate\Tests\Integration\Queue;

use Illuminate\Contracts\Bus\QueueingDispatcher;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Queue;
use Illuminate\Support\ServiceProvider;
use Orchestra\Testbench\TestCase;

class CustomPayloadTest extends TestCase
{
protected function getPackageProviders($app)
{
return [QueueServiceProvider::class];
}

public function websites()
{
yield ['laravel.com'];

yield ['blog.laravel.com'];
}

/**
* @dataProvider websites
*/
public function test_custom_payload_gets_cleared_for_each_data_provider(string $websites)
{
$dispatcher = $this->app->make(QueueingDispatcher::class);

$dispatcher->dispatchToQueue(new MyJob);
}
}

class QueueServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('one.time.password', function () {
return random_int(1, 10);
});

Queue::createPayloadUsing(function () {
$password = $this->app->make('one.time.password');

$this->app->offsetUnset('one.time.password');

return ['password' => $password];
});
}
}

class MyJob implements ShouldQueue
{
public $connection = 'sync';

public function handle()
{
}
}

0 comments on commit ac219bf

Please sign in to comment.