Skip to content
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
91 changes: 91 additions & 0 deletions src/Illuminate/Queue/InteractsWithQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use DateTimeInterface;
use Illuminate\Contracts\Queue\Job as JobContract;
use Illuminate\Queue\Jobs\FakeJob;
use Illuminate\Support\InteractsWithTime;
use InvalidArgumentException;
use PHPUnit\Framework\Assert as PHPUnit;
use RuntimeException;
use Throwable;

trait InteractsWithQueue
Expand Down Expand Up @@ -79,6 +82,94 @@ public function release($delay = 0)
}
}

/**
* Indicate that queue interactions like fail, delete, and release should be faked.
*
* @return $this
*/
public function withFakeQueueInteractions()
{
$this->job = new FakeJob;

return $this;
}

/**
* Assert that the job was deleted from the queue.
*
* @return $this
*/
public function assertDeleted()
{
$this->ensureQueueInteractionsHaveBeenFaked();

PHPUnit::assertTrue(
$this->job->isDeleted(),
'Job was expected to be deleted, but was not.'
);

return $this;
}

/**
* Assert that the job was manually failed.
*
* @return $this
*/
public function assertFailed()
{
$this->ensureQueueInteractionsHaveBeenFaked();

PHPUnit::assertTrue(
$this->job->hasFailed(),
'Job was expected to be manually failed, but was not.'
);

return $this;
}

/**
* Assert that the job was released back onto the queue.
*
* @param \DateTimeInterface|\DateInterval|int $delay
* @return $this
*/
public function assertReleased($delay = null)
{
$this->ensureQueueInteractionsHaveBeenFaked();

$delay = $delay instanceof DateTimeInterface
? $this->secondsUntil($delay)
: $delay;

PHPUnit::assertTrue(
$this->job->isReleased(),
'Job was expected to be released, but was not.'
);

if (! is_null($delay)) {
PHPUnit::assertSame(
$delay,
$this->job->releaseDelay,
"Expected job to be released with delay of [{$delay}] seconds, but was released with delay of [{$this->job->releaseDelay}] seconds."
);
}

return $this;
}

/**
* Ensure that queue interactions have been faked.
*
* @return void
*/
private function ensureQueueInteractionsHaveBeenFaked()
{
if (! $this->job instanceof FakeJob) {
throw new RuntimeException('Queue interactions have not been faked.');
}
}

/**
* Set the base queue job instance.
*
Expand Down
76 changes: 76 additions & 0 deletions src/Illuminate/Queue/Jobs/FakeJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Illuminate\Queue\Jobs;

use Illuminate\Support\Str;

class FakeJob extends Job
{
/**
* The number of seconds the released job was delayed.
*
* @var int
*/
public $releaseDelay;

/**
* The exception the job failed with.
*
* @var \Throwable
*/
public $failedWith;

/**
* Get the job identifier.
*
* @return string
*/
public function getJobId()
{
return once(fn () => (string) Str::uuid());
}

/**
* Get the raw body of the job.
*
* @return string
*/
public function getRawBody()
{
return '';
}

/**
* Release the job back into the queue after (n) seconds.
*
* @param \DateTimeInterface|\DateInterval|int $delay
* @return void
*/
public function release($delay = 0)
{
$this->released = true;
$this->releaseDelay = $delay;
}

/**
* Delete the job from the queue.
*
* @return void
*/
public function delete()
{
$this->deleted = true;
}

/**
* Delete the job, call the "failed" method, and raise the failed job event.
*
* @param \Throwable|null $e
* @return void
*/
public function fail($exception = null)
{
$this->failed = true;
$this->failedWith = $exception;
}
}