Skip to content

Commit

Permalink
Improve types for screen testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Jan 26, 2025
1 parent 48445b0 commit 65df2b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
35 changes: 16 additions & 19 deletions src/Support/Testing/DynamicTestScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ class DynamicTestScreen
/**
* @var MakesHttpRequestsWrapper Wrapper for HTTP requests
*/
protected $http;
protected MakesHttpRequestsWrapper $http;

/**
* Name of the route
*
* @var string
*/
protected $name;
protected string $name;

/**
* Route parameters
*
* @var array
*/
protected $parameters = [];
protected array $parameters = [];

/**
* Session data
*
* @var array
*/
protected $session = [];
protected array $session = [];

/**
* Indicates whether redirects should be followed.
Expand All @@ -62,7 +62,7 @@ public function __construct(?string $name = null)
* @param string|null $route Route name
* @param array|string $middleware Middleware to be used
*/
public function register(string $screen, ?string $route = null, $middleware = 'web'): DynamicTestScreen
public function register(string $screen, ?string $route = null, array|string $middleware = 'web'): DynamicTestScreen
{
Route::screen('/_test/'.($route ?? $this->name), $screen)
->middleware($middleware)
Expand Down Expand Up @@ -107,14 +107,14 @@ public function session(array $data): DynamicTestScreen
*
* @param array $headers Headers to be used
*
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\Container\ContainerExceptionInterface
*
* @return \Illuminate\Testing\TestResponse|mixed
* @return \Illuminate\Testing\TestResponse
*/
public function display(array $headers = [])
public function display(array $headers = []): TestResponse
{
return $this->http->get($this->route(), $headers);
return $this->http
->when($this->followRedirects, fn($http) => $http->followingRedirects())
->withSession($this->session)
->get($this->route(), $headers);
}

/**
Expand All @@ -135,13 +135,10 @@ public function method(string $method, array $parameters = [], array $headers =

$this->from($route);

$http = $this->http->withSession($this->session);

if ($this->followRedirects) {
$http->followingRedirects();
}

return $http->post($route, $parameters, $headers);
return $this->http
->when($this->followRedirects, fn($http) => $http->followingRedirects())
->withSession($this->session)
->post($route, $parameters, $headers);
}

/**
Expand Down Expand Up @@ -179,7 +176,7 @@ public function actingAs(UserContract $user, $guard = null): self
* @param UserContract $user User to act as
* @param string|null $guard Guard name
*/
public function be(UserContract $user, $guard = null): self
public function be(UserContract $user, ?string $guard = null): self
{
$this->http->be($user, $guard);

Expand Down
17 changes: 4 additions & 13 deletions src/Support/Testing/MakesHttpRequestsWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling;
use Illuminate\Foundation\Testing\Concerns\InteractsWithSession;
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;
use Illuminate\Support\Traits\Conditionable;

/**
* Trait MakesHttpRequestsWrapper
Expand All @@ -19,24 +20,14 @@
*/
class MakesHttpRequestsWrapper
{
use InteractsWithAuthentication, InteractsWithExceptionHandling, InteractsWithSession, MakesHttpRequests;

/**
* The application instance
*
* @var Application
*/
protected $app;
use InteractsWithAuthentication, InteractsWithExceptionHandling, InteractsWithSession, MakesHttpRequests, Conditionable;

/**
* Creates a new wrapper instance.
*
* @param Application $app
* @param Application $app The application instance
*/
public function __construct(Application $app)
{
$this->app = $app;
}
public function __construct(protected Application $app){}

/**
* Get the instance of the application
Expand Down

0 comments on commit 65df2b4

Please sign in to comment.