From 872fe0c8fe9b0e036332aef25cc98fb8a31d76f1 Mon Sep 17 00:00:00 2001 From: Martin Kluska Date: Tue, 5 Sep 2023 20:11:01 +0200 Subject: [PATCH] feat(Testing): CreateRequest now accept user parameter that is passed to request - request->user() now will return given user --- src/Testing/Concerns/CreateRequest.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Testing/Concerns/CreateRequest.php b/src/Testing/Concerns/CreateRequest.php index d8de196b..8c5505be 100644 --- a/src/Testing/Concerns/CreateRequest.php +++ b/src/Testing/Concerns/CreateRequest.php @@ -5,6 +5,7 @@ namespace LaraStrict\Testing\Concerns; use Closure; +use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Foundation\Http\FormRequest; @@ -82,7 +83,8 @@ protected function createFormRequest( array $cookies = [], array $files = [], array $server = [], - array $makeBindings = [] + array $makeBindings = [], + Authenticatable $user = null, ): Request { $symfonyRequest = SymfonyRequest::create( uri: 'https://testing', @@ -96,18 +98,22 @@ protected function createFormRequest( ], ); - $requestClass = $requestClass::createFromBase($symfonyRequest); + $request = $requestClass::createFromBase($symfonyRequest); - $requestClass->setContainer( + $request->setContainer( new TestingContainer( makeBindings: $makeBindings, makeAlwaysBinding: static fn (array $parameters, string $class) => new $class(...$parameters) ) ); - $requestClass->setValidator(new ValidatorMock($data)); - $requestClass->validateResolved(); + if ($user !== null) { + $request->setUserResolver(static fn () => $user); + } - return $requestClass; + $request->setValidator(new ValidatorMock($data)); + $request->validateResolved(); + + return $request; } }