Skip to content

Commit

Permalink
Merge branch '5.7' of https://github.com/tjmartin69/framework into tj…
Browse files Browse the repository at this point in the history
…martin69-5.7
  • Loading branch information
taylorotwell committed Oct 25, 2018
2 parents 005b44d + 1d23911 commit e91ef69
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ abstract class Queue
/**
* The create payload callback.
*
* @var callable|null
* @var callable[]
*/
protected static $createPayloadCallback;
protected static $createPayloadCallback = [];

/**
* Push a new job onto the queue.
Expand Down Expand Up @@ -197,7 +197,11 @@ protected function createStringPayload($job, $queue, $data)
*/
public static function createPayloadUsing($callback)
{
static::$createPayloadCallback = $callback;
if (is_null($callback)) {
static::$createPayloadCallback = [];
} else {
static::$createPayloadCallback[] = $callback;
}
}

/**
Expand All @@ -209,10 +213,12 @@ public static function createPayloadUsing($callback)
*/
protected function withCreatePayloadHooks($queue, array $payload)
{
if (static::$createPayloadCallback) {
return array_merge($payload, call_user_func(
static::$createPayloadCallback, $this->getConnectionName(), $queue, $payload
));
if (! empty(static::$createPayloadCallback)) {
foreach (static::$createPayloadCallback as $callback) {
$payload = array_merge($payload, call_user_func(
$callback, $this->getConnectionName(), $queue, $payload
));
}
}

return $payload;
Expand Down
21 changes: 21 additions & 0 deletions tests/Queue/QueueRedisQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ public function testPushProperlyPushesJobOntoRedisWithCustomPayloadHook()
Queue::createPayloadUsing(null);
}

public function testPushProperlyPushesJobOntoRedisWithTwoCustomPayloadHook()
{
$queue = $this->getMockBuilder(RedisQueue::class)->setMethods(['getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
$queue->expects($this->once())->method('getRandomId')->will($this->returnValue('foo'));
$redis->shouldReceive('connection')->once()->andReturn($redis);
$redis->shouldReceive('rpush')->once()->with('queues:default', json_encode(['displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'timeout' => null, 'data' => ['data'], 'custom' => 'taylor', 'bar' => 'foo', 'id' => 'foo', 'attempts' => 0]));

Queue::createPayloadUsing(function ($connection, $queue, $payload) {
return ['custom' => 'taylor'];
});

Queue::createPayloadUsing(function ($connection, $queue, $payload) {
return ['bar' => 'foo'];
});

$id = $queue->push('foo', ['data']);
$this->assertEquals('foo', $id);

Queue::createPayloadUsing(null);
}

public function testDelayedPushProperlyPushesJobOntoRedis()
{
$queue = $this->getMockBuilder(RedisQueue::class)->setMethods(['availableAt', 'getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
Expand Down

0 comments on commit e91ef69

Please sign in to comment.