Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix php 7.4 tagged model typed props #732

Merged
merged 7 commits into from
Dec 23, 2019
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
14 changes: 13 additions & 1 deletion src/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Mail\SendQueuedMailable;
use Illuminate\Notifications\SendQueuedNotifications;
use ReflectionClass;
use ReflectionProperty;
use stdClass;

class Tags
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}
}

return $property->getValue($target);
}

/**
* Extract the listener from a queued job.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/Fixtures/FakeListenerWithProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;

use Illuminate\Contracts\Events\Dispatcher;

class FakeListenerWithProperties
{
/**
* @var Dispatcher
*/
protected $dispatcher;

/**
* @var FakeEventWithModel
*/
protected $fakeModel;

public function __construct(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

public function handle(FakeEventWithModel $fakeEventWithModel): void
{
$this->fakeModel = $fakeEventWithModel->model;
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Fixtures/FakeListenerWithTypedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;

use Illuminate\Contracts\Events\Dispatcher;

class FakeListenerWithTypedProperties
{
protected Dispatcher $dispatcher;

protected FakeEventWithModel $fakeModel;

public function __construct(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

public function handle(FakeEventWithModel $fakeEventWithModel): void
{
$this->fakeModel = $fakeEventWithModel->model;
}
}
27 changes: 27 additions & 0 deletions tests/Unit/RedisPayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
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\FakeListenerWithTypedProperties;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeModel;
use Laravel\Horizon\Tests\UnitTest;
use Mockery;
Expand Down Expand Up @@ -94,6 +96,31 @@ 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']);
}

/**
* @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]));
Expand Down