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

[5.3] throw exception if queue failed to create payload #15284

Merged
merged 1 commit into from
Sep 5, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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