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

[11.x] Fix explicit route binding for broadcast routes #52280

Merged
merged 2 commits into from
Jul 26, 2024
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
4 changes: 2 additions & 2 deletions src/Illuminate/Routing/RouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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';

Expand Down
18 changes: 18 additions & 0 deletions tests/Broadcasting/BroadcasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down