forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerAssertionsTrait.php
More file actions
207 lines (189 loc) · 5.37 KB
/
ControllerAssertionsTrait.php
File metadata and controls
207 lines (189 loc) · 5.37 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Assertions;
use Illuminate\View\View;
use Illuminate\Http\Response;
use PHPUnit\Framework\Assert;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
trait ControllerAssertionsTrait
{
/**
* Assert that a response is an instance of Illuminate View.
*
* @param mixed $response
*/
public function assertIsViewResponse($response)
{
Assert::assertInstanceOf(View::class, $response);
}
/**
* Assert that a response is an instance of Illuminate Redirect Response.
*
* @param mixed $response
*/
public function assertIsRedirectResponse($response)
{
Assert::assertInstanceOf(RedirectResponse::class, $response);
}
/**
* Assert that a response is an instance of Illuminate Json Response.
*
* @param mixed $response
*/
public function assertIsJsonResponse($response)
{
Assert::assertInstanceOf(JsonResponse::class, $response);
}
/**
* Assert that a response is an instance of Illuminate Response.
*
* @param mixed $response
*/
public function assertIsResponse($response)
{
Assert::assertInstanceOf(Response::class, $response);
}
/**
* Assert that a view name equals the passed name.
*
* @param string $name
* @param mixed $view
*/
public function assertViewNameEquals($name, $view)
{
Assert::assertEquals($name, $view->getName());
}
/**
* Assert that a view name does not equal a provided name.
*
* @param string $name
* @param mixed $view
*/
public function assertViewNameNotEquals($name, $view)
{
Assert::assertNotEquals($name, $view->getName());
}
/**
* Assert that a view has an attribute passed into it.
*
* @param string $attribute
* @param mixed $view
*/
public function assertViewHasKey($attribute, $view)
{
if (str_contains($attribute, '.')) {
Assert::assertNotEquals(
'__TEST__FAIL',
array_get($view->getData(), $attribute, '__TEST__FAIL')
);
} else {
Assert::assertArrayHasKey($attribute, $view->getData());
}
}
/**
* Assert that a view does not have a specific attribute passed in.
*
* @param string $attribute
* @param mixed $view
*/
public function assertViewNotHasKey($attribute, $view)
{
if (str_contains($attribute, '.')) {
Assert::assertEquals(
'__TEST__PASS',
array_get($view->getData(), $attribute, '__TEST__PASS')
);
} else {
Assert::assertArrayNotHasKey($attribute, $view->getData());
}
}
/**
* Assert that a view attribute equals a given parameter.
*
* @param string $attribute
* @param mixed $value
* @param mixed $view
*/
public function assertViewKeyEquals($attribute, $value, $view)
{
Assert::assertEquals($value, array_get($view->getData(), $attribute, '__TEST__FAIL'));
}
/**
* Assert that a view attribute does not equal a given parameter.
*
* @param string $attribute
* @param mixed $value
* @param mixed $view
*/
public function assertViewKeyNotEquals($attribute, $value, $view)
{
Assert::assertNotEquals($value, array_get($view->getData(), $attribute, '__TEST__FAIL'));
}
/**
* Assert that a route redirect equals a given route name.
*
* @param string $route
* @param mixed $response
* @param array $args
*/
public function assertRedirectRouteEquals($route, $response, array $args = [])
{
Assert::assertEquals(route($route, $args), $response->getTargetUrl());
}
/**
* Assert that a route redirect URL equals as passed URL.
*
* @param string $url
* @param mixed $response
*/
public function assertRedirectUrlEquals($url, $response)
{
Assert::assertEquals($url, $response->getTargetUrl());
}
/**
* Assert that a response code equals a given code.
*
* @param int $code
* @param mixed $response
*/
public function assertResponseCodeEquals($code, $response)
{
Assert::assertEquals($code, $response->getStatusCode());
}
/**
* Assert that a response code does not equal a given code.
*
* @param int $code
* @param mixed $response
*/
public function assertResponseCodeNotEquals($code, $response)
{
Assert::assertNotEquals($code, $response->getStatusCode());
}
/**
* Assert that a response is in a JSON format.
*
* @param mixed $response
*/
public function assertResponseHasJsonHeaders($response)
{
Assert::assertEquals('application/json', $response->headers->get('content-type'));
}
/**
* Assert that response JSON matches a given JSON string.
*
* @param array|string $json
* @param mixed $response
*/
public function assertResponseJsonEquals($json, $response)
{
Assert::assertEquals(is_array($json) ? json_encode($json) : $json, $response->getContent());
}
}