2017-08-13 19:19:17 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Illuminate\Tests\Support;
|
|
|
|
|
|
2019-04-10 10:35:51 -03:00
|
|
|
use BadMethodCallException;
|
2018-03-13 16:45:17 -05:00
|
|
|
use Illuminate\Bus\Queueable;
|
2017-08-13 19:19:17 -03:00
|
|
|
use Illuminate\Foundation\Application;
|
2024-06-17 15:57:54 +02:00
|
|
|
use Illuminate\Queue\CallQueuedClosure;
|
2022-03-10 13:00:51 -06:00
|
|
|
use Illuminate\Queue\QueueManager;
|
2017-08-13 19:19:17 -03:00
|
|
|
use Illuminate\Support\Testing\Fakes\QueueFake;
|
2022-03-10 13:00:51 -06:00
|
|
|
use Mockery as m;
|
2019-09-10 17:16:05 +02:00
|
|
|
use PHPUnit\Framework\ExpectationFailedException;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-08-13 19:19:17 -03:00
|
|
|
|
2018-03-29 00:14:44 -03:00
|
|
|
class SupportTestingQueueFakeTest extends TestCase
|
2017-08-13 19:19:17 -03:00
|
|
|
{
|
2018-10-04 10:27:50 -03:00
|
|
|
/**
|
2020-03-28 16:25:38 +01:00
|
|
|
* @var \Illuminate\Support\Testing\Fakes\QueueFake
|
2018-10-04 10:27:50 -03:00
|
|
|
*/
|
|
|
|
|
private $fake;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-28 16:25:38 +01:00
|
|
|
* @var \Illuminate\Tests\Support\JobStub
|
2018-10-04 10:27:50 -03:00
|
|
|
*/
|
|
|
|
|
private $job;
|
|
|
|
|
|
2019-02-07 16:07:08 +01:00
|
|
|
protected function setUp(): void
|
2017-08-13 19:19:17 -03:00
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$this->fake = new QueueFake(new Application);
|
|
|
|
|
$this->job = new JobStub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertPushed()
|
|
|
|
|
{
|
2018-01-31 10:51:40 -03:00
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushed(JobStub::class);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.', $e->getMessage());
|
2018-01-31 10:51:40 -03:00
|
|
|
}
|
2017-08-13 19:19:17 -03:00
|
|
|
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushed(JobStub::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 01:00:05 +10:00
|
|
|
public function testItCanAssertAgainstDataWithPush()
|
|
|
|
|
{
|
|
|
|
|
$data = null;
|
|
|
|
|
$this->fake->push(JobStub::class, ['foo' => 'bar'], 'redis');
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushed(JobStub::class, function ($job, $queue, $jobData) use (&$data) {
|
|
|
|
|
$data = $jobData;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->assertSame(['foo' => 'bar'], $data);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 13:00:51 -06:00
|
|
|
public function testAssertPushedWithIgnore()
|
|
|
|
|
{
|
|
|
|
|
$job = new JobStub;
|
|
|
|
|
|
|
|
|
|
$manager = m::mock(QueueManager::class);
|
|
|
|
|
$manager->shouldReceive('push')->once()->withArgs(function ($passedJob) use ($job) {
|
|
|
|
|
return $passedJob === $job;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$fake = new QueueFake(new Application, JobToFakeStub::class, $manager);
|
|
|
|
|
|
|
|
|
|
$fake->push($job);
|
|
|
|
|
$fake->push(new JobToFakeStub());
|
|
|
|
|
|
|
|
|
|
$fake->assertNotPushed(JobStub::class);
|
|
|
|
|
$fake->assertPushed(JobToFakeStub::class);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 13:27:33 +02:00
|
|
|
public function testAssertPushedWithClosure()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushed(function (JobStub $job) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 11:20:58 -04:00
|
|
|
public function testQueueSize()
|
|
|
|
|
{
|
|
|
|
|
$this->assertEquals(0, $this->fake->size());
|
|
|
|
|
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(1, $this->fake->size());
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-13 19:19:17 -03:00
|
|
|
public function testAssertNotPushed()
|
2020-05-01 13:27:33 +02:00
|
|
|
{
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->fake->assertNotPushed(JobStub::class);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The unexpected [Illuminate\Tests\Support\JobStub] job was pushed.', $e->getMessage());
|
2020-05-01 13:27:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertNotPushedWithClosure()
|
2017-08-13 19:19:17 -03:00
|
|
|
{
|
|
|
|
|
$this->fake->assertNotPushed(JobStub::class);
|
|
|
|
|
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
2018-01-31 10:51:40 -03:00
|
|
|
try {
|
2020-05-01 13:27:33 +02:00
|
|
|
$this->fake->assertNotPushed(function (JobStub $job) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2018-01-31 10:51:40 -03:00
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The unexpected [Illuminate\Tests\Support\JobStub] job was pushed.', $e->getMessage());
|
2018-01-31 10:51:40 -03:00
|
|
|
}
|
2017-08-13 19:19:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertPushedOn()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push($this->job, '', 'foo');
|
|
|
|
|
|
2018-01-31 10:51:40 -03:00
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushedOn('bar', JobStub::class);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.', $e->getMessage());
|
2018-01-31 10:51:40 -03:00
|
|
|
}
|
2017-08-13 19:19:17 -03:00
|
|
|
|
|
|
|
|
$this->fake->assertPushedOn('foo', JobStub::class);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 13:27:33 +02:00
|
|
|
public function testAssertPushedOnWithClosure()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push($this->job, '', 'foo');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushedOn('bar', function (JobStub $job) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.', $e->getMessage());
|
2020-05-01 13:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushedOn('foo', function (JobStub $job) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-13 19:19:17 -03:00
|
|
|
public function testAssertPushedTimes()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
2018-01-31 10:51:40 -03:00
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushed(JobStub::class, 1);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2025-08-05 12:28:53 +10:00
|
|
|
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\JobStub] job was pushed 2 times instead of 1 time.', $e->getMessage());
|
2018-01-31 10:51:40 -03:00
|
|
|
}
|
2017-08-13 19:19:17 -03:00
|
|
|
|
|
|
|
|
$this->fake->assertPushed(JobStub::class, 2);
|
|
|
|
|
}
|
2017-08-21 01:33:22 -03:00
|
|
|
|
2024-01-07 19:25:16 +04:00
|
|
|
public function testAssertCount()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push(function () {
|
|
|
|
|
// Do nothing
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
|
|
|
|
$this->fake->assertCount(3);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 01:33:22 -03:00
|
|
|
public function testAssertNothingPushed()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->assertNothingPushed();
|
|
|
|
|
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
2024-06-17 15:57:54 +02:00
|
|
|
$this->fake->push(function () {
|
|
|
|
|
//
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-31 10:51:40 -03:00
|
|
|
try {
|
|
|
|
|
$this->fake->assertNothingPushed();
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2024-06-17 15:57:54 +02:00
|
|
|
$this->assertStringContainsString('The following jobs were pushed unexpectedly', $e->getMessage());
|
|
|
|
|
$this->assertStringContainsString(get_class($this->job), $e->getMessage());
|
|
|
|
|
$this->assertStringContainsString(CallQueuedClosure::class, $e->getMessage());
|
2018-01-31 10:51:40 -03:00
|
|
|
}
|
2017-08-21 01:33:22 -03:00
|
|
|
}
|
2018-02-26 08:55:50 -05:00
|
|
|
|
|
|
|
|
public function testAssertPushedUsingBulk()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->assertNothingPushed();
|
|
|
|
|
|
|
|
|
|
$queue = 'my-test-queue';
|
|
|
|
|
$this->fake->bulk([
|
|
|
|
|
$this->job,
|
2018-10-10 22:24:53 +02:00
|
|
|
new JobStub,
|
2018-02-26 08:55:50 -05:00
|
|
|
], null, $queue);
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushedOn($queue, JobStub::class);
|
|
|
|
|
$this->fake->assertPushed(JobStub::class, 2);
|
|
|
|
|
}
|
2018-03-13 16:45:17 -05:00
|
|
|
|
|
|
|
|
public function testAssertPushedWithChainUsingClassesOrObjectsArray()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push(new JobWithChainStub([
|
2018-03-14 07:56:14 -05:00
|
|
|
new JobStub,
|
2018-03-13 16:45:17 -05:00
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainStub::class, [
|
2018-03-14 07:56:14 -05:00
|
|
|
JobStub::class,
|
2018-03-13 16:45:17 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainStub::class, [
|
2018-03-14 07:56:14 -05:00
|
|
|
new JobStub,
|
2018-03-13 16:45:17 -05:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 08:22:17 -06:00
|
|
|
public function testAssertPushedWithoutChain()
|
2020-02-03 08:31:46 +01:00
|
|
|
{
|
|
|
|
|
$this->fake->push(new JobWithChainStub([]));
|
|
|
|
|
|
2020-02-03 08:22:17 -06:00
|
|
|
$this->fake->assertPushedWithoutChain(JobWithChainStub::class);
|
2020-02-03 08:31:46 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-13 16:45:17 -05:00
|
|
|
public function testAssertPushedWithChainSameJobDifferentChains()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push(new JobWithChainStub([
|
2018-03-14 07:56:14 -05:00
|
|
|
new JobStub,
|
2018-03-13 16:45:17 -05:00
|
|
|
]));
|
|
|
|
|
$this->fake->push(new JobWithChainStub([
|
|
|
|
|
new JobStub,
|
2018-03-14 07:56:14 -05:00
|
|
|
new JobStub,
|
2018-03-13 16:45:17 -05:00
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainStub::class, [
|
|
|
|
|
JobStub::class,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainStub::class, [
|
|
|
|
|
JobStub::class,
|
2018-03-14 07:56:14 -05:00
|
|
|
JobStub::class,
|
2018-03-13 16:45:17 -05:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertPushedWithChainUsingCallback()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push(new JobWithChainAndParameterStub('first', [
|
|
|
|
|
new JobStub,
|
2018-03-14 07:56:14 -05:00
|
|
|
new JobStub,
|
2018-03-13 16:45:17 -05:00
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->fake->push(new JobWithChainAndParameterStub('second', [
|
2018-03-14 07:56:14 -05:00
|
|
|
new JobStub,
|
2018-03-13 16:45:17 -05:00
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
|
2018-03-14 07:56:14 -05:00
|
|
|
JobStub::class,
|
2018-03-13 16:45:17 -05:00
|
|
|
], function ($job) {
|
2021-03-18 22:27:31 +01:00
|
|
|
return $job->parameter === 'second';
|
2018-03-13 16:45:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
|
|
|
|
|
JobStub::class,
|
|
|
|
|
JobStub::class,
|
|
|
|
|
], function ($job) {
|
2021-03-18 22:27:31 +01:00
|
|
|
return $job->parameter === 'second';
|
2018-03-13 16:45:17 -05:00
|
|
|
});
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The expected chain was not pushed.', $e->getMessage());
|
2018-03-13 16:45:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertPushedWithChainErrorHandling()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainStub::class, []);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\JobWithChainStub] job was not pushed.', $e->getMessage());
|
2018-03-13 16:45:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->fake->push(new JobWithChainStub([
|
2018-03-14 07:56:14 -05:00
|
|
|
new JobStub,
|
2018-03-13 16:45:17 -05:00
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainStub::class, []);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The expected chain can not be empty.', $e->getMessage());
|
2018-03-13 16:45:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainStub::class, [
|
|
|
|
|
new JobStub,
|
2018-03-14 07:56:14 -05:00
|
|
|
new JobStub,
|
2018-03-13 16:45:17 -05:00
|
|
|
]);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The expected chain was not pushed.', $e->getMessage());
|
2018-03-13 16:45:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->fake->assertPushedWithChain(JobWithChainStub::class, [
|
|
|
|
|
JobStub::class,
|
2018-03-14 07:56:14 -05:00
|
|
|
JobStub::class,
|
2018-03-13 16:45:17 -05:00
|
|
|
]);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertStringContainsString('The expected chain was not pushed.', $e->getMessage());
|
2018-03-13 16:45:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-10 10:35:51 -03:00
|
|
|
|
|
|
|
|
public function testCallUndefinedMethodErrorHandling()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->fake->undefinedMethod();
|
|
|
|
|
} catch (BadMethodCallException $e) {
|
2023-02-03 16:32:36 +00:00
|
|
|
$this->assertSame(sprintf(
|
2019-04-10 10:35:51 -03:00
|
|
|
'Call to undefined method %s::%s()', get_class($this->fake), 'undefinedMethod'
|
2023-02-03 16:32:36 +00:00
|
|
|
), $e->getMessage());
|
2019-04-10 10:35:51 -03:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-01 18:48:27 +01:00
|
|
|
|
|
|
|
|
public function testAssertClosurePushed()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push(function () {
|
|
|
|
|
// Do nothing
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->fake->assertClosurePushed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertClosurePushedWithTimes()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push(function () {
|
|
|
|
|
// Do nothing
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->fake->push(function () {
|
|
|
|
|
// Do nothing
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->fake->assertClosurePushed(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertClosureNotPushed()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->push($this->job);
|
|
|
|
|
|
|
|
|
|
$this->fake->assertClosureNotPushed();
|
|
|
|
|
}
|
2022-09-19 16:41:05 +02:00
|
|
|
|
2022-09-13 16:46:38 -03:00
|
|
|
public function testItDoesntFakeJobsPassedViaExcept()
|
|
|
|
|
{
|
|
|
|
|
$job = new JobStub;
|
|
|
|
|
|
|
|
|
|
$manager = m::mock(QueueManager::class);
|
|
|
|
|
$manager->shouldReceive('push')->once()->withArgs(function ($passedJob) use ($job) {
|
|
|
|
|
return $passedJob === $job;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$fake = (new QueueFake(new Application, [], $manager))->except(JobStub::class);
|
|
|
|
|
|
|
|
|
|
$fake->push($job);
|
|
|
|
|
$fake->push(new JobToFakeStub());
|
|
|
|
|
|
|
|
|
|
$fake->assertNotPushed(JobStub::class);
|
|
|
|
|
$fake->assertPushed(JobToFakeStub::class);
|
|
|
|
|
}
|
2023-08-25 09:27:39 -05:00
|
|
|
|
|
|
|
|
public function testItCanSerializeAndRestoreJobs()
|
|
|
|
|
{
|
|
|
|
|
// confirm that the default behavior is maintained
|
|
|
|
|
$this->fake->push(new JobWithSerialization('hello'));
|
2023-08-25 14:28:02 +00:00
|
|
|
$this->fake->assertPushed(JobWithSerialization::class, fn ($job) => $job->value === 'hello');
|
2023-08-25 09:27:39 -05:00
|
|
|
|
|
|
|
|
$job = new JobWithSerialization('hello');
|
|
|
|
|
|
|
|
|
|
$fake = new QueueFake(new Application);
|
|
|
|
|
$fake->serializeAndRestore();
|
|
|
|
|
$fake->push($job);
|
|
|
|
|
|
|
|
|
|
$fake->assertPushed(
|
|
|
|
|
JobWithSerialization::class,
|
2023-08-25 14:28:02 +00:00
|
|
|
fn ($job) => $job->value === 'hello-serialized-unserialized'
|
2023-08-25 09:27:39 -05:00
|
|
|
);
|
|
|
|
|
}
|
2023-09-19 00:50:36 +10:00
|
|
|
|
|
|
|
|
public function testItCanFakePushedJobsWithClassAndPayload()
|
|
|
|
|
{
|
|
|
|
|
$fake = new QueueFake(new Application, ['JobStub']);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($fake->shouldFakeJob('JobStub'));
|
|
|
|
|
|
|
|
|
|
$fake->push('JobStub', ['job' => 'payload']);
|
|
|
|
|
|
|
|
|
|
$fake->assertPushed('JobStub');
|
|
|
|
|
$fake->assertPushed('JobStub', 1);
|
|
|
|
|
$fake->assertPushed('JobStub', fn ($job, $queue, $payload) => $payload === ['job' => 'payload']);
|
|
|
|
|
}
|
2024-04-01 22:47:36 +02:00
|
|
|
|
|
|
|
|
public function testAssertChainUsingClassesOrObjectsArray()
|
|
|
|
|
{
|
|
|
|
|
$job = new JobWithChainStub([
|
|
|
|
|
new JobStub,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$job->assertHasChain([
|
|
|
|
|
JobStub::class,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$job->assertHasChain([
|
|
|
|
|
new JobStub(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertNoChain()
|
|
|
|
|
{
|
|
|
|
|
$job = new JobWithChainStub([]);
|
|
|
|
|
|
|
|
|
|
$job->assertDoesntHaveChain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAssertChainErrorHandling()
|
|
|
|
|
{
|
|
|
|
|
$job = new JobWithChainStub([
|
|
|
|
|
new JobStub,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$job->assertHasChain([]);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
|
|
|
|
$this->assertStringContainsString('The expected chain can not be empty.', $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$job->assertHasChain([
|
|
|
|
|
new JobStub,
|
|
|
|
|
new JobStub,
|
|
|
|
|
]);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
|
|
|
|
$this->assertStringContainsString('The job does not have the expected chain.', $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$job->assertHasChain([
|
|
|
|
|
JobStub::class,
|
|
|
|
|
JobStub::class,
|
|
|
|
|
]);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
|
|
|
|
$this->assertStringContainsString('The job does not have the expected chain.', $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$job->assertDoesntHaveChain();
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (ExpectationFailedException $e) {
|
|
|
|
|
$this->assertStringContainsString('The job has chained jobs.', $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-19 15:53:43 -05:00
|
|
|
|
|
|
|
|
public function testGetRawPushes()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->pushRaw('some-payload', null, ['options' => 'yeah']);
|
|
|
|
|
$this->fake->pushRaw('some-other-payload', 'my-queue', ['options' => 'also yeah']);
|
|
|
|
|
|
|
|
|
|
$actualPushedRaw = $this->fake->rawPushes();
|
|
|
|
|
|
|
|
|
|
$this->assertEqualsCanonicalizing([
|
|
|
|
|
['payload' => 'some-payload', 'queue' => null, 'options' => ['options' => 'yeah']],
|
|
|
|
|
['payload' => 'some-other-payload', 'queue' => 'my-queue', 'options' => ['options' => 'also yeah']],
|
|
|
|
|
], $actualPushedRaw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPushedRaw()
|
|
|
|
|
{
|
|
|
|
|
$this->fake->pushRaw('some-payload', null, ['options' => 'yeah']);
|
|
|
|
|
$this->fake->pushRaw('some-other-payload', 'my-queue', ['options' => 'also yeah']);
|
|
|
|
|
|
|
|
|
|
$this->assertCount(2, $this->fake->pushedRaw());
|
|
|
|
|
|
|
|
|
|
$pushedRaw = $this->fake->pushedRaw(fn ($payload) => $payload === 'some-payload');
|
|
|
|
|
$this->assertCount(1, $pushedRaw);
|
|
|
|
|
$this->assertEqualsCanonicalizing(
|
|
|
|
|
['payload' => 'some-payload', 'queue' => null, 'options' => ['options' => 'yeah']],
|
|
|
|
|
$pushedRaw[0]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$pushedRaw = $this->fake->pushedRaw(
|
|
|
|
|
fn ($payload, $queue, $options) => $payload === 'some-other-payload'
|
|
|
|
|
&& $queue === 'my-queue'
|
|
|
|
|
&& $options['options'] === 'also yeah'
|
|
|
|
|
);
|
|
|
|
|
$this->assertCount(1, $pushedRaw);
|
|
|
|
|
|
|
|
|
|
$pushedRaw = $this->fake->pushedRaw(fn ($payload, $queue, $options) => $options === []);
|
|
|
|
|
$this->assertCount(0, $pushedRaw);
|
|
|
|
|
}
|
2017-08-13 19:19:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class JobStub
|
|
|
|
|
{
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-13 16:45:17 -05:00
|
|
|
|
2022-03-10 13:00:51 -06:00
|
|
|
class JobToFakeStub
|
|
|
|
|
{
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-13 16:45:17 -05:00
|
|
|
class JobWithChainStub
|
|
|
|
|
{
|
|
|
|
|
use Queueable;
|
|
|
|
|
|
2018-03-14 07:56:14 -05:00
|
|
|
public function __construct($chain)
|
2018-03-13 16:45:17 -05:00
|
|
|
{
|
|
|
|
|
$this->chain($chain);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class JobWithChainAndParameterStub
|
|
|
|
|
{
|
|
|
|
|
use Queueable;
|
|
|
|
|
|
|
|
|
|
public $parameter;
|
|
|
|
|
|
2018-03-14 07:56:14 -05:00
|
|
|
public function __construct($parameter, $chain)
|
2018-03-13 16:45:17 -05:00
|
|
|
{
|
|
|
|
|
$this->parameter = $parameter;
|
|
|
|
|
$this->chain($chain);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
2018-03-14 07:56:14 -05:00
|
|
|
}
|
2023-08-25 09:27:39 -05:00
|
|
|
|
|
|
|
|
class JobWithSerialization
|
|
|
|
|
{
|
|
|
|
|
use Queueable;
|
|
|
|
|
|
|
|
|
|
public function __construct(public $value)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __serialize(): array
|
|
|
|
|
{
|
2023-08-25 14:28:02 +00:00
|
|
|
return ['value' => $this->value.'-serialized'];
|
2023-08-25 09:27:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __unserialize(array $data): void
|
|
|
|
|
{
|
2023-08-25 14:28:02 +00:00
|
|
|
$this->value = $data['value'].'-unserialized';
|
2023-08-25 09:27:39 -05:00
|
|
|
}
|
|
|
|
|
}
|