Skip to content

Commit

Permalink
throw exception if failed to create queue payload (#15284)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Sep 5, 2016
1 parent 3bd2712 commit 1812e20
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Illuminate\Container\Container;

abstract class Queue
Expand Down Expand Up @@ -71,20 +72,28 @@ public function bulk($jobs, $data = '', $queue = null)
* @param mixed $data
* @param string $queue
* @return string
*
* @throws \InvalidArgumentException
*/
protected function createPayload($job, $data = '', $queue = null)
{
if (is_object($job)) {
return json_encode([
$payload = json_encode([
'job' => 'Illuminate\Queue\CallQueuedHandler@call',
'data' => [
'commandName' => get_class($job),
'command' => serialize(clone $job),
],
]);
} else {
$payload = json_encode($this->createPlainPayload($job, $data));
}

if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('Unable to create payload: '.json_last_error_msg());
}

return json_encode($this->createPlainPayload($job, $data));
return $payload;
}

/**
Expand All @@ -106,12 +115,20 @@ protected function createPlainPayload($job, $data)
* @param string $key
* @param string $value
* @return string
*
* @throws \InvalidArgumentException
*/
protected function setMeta($payload, $key, $value)
{
$payload = json_decode($payload, true);

return json_encode(Arr::set($payload, $key, $value));
$payload = json_encode(Arr::set($payload, $key, $value));

if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('Unable to create payload: '.json_last_error_msg());
}

return $payload;
}

/**
Expand Down
45 changes: 45 additions & 0 deletions tests/Queue/QueueDatabaseQueueUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,51 @@ public function testDelayedPushProperlyPushesJobOntoDatabase()
$queue->later(10, 'foo', ['data']);
}

public function testFailureToCreatePayloadFromObject()
{
$this->expectException('InvalidArgumentException');

$job = new stdClass();
$job->invalid = "\xc3\x28";

$queue = $this->getMockForAbstractClass('Illuminate\Queue\Queue');
$class = new ReflectionClass('Illuminate\Queue\Queue');

$createPayload = $class->getMethod('createPayload');
$createPayload->setAccessible(true);
$createPayload->invokeArgs($queue, [
$job,
]);
}

public function testFailureToCreatePayloadFromArray()
{
$this->expectException('InvalidArgumentException');

$queue = $this->getMockForAbstractClass('Illuminate\Queue\Queue');
$class = new ReflectionClass('Illuminate\Queue\Queue');

$createPayload = $class->getMethod('createPayload');
$createPayload->setAccessible(true);
$createPayload->invokeArgs($queue, [
["\xc3\x28"],
]);
}

public function testFailureToCreatePayloadAfterAddingMeta()
{
$this->expectException('InvalidArgumentException');

$queue = $this->getMockForAbstractClass('Illuminate\Queue\Queue');
$class = new ReflectionClass('Illuminate\Queue\Queue');

$setMeta = $class->getMethod('setMeta');
$setMeta->setAccessible(true);
$setMeta->invokeArgs($queue, [
json_encode(['valid']), 'key', "\xc3\x28",
]);
}

public function testBulkBatchPushesOntoDatabase()
{
$database = m::mock('Illuminate\Database\Connection');
Expand Down

0 comments on commit 1812e20

Please sign in to comment.