forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestMockHelpers.php
More file actions
73 lines (62 loc) · 1.84 KB
/
RequestMockHelpers.php
File metadata and controls
73 lines (62 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Tests\Traits\Http;
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();
}
/**
* 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('First argument passed to buildRequestMock must be an instance of \Illuminate\Http\Request when mocked.');
}
$this->request->attributes = new ParameterBag();
}
/**
* Set a request attribute on the mock object.
*
* @param string $attribute
* @param mixed $value
*/
protected function setRequestAttribute(string $attribute, $value)
{
$this->request->attributes->set($attribute, $value);
}
/**
* 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
*/
protected function setRequestUser(User $user = null): User
{
$user = $user instanceof User ? $user : factory(User::class)->make();
$this->request->shouldReceive('user')->withNoArgs()->andReturn($user);
return $user;
}
}