Files

106 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2017-11-03 16:43:28 -05:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2021-01-23 12:09:16 -08:00
namespace Pterodactyl\Tests\Traits\Http;
2017-11-03 16:43:28 -05:00
use Illuminate\Http\Request;
2026-10-05 18:10:18 -04:00
use Illuminate\Routing\Route;
use InvalidArgumentException;
2017-11-03 16:43:28 -05:00
use Pterodactyl\Models\User;
trait RequestMockHelpers
{
2026-10-05 18:10:18 -04:00
protected Request $request;
2017-11-03 16:43:28 -05:00
2026-10-05 18:10:18 -04:00
private string $requestMockClass = Request::class;
2017-11-03 16:43:28 -05:00
/**
* Set the class to mock for requests.
*/
2022-10-14 10:59:20 -06:00
public function setRequestMockClass(string $class): void
2017-11-03 16:43:28 -05:00
{
$this->requestMockClass = $class;
$this->buildRequestMock();
}
/**
2026-10-05 18:10:18 -04:00
* Configure the user model that the request should return with.
2017-11-03 16:43:28 -05:00
*/
2024-10-21 19:18:20 -06:00
public function setRequestUserModel(?User $user = null): void
2017-11-03 16:43:28 -05:00
{
2026-10-05 18:10:18 -04:00
$this->request->setUserResolver(fn () => $user);
}
2017-11-03 16:43:28 -05:00
/**
* Generates a new request user model and also returns the generated model.
*/
public function generateRequestUserModel(array $args = []): User
{
2024-10-21 19:18:20 -06:00
/** @var User $user */
2021-01-23 12:09:16 -08:00
$user = User::factory()->make($args);
$this->setRequestUserModel($user);
return $user;
2017-11-03 16:43:28 -05:00
}
/**
* Set a request attribute on the mock object.
*/
2022-10-14 10:59:20 -06:00
public function setRequestAttribute(string $attribute, mixed $value): void
2017-11-03 16:43:28 -05:00
{
$this->request->attributes->set($attribute, $value);
}
/**
* Set the request route name.
*/
2022-10-14 10:59:20 -06:00
public function setRequestRouteName(string $name): void
{
2026-10-05 18:10:18 -04:00
$route = new Route(['GET'], '/', ['as' => $name, 'uses' => fn () => null]);
$this->request->setRouteResolver(fn () => $route);
}
/**
* Set the bearer token carried by the request's Authorization header.
*/
public function setRequestBearerToken(?string $token): void
{
if ($token === null) {
$this->request->headers->remove('Authorization');
return;
}
$this->request->headers->set('Authorization', 'Bearer '.$token);
}
/**
2026-10-05 18:10:18 -04:00
* Set the active request object to be a real request instance.
*/
2022-10-14 10:59:20 -06:00
protected function buildRequestMock(): void
{
2026-10-05 18:10:18 -04:00
if (! is_a($this->requestMockClass, Request::class, true)) {
throw new InvalidArgumentException('Request mock class must be an instance of '.Request::class.' when mocked.');
}
2026-10-05 18:10:18 -04:00
$class = $this->requestMockClass;
$this->request = $class::create('/', 'GET');
}
2017-11-03 16:43:28 -05:00
/**
* Sets the mocked request user. If a user model is not provided, a factory model
* will be created and returned.
*
* @deprecated
2017-11-03 16:43:28 -05:00
*/
2024-10-21 19:18:20 -06:00
protected function setRequestUser(?User $user = null): User
2017-11-03 16:43:28 -05:00
{
2021-01-23 12:09:16 -08:00
$user = $user instanceof User ? $user : User::factory()->make();
2026-10-05 18:10:18 -04:00
$this->setRequestUserModel($user);
2017-11-03 16:43:28 -05:00
return $user;
}
}