-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[11.x] Support soft deleted models when using explicit route model bi…
…nding (#51651) * Added support for soft deleted models using explicit binding. * Fixed use statement order. * Update RouteBinding.php --------- Co-authored-by: Graham Bradley <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
3672fbe
commit 4ebbabe
Showing
2 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Routing; | ||
|
||
use Illuminate\Container\Container; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Routing\RouteBinding; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RouteBindingTest extends TestCase | ||
{ | ||
public function test_it_can_resolve_the_explicit_model_for_the_given_route() | ||
{ | ||
$container = Container::getInstance(); | ||
|
||
$route = new Route('GET', '/users/{user}', function () { | ||
}); | ||
|
||
$callback = RouteBinding::forModel($container, ExplicitRouteBindingUser::class); | ||
$this->assertInstanceOf(ExplicitRouteBindingUser::class, $callback(1, $route)); | ||
} | ||
|
||
public function test_it_cannot_resolve_the_explicit_soft_deleted_model_for_the_given_route() | ||
{ | ||
$container = Container::getInstance(); | ||
|
||
$route = new Route('GET', '/users/{user}', function () { | ||
}); | ||
|
||
$callback = RouteBinding::forModel($container, ExplicitRouteBindingSoftDeletableUser::class); | ||
|
||
$this->expectException(ModelNotFoundException::class); | ||
$callback(1, $route); | ||
} | ||
|
||
public function test_it_can_resolve_the_explicit_soft_deleted_model_for_the_given_route_with_trashed() | ||
{ | ||
$container = Container::getInstance(); | ||
|
||
$route = (new Route('GET', '/users/{user}', function () { | ||
}))->withTrashed(); | ||
|
||
$callback = RouteBinding::forModel($container, ExplicitRouteBindingSoftDeletableUser::class); | ||
$this->assertInstanceOf(ExplicitRouteBindingSoftDeletableUser::class, $callback(1, $route)); | ||
} | ||
} | ||
|
||
class ExplicitRouteBindingUser extends Model | ||
{ | ||
public function resolveRouteBinding($value, $field = null) | ||
{ | ||
return new self(); | ||
} | ||
} | ||
|
||
class ExplicitRouteBindingSoftDeletableUser extends Model | ||
{ | ||
use SoftDeletes; | ||
|
||
public function resolveRouteBinding($value, $field = null) | ||
{ | ||
return null; | ||
} | ||
|
||
public function resolveSoftDeletableRouteBinding($value, $field = null) | ||
{ | ||
return new self(); | ||
} | ||
} |