From f67004ca07b41b814667f0222347d4af059a6c5a Mon Sep 17 00:00:00 2001 From: Christian Charzewski Date: Fri, 26 Jul 2024 07:38:33 +0200 Subject: [PATCH 1/2] Made route parameter in Route Binding optional to allow explicit route binding in broadcast routes --- src/Illuminate/Routing/RouteBinding.php | 4 ++-- tests/Broadcasting/BroadcasterTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Routing/RouteBinding.php b/src/Illuminate/Routing/RouteBinding.php index 9d545039aa1a..ef37e7063564 100644 --- a/src/Illuminate/Routing/RouteBinding.php +++ b/src/Illuminate/Routing/RouteBinding.php @@ -58,7 +58,7 @@ protected static function createClassBinding($container, $binding) */ public static function forModel($container, $class, $callback = null) { - return function ($value, $route) use ($container, $class, $callback) { + return function ($value, $route = null) use ($container, $class, $callback) { if (is_null($value)) { return; } @@ -68,7 +68,7 @@ public static function forModel($container, $class, $callback = null) // throw a not found exception otherwise we will return the instance. $instance = $container->make($class); - $routeBindingMethod = $route->allowsTrashedBindings() && in_array(SoftDeletes::class, class_uses_recursive($instance)) + $routeBindingMethod = $route?->allowsTrashedBindings() && in_array(SoftDeletes::class, class_uses_recursive($instance)) ? 'resolveSoftDeletableRouteBinding' : 'resolveRouteBinding'; diff --git a/tests/Broadcasting/BroadcasterTest.php b/tests/Broadcasting/BroadcasterTest.php index eca82ca9754d..1991f7ceed3c 100644 --- a/tests/Broadcasting/BroadcasterTest.php +++ b/tests/Broadcasting/BroadcasterTest.php @@ -8,6 +8,7 @@ use Illuminate\Contracts\Routing\BindingRegistrar; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; +use Illuminate\Routing\RouteBinding; use Mockery as m; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -84,6 +85,23 @@ public function testCanUseChannelClasses() $this->assertEquals(['model.1.instance', 'something'], $parameters); } + public function testModelRouteBinding() + { + $container = new Container; + Container::setInstance($container); + $binder = m::mock(BindingRegistrar::class); + $callback = RouteBinding::forModel($container, BroadcasterTestEloquentModelStub::class); + + $binder->shouldReceive('getBindingCallback')->times(2)->with('model')->andReturn($callback); + $container->instance(BindingRegistrar::class, $binder); + $callback = function ($user, $model) { + // + }; + $parameters = $this->broadcaster->extractAuthParameters('something.{model}', 'something.1', $callback); + $this->assertEquals(['model.1.instance'], $parameters); + Container::setInstance(new Container); + } + public function testUnknownChannelAuthHandlerTypeThrowsException() { $this->expectException(Exception::class); From b4bc4abf70bf607ee1b5b5106a3a5a988b4c0e72 Mon Sep 17 00:00:00 2001 From: Christian Charzewski Date: Fri, 26 Jul 2024 07:49:12 +0200 Subject: [PATCH 2/2] Apply style ci errors --- tests/Broadcasting/BroadcasterTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Broadcasting/BroadcasterTest.php b/tests/Broadcasting/BroadcasterTest.php index 1991f7ceed3c..91e64d4d4323 100644 --- a/tests/Broadcasting/BroadcasterTest.php +++ b/tests/Broadcasting/BroadcasterTest.php @@ -91,7 +91,7 @@ public function testModelRouteBinding() Container::setInstance($container); $binder = m::mock(BindingRegistrar::class); $callback = RouteBinding::forModel($container, BroadcasterTestEloquentModelStub::class); - + $binder->shouldReceive('getBindingCallback')->times(2)->with('model')->andReturn($callback); $container->instance(BindingRegistrar::class, $binder); $callback = function ($user, $model) { @@ -99,7 +99,7 @@ public function testModelRouteBinding() }; $parameters = $this->broadcaster->extractAuthParameters('something.{model}', 'something.1', $callback); $this->assertEquals(['model.1.instance'], $parameters); - Container::setInstance(new Container); + Container::setInstance(new Container); } public function testUnknownChannelAuthHandlerTypeThrowsException()