1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
<?php
namespace Illuminate\Tests\Integration\Queue;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Dispatcher;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\CallQueuedHandler;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\SkipIfBatchCancelled;
use Mockery as m;
use Orchestra\Testbench\TestCase;
class SkipIfBatchCancelledTest extends TestCase
{
public function testJobsAreSkippedOnceBatchIsCancelled()
{
[$beforeCancelled] = (new SkipCancelledBatchableTestJob())->withFakeBatch();
[$afterCancelled] = (new SkipCancelledBatchableTestJob())->withFakeBatch(
cancelledAt: \Carbon\CarbonImmutable::now()
);
$this->assertJobRanSuccessfully($beforeCancelled);
$this->assertJobWasSkipped($afterCancelled);
}
protected function assertJobRanSuccessfully($class)
{
$this->assertJobHandled($class, true);
}
protected function assertJobWasSkipped($class)
{
$this->assertJobHandled($class, false);
}
protected function assertJobHandled($class, $expectedHandledValue)
{
$class::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
$job = m::mock(Job::class);
$job->shouldReceive('uuid')->once()->andReturn('simple-test-uuid');
$job->shouldReceive('hasFailed')->once()->andReturn(false);
$job->shouldReceive('isReleased')->andReturn(false);
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(false);
$job->shouldReceive('delete')->once();
$instance->call($job, [
'command' => serialize($command = $class),
]);
$this->assertEquals($expectedHandledValue, $class::$handled);
}
}
class SkipCancelledBatchableTestJob
{
use Batchable, InteractsWithQueue, Queueable;
public static $handled = false;
public function handle()
{
static::$handled = true;
}
public function middleware()
{
return [new SkipIfBatchCancelled];
}
}
|