From 2fc462062a5b76fc5e09b02ca589904601dcbd90 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Mon, 21 Aug 2023 09:51:24 -0400 Subject: [PATCH 01/11] serialize and restore model --- .../Support/Testing/Fakes/QueueFake.php | 33 ++++++++++++++++- tests/Support/SupportTestingQueueFakeTest.php | 37 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 7218644ba860..492936d7ab8c 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -43,6 +43,13 @@ class QueueFake extends QueueManager implements Fake, Queue */ protected $jobs = []; + /** + * Whether jobs should be serialized and then restored before checking assertions. + * + * @var bool + */ + protected bool $serializeAndRestore = false; + /** * Create a new fake queue instance. * @@ -60,6 +67,19 @@ public function __construct($app, $jobsToFake = [], $queue = null) $this->queue = $queue; } + /** + * Set if the job should serialize and restore before checking assertions. + * + * @param bool $serializeAndRestore + * @return $this + */ + public function serializeAndRestoreJobs(bool $serializeAndRestore = true): self + { + $this->serializeAndRestore = $serializeAndRestore; + + return $this; + } + /** * Specify the jobs that should be queued instead of faked. * @@ -352,7 +372,7 @@ public function push($job, $data = '', $queue = null) } $this->jobs[is_object($job) ? get_class($job) : $job][] = [ - 'job' => $job, + 'job' => $this->serializeAndRestore ? $this->serializeAndRestore($job) : $job, 'queue' => $queue, 'data' => $data, ]; @@ -363,6 +383,17 @@ public function push($job, $data = '', $queue = null) } } + /** + * Serialize and then unserialize the job to simulate the queueing process. + * + * @param $job + * @return mixed + */ + protected function serializeAndRestore($job) + { + return unserialize(serialize($job)); + } + /** * Determine if a job should be faked or actually dispatched. * diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index b1d2afefbf25..1d8a9f942146 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -374,6 +374,24 @@ public function testItDoesntFakeJobsPassedViaExcept() $fake->assertNotPushed(JobStub::class); $fake->assertPushed(JobToFakeStub::class); } + + public function testItCanSerializeAndRestoreJobs() + { + // confirm that the default behavior is maintained + $this->fake->push(new JobWithSerialization('hello')); + $this->fake->assertPushed(JobWithSerialization::class, fn($job) => $job->value === 'hello'); + + $job = new JobWithSerialization('hello'); + + $fake = new QueueFake(new Application); + $fake->serializeAndRestoreJobs(); + $fake->push($job); + + $fake->assertPushed( + JobWithSerialization::class, + fn($job) => $job->value === 'hello-serialized-unserialized' + ); + } } class JobStub @@ -424,3 +442,22 @@ public function handle() // } } + +class JobWithSerialization +{ + use Queueable; + + public function __construct(public $value) + { + } + + public function __serialize(): array + { + return ['value' => $this->value .'-serialized']; + } + + public function __unserialize(array $data): void + { + $this->value = $data['value'] .'-unserialized'; + } +} From 164f6801b1d146ba65d110c5b177a03a636a4a52 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Mon, 21 Aug 2023 10:27:38 -0400 Subject: [PATCH 02/11] move to a trait --- .../Support/Testing/Fakes/QueueFake.php | 34 +--------------- .../Traits/SerializesAndRestoresTrait.php | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 492936d7ab8c..9c57a4e3cbd5 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -9,11 +9,12 @@ use Illuminate\Queue\QueueManager; use Illuminate\Support\Collection; use Illuminate\Support\Traits\ReflectsClosures; +use Illuminate\Support\Traits\SerializesAndRestoresTrait; use PHPUnit\Framework\Assert as PHPUnit; class QueueFake extends QueueManager implements Fake, Queue { - use ReflectsClosures; + use ReflectsClosures, SerializesAndRestoresTrait; /** * The original queue manager. @@ -43,13 +44,6 @@ class QueueFake extends QueueManager implements Fake, Queue */ protected $jobs = []; - /** - * Whether jobs should be serialized and then restored before checking assertions. - * - * @var bool - */ - protected bool $serializeAndRestore = false; - /** * Create a new fake queue instance. * @@ -67,19 +61,6 @@ public function __construct($app, $jobsToFake = [], $queue = null) $this->queue = $queue; } - /** - * Set if the job should serialize and restore before checking assertions. - * - * @param bool $serializeAndRestore - * @return $this - */ - public function serializeAndRestoreJobs(bool $serializeAndRestore = true): self - { - $this->serializeAndRestore = $serializeAndRestore; - - return $this; - } - /** * Specify the jobs that should be queued instead of faked. * @@ -383,17 +364,6 @@ public function push($job, $data = '', $queue = null) } } - /** - * Serialize and then unserialize the job to simulate the queueing process. - * - * @param $job - * @return mixed - */ - protected function serializeAndRestore($job) - { - return unserialize(serialize($job)); - } - /** * Determine if a job should be faked or actually dispatched. * diff --git a/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php b/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php new file mode 100644 index 000000000000..537710777397 --- /dev/null +++ b/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php @@ -0,0 +1,39 @@ +serializeAndRestore = $serializeAndRestore; + + return $this; + } + + /** + * Serialize and then unserialize the job to simulate the queueing process. + * + * @param mixed $job + * @return mixed + */ + protected function serializeAndRestore($job) + { + return unserialize(serialize($job)); + } +} From 36ba2b431a8696dabc2870e78b56b2262bda8edf Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Mon, 21 Aug 2023 10:53:59 -0400 Subject: [PATCH 03/11] make name more generic --- src/Illuminate/Support/Testing/Fakes/QueueFake.php | 2 +- .../Support/Traits/SerializesAndRestoresTrait.php | 12 +++++------- tests/Support/SupportTestingQueueFakeTest.php | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 9c57a4e3cbd5..0f5367a5c29c 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -353,7 +353,7 @@ public function push($job, $data = '', $queue = null) } $this->jobs[is_object($job) ? get_class($job) : $job][] = [ - 'job' => $this->serializeAndRestore ? $this->serializeAndRestore($job) : $job, + 'job' => $this->serializeAndRestore ? $this->serializeAndRestoreQueueable($job) : $job, 'queue' => $queue, 'data' => $data, ]; diff --git a/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php b/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php index 537710777397..001d49534f7d 100644 --- a/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php +++ b/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php @@ -2,8 +2,6 @@ namespace Illuminate\Support\Traits; -use Illuminate\Support\Testing\Fakes\QueueFake; - trait SerializesAndRestoresTrait { /** @@ -19,7 +17,7 @@ trait SerializesAndRestoresTrait * @param bool $serializeAndRestore * @return $this */ - public function serializeAndRestoreJobs(bool $serializeAndRestore = true): static + public function serializeAndRestoreItems(bool $serializeAndRestore = true): static { $this->serializeAndRestore = $serializeAndRestore; @@ -27,13 +25,13 @@ public function serializeAndRestoreJobs(bool $serializeAndRestore = true): stati } /** - * Serialize and then unserialize the job to simulate the queueing process. + * Serialize and then unserialize the item to simulate the queueing process. * - * @param mixed $job + * @param mixed $queueable * @return mixed */ - protected function serializeAndRestore($job) + protected function serializeAndRestoreQueueable($queueable) { - return unserialize(serialize($job)); + return unserialize(serialize($queueable)); } } diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 1d8a9f942146..9661189e2f4c 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -384,7 +384,7 @@ public function testItCanSerializeAndRestoreJobs() $job = new JobWithSerialization('hello'); $fake = new QueueFake(new Application); - $fake->serializeAndRestoreJobs(); + $fake->serializeAndRestoreItems(); $fake->push($job); $fake->assertPushed( From 7a319c14daaabf6e54a80347a6142ee45bb78984 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:49:24 -0400 Subject: [PATCH 04/11] remove references to `job` --- src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php b/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php index 001d49534f7d..fa291575f692 100644 --- a/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php +++ b/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php @@ -5,14 +5,14 @@ trait SerializesAndRestoresTrait { /** - * Whether jobs should be serialized and then restored before checking assertions. + * Whether items should be serialized and then restored before checking assertions. * * @var bool */ protected bool $serializeAndRestore = false; /** - * Set if the job should serialize and restore before checking assertions. + * Set if items should serialize and restore before checking assertions. * * @param bool $serializeAndRestore * @return $this From 163727e21f2715e09d879309677395612431f152 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 23 Aug 2023 14:12:32 -0500 Subject: [PATCH 05/11] formatting --- .../Support/Testing/Fakes/QueueFake.php | 34 ++++++++++++++++- .../Traits/SerializesAndRestoresTrait.php | 37 ------------------- tests/Support/SupportTestingQueueFakeTest.php | 2 +- 3 files changed, 33 insertions(+), 40 deletions(-) delete mode 100644 src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 0f5367a5c29c..9192ed40b6bb 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -9,12 +9,11 @@ use Illuminate\Queue\QueueManager; use Illuminate\Support\Collection; use Illuminate\Support\Traits\ReflectsClosures; -use Illuminate\Support\Traits\SerializesAndRestoresTrait; use PHPUnit\Framework\Assert as PHPUnit; class QueueFake extends QueueManager implements Fake, Queue { - use ReflectsClosures, SerializesAndRestoresTrait; + use ReflectsClosures; /** * The original queue manager. @@ -44,6 +43,13 @@ class QueueFake extends QueueManager implements Fake, Queue */ protected $jobs = []; + /** + * Indicates if items should be serialized and restored before checking assertions. + * + * @var bool + */ + protected bool $serializeAndRestore = false; + /** * Create a new fake queue instance. * @@ -492,6 +498,30 @@ public function pushedJobs() return $this->jobs; } + /** + * Specify if jobs should be serialized and restored before assertions are evaluated. + * + * @param bool $serializeAndRestore + * @return $this + */ + public function serializeAndRestore(bool $serializeAndRestore = true): static + { + $this->serializeAndRestore = $serializeAndRestore; + + return $this; + } + + /** + * Serialize and unserialize the item to simulate the queueing process. + * + * @param mixed $queueable + * @return mixed + */ + protected function serializeAndRestoreQueueable($queueable) + { + return unserialize(serialize($queueable)); + } + /** * Get the connection name for the queue. * diff --git a/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php b/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php deleted file mode 100644 index fa291575f692..000000000000 --- a/src/Illuminate/Support/Traits/SerializesAndRestoresTrait.php +++ /dev/null @@ -1,37 +0,0 @@ -serializeAndRestore = $serializeAndRestore; - - return $this; - } - - /** - * Serialize and then unserialize the item to simulate the queueing process. - * - * @param mixed $queueable - * @return mixed - */ - protected function serializeAndRestoreQueueable($queueable) - { - return unserialize(serialize($queueable)); - } -} diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 9661189e2f4c..468eafdf26da 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -384,7 +384,7 @@ public function testItCanSerializeAndRestoreJobs() $job = new JobWithSerialization('hello'); $fake = new QueueFake(new Application); - $fake->serializeAndRestoreItems(); + $fake->serializeAndRestore(); $fake->push($job); $fake->assertPushed( From 393f2e297329b38a92e562e8003a9756a15614da Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 23 Aug 2023 14:13:37 -0500 Subject: [PATCH 06/11] remove hint --- src/Illuminate/Support/Testing/Fakes/QueueFake.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 9192ed40b6bb..6c24250c9763 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -504,7 +504,7 @@ public function pushedJobs() * @param bool $serializeAndRestore * @return $this */ - public function serializeAndRestore(bool $serializeAndRestore = true): static + public function serializeAndRestore(bool $serializeAndRestore = true) { $this->serializeAndRestore = $serializeAndRestore; From b3cbd1ebb22aaced772310ef35dbc623aa43a11e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 23 Aug 2023 14:14:50 -0500 Subject: [PATCH 07/11] formatting --- src/Illuminate/Support/Testing/Fakes/QueueFake.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 6c24250c9763..280c9ab9c3fb 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -359,7 +359,7 @@ public function push($job, $data = '', $queue = null) } $this->jobs[is_object($job) ? get_class($job) : $job][] = [ - 'job' => $this->serializeAndRestore ? $this->serializeAndRestoreQueueable($job) : $job, + 'job' => $this->serializeAndRestore ? $this->serializeAndRestoreJob($job) : $job, 'queue' => $queue, 'data' => $data, ]; @@ -512,14 +512,14 @@ public function serializeAndRestore(bool $serializeAndRestore = true) } /** - * Serialize and unserialize the item to simulate the queueing process. + * Serialize and unserialize the job to simulate the queueing process. * - * @param mixed $queueable + * @param mixed $job * @return mixed */ - protected function serializeAndRestoreQueueable($queueable) + protected function serializeAndRestoreJob($job) { - return unserialize(serialize($queueable)); + return unserialize(serialize($job)); } /** From 39ccc2f9b2553602a8173b3a5d377edc92045df6 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 23 Aug 2023 20:14:42 -0400 Subject: [PATCH 08/11] fix docblock comment for QueueFake --- src/Illuminate/Support/Testing/Fakes/QueueFake.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 280c9ab9c3fb..aa63e2e10c18 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -44,7 +44,7 @@ class QueueFake extends QueueManager implements Fake, Queue protected $jobs = []; /** - * Indicates if items should be serialized and restored before checking assertions. + * Indicates if items should be serialized and restored when pushed to the queue. * * @var bool */ @@ -499,7 +499,7 @@ public function pushedJobs() } /** - * Specify if jobs should be serialized and restored before assertions are evaluated. + * Specify if jobs should be serialized and restored when being "pushed" to the queue. * * @param bool $serializeAndRestore * @return $this From 70e68e47cdb762749d28f171eee551b193c3b0d3 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 23 Aug 2023 20:52:33 -0400 Subject: [PATCH 09/11] add serialization/restore functionality to BusFake --- .../Support/Testing/Fakes/BusFake.php | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/BusFake.php b/src/Illuminate/Support/Testing/Fakes/BusFake.php index 6061344730d8..5922d6c4f129 100644 --- a/src/Illuminate/Support/Testing/Fakes/BusFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BusFake.php @@ -71,6 +71,13 @@ class BusFake implements Fake, QueueingDispatcher */ protected $batches = []; + /** + * Indicates if commands should be serialized and restored when pushed to the Bus. + * + * @var bool + */ + protected bool $serializeAndRestore = false; + /** * Create a new bus fake instance. * @@ -585,7 +592,7 @@ public function hasDispatchedAfterResponse($command) public function dispatch($command) { if ($this->shouldFakeJob($command)) { - $this->commands[get_class($command)][] = $command; + $this->commands[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatch($command); } @@ -603,7 +610,7 @@ public function dispatch($command) public function dispatchSync($command, $handler = null) { if ($this->shouldFakeJob($command)) { - $this->commandsSync[get_class($command)][] = $command; + $this->commandsSync[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatchSync($command, $handler); } @@ -619,7 +626,7 @@ public function dispatchSync($command, $handler = null) public function dispatchNow($command, $handler = null) { if ($this->shouldFakeJob($command)) { - $this->commands[get_class($command)][] = $command; + $this->commands[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatchNow($command, $handler); } @@ -634,7 +641,7 @@ public function dispatchNow($command, $handler = null) public function dispatchToQueue($command) { if ($this->shouldFakeJob($command)) { - $this->commands[get_class($command)][] = $command; + $this->commands[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatchToQueue($command); } @@ -649,7 +656,7 @@ public function dispatchToQueue($command) public function dispatchAfterResponse($command) { if ($this->shouldFakeJob($command)) { - $this->commandsAfterResponse[get_class($command)][] = $command; + $this->commandsAfterResponse[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatch($command); } @@ -690,6 +697,41 @@ public function batch($jobs) return new PendingBatchFake($this, Collection::wrap($jobs)); } + /** + * Specify if commands should be serialized and restored when being batched. + * + * @param bool $serializeAndRestore + * @return $this + */ + public function serializeAndRestore(bool $serializeAndRestore = true) + { + $this->serializeAndRestore = $serializeAndRestore; + + return $this; + } + + /** + * Serialize and unserialize the command to simulate the queueing process. + * + * @param mixed $command + * @return mixed + */ + protected function serializeAndRestoreCommand($command) + { + return unserialize(serialize($command)); + } + + /** + * Return the command representation that should be stored. + * + * @param mixed $command + * @return mixed + */ + protected function getCommandRepresentation($command) + { + return $this->serializeAndRestore ? $this->serializeAndRestoreCommand($command) : $command; + } + /** * Dispatch an empty job batch for testing. * From e356fe208df37ec393ee3533f42f487f0dc03c8a Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 23 Aug 2023 20:52:55 -0400 Subject: [PATCH 10/11] add serialization tests for BusFake --- tests/Support/SupportTestingBusFakeTest.php | 71 +++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/Support/SupportTestingBusFakeTest.php b/tests/Support/SupportTestingBusFakeTest.php index 4ea6cdd9b19d..a95de8c32bc1 100644 --- a/tests/Support/SupportTestingBusFakeTest.php +++ b/tests/Support/SupportTestingBusFakeTest.php @@ -7,7 +7,9 @@ use Illuminate\Contracts\Bus\QueueingDispatcher; use Illuminate\Support\Testing\Fakes\BatchRepositoryFake; use Illuminate\Support\Testing\Fakes\BusFake; +use Illuminate\Support\Testing\Fakes\PendingBatchFake; use Mockery as m; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; @@ -666,6 +668,55 @@ public function testDecrementPendingJobsInFakeBatch() $this->assertSame(0, $batch->failedJobs); $this->assertSame(0, $batch->pendingJobs); } + + #[DataProvider('serializeAndRestoreCommandMethodsDataProvider')] + public function testCanSerializeAndRestoreCommands($commandFunctionName, $assertionFunctionName) + { + $serializingBusFake = (clone $this->fake)->serializeAndRestore(); + + // without setting the serialization, the job should return the value passed in + $this->fake->{$commandFunctionName}(new BusFakeJobWithSerialization('hello')); + $this->fake->{$assertionFunctionName}(BusFakeJobWithSerialization::class, fn($command) => $command->value === 'hello'); + + // when enabling the serializeAndRestore property, job has value modified + $serializingBusFake->{$commandFunctionName}(new BusFakeJobWithSerialization('hello')); + $serializingBusFake->{$assertionFunctionName}( + BusFakeJobWithSerialization::class, + fn($command) => $command->value === 'hello-serialized-unserialized' + ); + } + + public static function serializeAndRestoreCommandMethodsDataProvider(): array + { + return [ + 'dispatch' => ['dispatch', 'assertDispatched'], + 'dispatchSync' => ['dispatchSync', 'assertDispatchedSync'], + 'dispatchNow' => ['dispatchNow', 'assertDispatched'], + 'dispatchAfterResponse' => ['dispatchAfterResponse', 'assertDispatchedAfterResponse'], + ]; + } + + public function testCanSerializeAndRestoreCommandsInBatch() + { + $serializingBusFake = (clone $this->fake)->serializeAndRestore(); + + // without setting the serialization, the batch should return the value passed in + $this->fake->batch([ + new BusFakeJobWithSerialization('hello'), + ])->dispatch(); + $this->fake->assertBatched(function(PendingBatchFake $batchedCollection) { + return $batchedCollection->jobs->count() === 1 && $batchedCollection->jobs->first()->value === 'hello'; + }); + + // when enabling the serializeAndRestore property, each batch jobs will each be serialized/restored + $serializingBusFake->batch([ + new BusFakeJobWithSerialization('hello'), + ])->dispatch(); + + $serializingBusFake->assertBatched(function(PendingBatchFake $batchedCollection) { + return $batchedCollection->jobs->count() === 1 && $batchedCollection->jobs->first()->value === 'hello'; + }); + } } class BusJobStub @@ -692,3 +743,23 @@ class ThirdJob { // } + + +class BusFakeJobWithSerialization +{ + use Queueable; + + public function __construct(public $value) + { + } + + public function __serialize(): array + { + return ['value' => $this->value .'-serialized']; + } + + public function __unserialize(array $data): void + { + $this->value = $data['value'] .'-unserialized'; + } +} From cb7c669221992eeb6b0261aea978c4aeffa6eaae Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 25 Aug 2023 09:23:33 -0500 Subject: [PATCH 11/11] formatting --- .../Support/Testing/Fakes/BusFake.php | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/BusFake.php b/src/Illuminate/Support/Testing/Fakes/BusFake.php index 5922d6c4f129..669f512b3daa 100644 --- a/src/Illuminate/Support/Testing/Fakes/BusFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BusFake.php @@ -697,41 +697,6 @@ public function batch($jobs) return new PendingBatchFake($this, Collection::wrap($jobs)); } - /** - * Specify if commands should be serialized and restored when being batched. - * - * @param bool $serializeAndRestore - * @return $this - */ - public function serializeAndRestore(bool $serializeAndRestore = true) - { - $this->serializeAndRestore = $serializeAndRestore; - - return $this; - } - - /** - * Serialize and unserialize the command to simulate the queueing process. - * - * @param mixed $command - * @return mixed - */ - protected function serializeAndRestoreCommand($command) - { - return unserialize(serialize($command)); - } - - /** - * Return the command representation that should be stored. - * - * @param mixed $command - * @return mixed - */ - protected function getCommandRepresentation($command) - { - return $this->serializeAndRestore ? $this->serializeAndRestoreCommand($command) : $command; - } - /** * Dispatch an empty job batch for testing. * @@ -796,6 +761,41 @@ protected function shouldDispatchCommand($command) })->isNotEmpty(); } + /** + * Specify if commands should be serialized and restored when being batched. + * + * @param bool $serializeAndRestore + * @return $this + */ + public function serializeAndRestore(bool $serializeAndRestore = true) + { + $this->serializeAndRestore = $serializeAndRestore; + + return $this; + } + + /** + * Serialize and unserialize the command to simulate the queueing process. + * + * @param mixed $command + * @return mixed + */ + protected function serializeAndRestoreCommand($command) + { + return unserialize(serialize($command)); + } + + /** + * Return the command representation that should be stored. + * + * @param mixed $command + * @return mixed + */ + protected function getCommandRepresentation($command) + { + return $this->serializeAndRestore ? $this->serializeAndRestoreCommand($command) : $command; + } + /** * Set the pipes commands should be piped through before dispatching. *