2017-11-03 21:43:28 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
namespace Pterodactyl\Tests\Traits\Http;
|
2017-11-03 21:43:28 +00:00
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
|
|
|
|
|
trait RequestMockHelpers
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $requestMockClass = Request::class;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Http\Request|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the class to mock for requests.
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
*/
|
|
|
|
public function setRequestMockClass(string $class)
|
|
|
|
{
|
|
|
|
$this->requestMockClass = $class;
|
|
|
|
|
|
|
|
$this->buildRequestMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-15 03:05:26 +00:00
|
|
|
* Configure the user model that the request mock should return with.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\User|null $user
|
2017-11-03 21:43:28 +00:00
|
|
|
*/
|
2017-12-15 03:05:26 +00:00
|
|
|
public function setRequestUserModel(User $user = null)
|
2017-11-03 21:43:28 +00:00
|
|
|
{
|
2017-12-15 03:05:26 +00:00
|
|
|
$this->request->shouldReceive('user')->andReturn($user);
|
|
|
|
}
|
2017-11-03 21:43:28 +00:00
|
|
|
|
2017-12-15 03:05:26 +00:00
|
|
|
/**
|
|
|
|
* Generates a new request user model and also returns the generated model.
|
|
|
|
*
|
2018-01-05 04:49:50 +00:00
|
|
|
* @param array $args
|
2017-12-15 03:05:26 +00:00
|
|
|
* @return \Pterodactyl\Models\User
|
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function generateRequestUserModel(array $args = []): User
|
2017-12-15 03:05:26 +00:00
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->make($args);
|
2017-12-15 03:05:26 +00:00
|
|
|
$this->setRequestUserModel($user);
|
|
|
|
|
|
|
|
return $user;
|
2017-11-03 21:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a request attribute on the mock object.
|
|
|
|
*
|
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2017-12-15 03:05:26 +00:00
|
|
|
public function setRequestAttribute(string $attribute, $value)
|
2017-11-03 21:43:28 +00:00
|
|
|
{
|
|
|
|
$this->request->attributes->set($attribute, $value);
|
|
|
|
}
|
|
|
|
|
2017-12-15 03:05:26 +00:00
|
|
|
/**
|
|
|
|
* Set the request route name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function setRequestRouteName(string $name)
|
|
|
|
{
|
|
|
|
$this->request->shouldReceive('route->getName')->andReturn($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the active request object to be an instance of a mocked request.
|
|
|
|
*/
|
|
|
|
protected function buildRequestMock()
|
|
|
|
{
|
|
|
|
$this->request = m::mock($this->requestMockClass);
|
|
|
|
if (! $this->request instanceof Request) {
|
|
|
|
throw new InvalidArgumentException('Request mock class must be an instance of ' . Request::class . ' when mocked.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->request->attributes = new ParameterBag();
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
/**
|
|
|
|
* Sets the mocked request user. If a user model is not provided, a factory model
|
|
|
|
* will be created and returned.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\User|null $user
|
|
|
|
* @return \Pterodactyl\Models\User
|
2017-12-15 03:05:26 +00:00
|
|
|
* @deprecated
|
2017-11-03 21:43:28 +00:00
|
|
|
*/
|
|
|
|
protected function setRequestUser(User $user = null): User
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = $user instanceof User ? $user : User::factory()->make();
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->andReturn($user);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|