From 610260ac449c8a660e5fb8d1a28088eab7a36a81 Mon Sep 17 00:00:00 2001 From: Jeff Puckett Date: Mon, 23 Dec 2019 02:38:27 +0000 Subject: [PATCH 1/7] add test tags are correctly determined for listeners --- .../Fixtures/FakeListenerWithProperties.php | 28 +++++++++++++++++++ tests/Unit/RedisPayloadTest.php | 12 ++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/Unit/Fixtures/FakeListenerWithProperties.php diff --git a/tests/Unit/Fixtures/FakeListenerWithProperties.php b/tests/Unit/Fixtures/FakeListenerWithProperties.php new file mode 100644 index 00000000..b8e54238 --- /dev/null +++ b/tests/Unit/Fixtures/FakeListenerWithProperties.php @@ -0,0 +1,28 @@ +dispatcher = $dispatcher; + } + + public function handle(FakeEventWithModel $fakeEventWithModel): void + { + $this->fakeModel = $fakeEventWithModel->model; + } +} diff --git a/tests/Unit/RedisPayloadTest.php b/tests/Unit/RedisPayloadTest.php index b6b04dfc..0e0de01e 100644 --- a/tests/Unit/RedisPayloadTest.php +++ b/tests/Unit/RedisPayloadTest.php @@ -15,6 +15,7 @@ use Laravel\Horizon\Tests\Unit\Fixtures\FakeJobWithEloquentModel; use Laravel\Horizon\Tests\Unit\Fixtures\FakeJobWithTagsMethod; use Laravel\Horizon\Tests\Unit\Fixtures\FakeListener; +use Laravel\Horizon\Tests\Unit\Fixtures\FakeListenerWithProperties; use Laravel\Horizon\Tests\Unit\Fixtures\FakeModel; use Laravel\Horizon\Tests\UnitTest; use Mockery; @@ -94,6 +95,17 @@ public function test_tags_are_correctly_extracted_for_listeners() ], $JobPayload->decoded['tags']); } + public function test_tags_are_correctly_determined_for_listeners() + { + $JobPayload = new JobPayload(json_encode(['id' => 1])); + + $job = new CallQueuedListener(FakeListenerWithProperties::class, 'handle', [new FakeEventWithModel(42)]); + + $JobPayload->prepare($job); + + $this->assertEquals([FakeModel::class.':42'], $JobPayload->decoded['tags']); + } + public function test_listener_and_event_tags_can_merge_auto_tag_events() { $JobPayload = new JobPayload(json_encode(['id' => 1])); From 35f4e14fec0a669d5f1921e56cc74ee296c844ee Mon Sep 17 00:00:00 2001 From: Jeff Puckett Date: Mon, 23 Dec 2019 02:42:51 +0000 Subject: [PATCH 2/7] add test with typed props --- .../FakeListenerWithTypedProperties.php | 22 +++++++++++++++++++ tests/Unit/RedisPayloadTest.php | 15 +++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tests/Unit/Fixtures/FakeListenerWithTypedProperties.php diff --git a/tests/Unit/Fixtures/FakeListenerWithTypedProperties.php b/tests/Unit/Fixtures/FakeListenerWithTypedProperties.php new file mode 100644 index 00000000..5062dddc --- /dev/null +++ b/tests/Unit/Fixtures/FakeListenerWithTypedProperties.php @@ -0,0 +1,22 @@ +dispatcher = $dispatcher; + } + + public function handle(FakeEventWithModel $fakeEventWithModel): void + { + $this->fakeModel = $fakeEventWithModel->model; + } +} diff --git a/tests/Unit/RedisPayloadTest.php b/tests/Unit/RedisPayloadTest.php index 0e0de01e..57095578 100644 --- a/tests/Unit/RedisPayloadTest.php +++ b/tests/Unit/RedisPayloadTest.php @@ -16,6 +16,7 @@ use Laravel\Horizon\Tests\Unit\Fixtures\FakeJobWithTagsMethod; use Laravel\Horizon\Tests\Unit\Fixtures\FakeListener; use Laravel\Horizon\Tests\Unit\Fixtures\FakeListenerWithProperties; +use Laravel\Horizon\Tests\Unit\Fixtures\FakeListenerWithTypedProperties; use Laravel\Horizon\Tests\Unit\Fixtures\FakeModel; use Laravel\Horizon\Tests\UnitTest; use Mockery; @@ -106,6 +107,20 @@ public function test_tags_are_correctly_determined_for_listeners() $this->assertEquals([FakeModel::class.':42'], $JobPayload->decoded['tags']); } + /** + * @requires PHP 7.4 + */ + public function test_tags_are_correctly_determined_for_listeners_with_property_types() + { + $JobPayload = new JobPayload(json_encode(['id' => 1])); + + $job = new CallQueuedListener(FakeListenerWithTypedProperties::class, 'handle', [new FakeEventWithModel(21)]); + + $JobPayload->prepare($job); + + $this->assertEquals([FakeModel::class.':21'], $JobPayload->decoded['tags']); + } + public function test_listener_and_event_tags_can_merge_auto_tag_events() { $JobPayload = new JobPayload(json_encode(['id' => 1])); From fc1334b7c25a3f3a74e1acaf4e4a0e151ea3d46d Mon Sep 17 00:00:00 2001 From: Jeff Puckett Date: Mon, 23 Dec 2019 03:40:13 +0000 Subject: [PATCH 3/7] fix laravel/horizon#730 php 7.4 uninitialized property type detection --- src/Tags.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Tags.php b/src/Tags.php index 99a5d366..6492ad6f 100644 --- a/src/Tags.php +++ b/src/Tags.php @@ -9,6 +9,7 @@ use Illuminate\Mail\SendQueuedMailable; use Illuminate\Notifications\SendQueuedNotifications; use ReflectionClass; +use ReflectionProperty; use stdClass; class Tags @@ -107,7 +108,7 @@ public static function modelsFor(array $targets) $models[] = collect((new ReflectionClass($target))->getProperties())->map(function ($property) use ($target) { $property->setAccessible(true); - $value = $property->getValue($target); + $value = static::getValue($property, $target); if ($value instanceof Model) { return [$value]; @@ -120,6 +121,17 @@ public static function modelsFor(array $targets) return collect($models)->collapse()->unique(); } + protected static function getValue(ReflectionProperty $property, $target) + { + if (method_exists($property, 'isInitialized')) { + if(!$property->isInitialized($target)) { + return null; + } + } + + return $property->getValue($target); + } + /** * Extract the listener from a queued job. * From 85f312fb47e93a5628d6b6e54e159717e9ed46e5 Mon Sep 17 00:00:00 2001 From: Jeff Puckett Date: Mon, 23 Dec 2019 04:12:35 +0000 Subject: [PATCH 4/7] cs fix style --- src/Tags.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tags.php b/src/Tags.php index 6492ad6f..27930e54 100644 --- a/src/Tags.php +++ b/src/Tags.php @@ -124,7 +124,7 @@ public static function modelsFor(array $targets) protected static function getValue(ReflectionProperty $property, $target) { if (method_exists($property, 'isInitialized')) { - if(!$property->isInitialized($target)) { + if(! $property->isInitialized($target)) { return null; } } From 7c06646f4c32364dc2ae651bd0c9c19bdee4aec0 Mon Sep 17 00:00:00 2001 From: Jeff Puckett Date: Mon, 23 Dec 2019 04:14:14 +0000 Subject: [PATCH 5/7] cs fix spacing --- src/Tags.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tags.php b/src/Tags.php index 27930e54..d0eb13ec 100644 --- a/src/Tags.php +++ b/src/Tags.php @@ -124,7 +124,7 @@ public static function modelsFor(array $targets) protected static function getValue(ReflectionProperty $property, $target) { if (method_exists($property, 'isInitialized')) { - if(! $property->isInitialized($target)) { + if (! $property->isInitialized($target)) { return null; } } From 54d2b1fbfc0d5f22843782573f1d47ad0456d27c Mon Sep 17 00:00:00 2001 From: Jeff Puckett Date: Mon, 23 Dec 2019 04:15:32 +0000 Subject: [PATCH 6/7] doc null or mixed return --- src/Tags.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Tags.php b/src/Tags.php index d0eb13ec..132f7667 100644 --- a/src/Tags.php +++ b/src/Tags.php @@ -121,6 +121,9 @@ public static function modelsFor(array $targets) return collect($models)->collapse()->unique(); } + /** + * @return mixed|null + */ protected static function getValue(ReflectionProperty $property, $target) { if (method_exists($property, 'isInitialized')) { From 265e01be40841c8eb64c4043a6c8b71b64cd1b6a Mon Sep 17 00:00:00 2001 From: Jeff Puckett Date: Mon, 23 Dec 2019 04:16:50 +0000 Subject: [PATCH 7/7] cs fix implicit null return --- src/Tags.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Tags.php b/src/Tags.php index 132f7667..6684ec05 100644 --- a/src/Tags.php +++ b/src/Tags.php @@ -121,14 +121,11 @@ public static function modelsFor(array $targets) return collect($models)->collapse()->unique(); } - /** - * @return mixed|null - */ protected static function getValue(ReflectionProperty $property, $target) { if (method_exists($property, 'isInitialized')) { if (! $property->isInitialized($target)) { - return null; + return; } }