Path: blob/1.0-develop/tests/Traits/Http/RequestMockHelpers.php
7461 views
<?php12namespace Pterodactyl\Tests\Traits\Http;34use Mockery as m;5use Mockery\Mock;6use Illuminate\Http\Request;7use Pterodactyl\Models\User;8use Symfony\Component\HttpFoundation\ParameterBag;910trait RequestMockHelpers11{12private string $requestMockClass = Request::class;1314protected Request|Mock $request;1516/**17* Set the class to mock for requests.18*/19public function setRequestMockClass(string $class): void20{21$this->requestMockClass = $class;2223$this->buildRequestMock();24}2526/**27* Configure the user model that the request mock should return with.28*/29public function setRequestUserModel(?User $user = null): void30{31$this->request->shouldReceive('user')->andReturn($user);32}3334/**35* Generates a new request user model and also returns the generated model.36*/37public function generateRequestUserModel(array $args = []): User38{39/** @var User $user */40$user = User::factory()->make($args);41$this->setRequestUserModel($user);4243return $user;44}4546/**47* Set a request attribute on the mock object.48*/49public function setRequestAttribute(string $attribute, mixed $value): void50{51$this->request->attributes->set($attribute, $value);52}5354/**55* Set the request route name.56*/57public function setRequestRouteName(string $name): void58{59$this->request->shouldReceive('route->getName')->andReturn($name);60}6162/**63* Set the active request object to be an instance of a mocked request.64*/65protected function buildRequestMock(): void66{67$this->request = m::mock($this->requestMockClass);68if (!$this->request instanceof Request) {69throw new \InvalidArgumentException('Request mock class must be an instance of ' . Request::class . ' when mocked.');70}7172$this->request->attributes = new ParameterBag();73}7475/**76* Sets the mocked request user. If a user model is not provided, a factory model77* will be created and returned.78*79* @deprecated80*/81protected function setRequestUser(?User $user = null): User82{83$user = $user instanceof User ? $user : User::factory()->make();84$this->request->shouldReceive('user')->withNoArgs()->andReturn($user);8586return $user;87}88}899091