Skip to content

Commit 37508a3

Browse files
committed
Finish up unit tests for base controllers
1 parent 4203cdc commit 37508a3

File tree

7 files changed

+508
-41
lines changed

7 files changed

+508
-41
lines changed

app/Http/Controllers/Base/SecurityController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ class SecurityController extends Controller
6767
*/
6868
protected $twoFactorSetupService;
6969

70+
/**
71+
* SecurityController constructor.
72+
*
73+
* @param \Prologue\Alerts\AlertsMessageBag $alert
74+
* @param \Illuminate\Contracts\Config\Repository $config
75+
* @param \Illuminate\Contracts\Session\Session $session
76+
* @param \Pterodactyl\Contracts\Repository\SessionRepositoryInterface $repository
77+
* @param \Pterodactyl\Services\Users\ToggleTwoFactorService $toggleTwoFactorService
78+
* @param \Pterodactyl\Services\Users\TwoFactorSetupService $twoFactorSetupService
79+
*/
7080
public function __construct(
7181
AlertsMessageBag $alert,
7282
ConfigRepository $config,
@@ -120,6 +130,9 @@ public function generateTotp(Request $request)
120130
*
121131
* @param \Illuminate\Http\Request $request
122132
* @return \Illuminate\Http\Response
133+
*
134+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
135+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
123136
*/
124137
public function setTotp(Request $request)
125138
{
@@ -137,6 +150,9 @@ public function setTotp(Request $request)
137150
*
138151
* @param \Illuminate\Http\Request $request
139152
* @return \Illuminate\Http\RedirectResponse
153+
*
154+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
155+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
140156
*/
141157
public function disableTotp(Request $request)
142158
{

tests/Assertions/ControllerAssertionsTrait.php

Lines changed: 103 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,84 @@
2424

2525
namespace Tests\Assertions;
2626

27+
use Illuminate\Http\JsonResponse;
28+
use Illuminate\Http\Response;
2729
use Illuminate\View\View;
2830
use PHPUnit_Framework_Assert;
2931
use Illuminate\Http\RedirectResponse;
3032

3133
trait ControllerAssertionsTrait
3234
{
35+
/**
36+
* Assert that a response is an instance of Illuminate View.
37+
*
38+
* @param mixed $response
39+
*/
40+
public function assertIsViewResponse($response)
41+
{
42+
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $response);
43+
}
44+
45+
/**
46+
* Assert that a response is an instance of Illuminate Redirect Response.
47+
*
48+
* @param mixed $response
49+
*/
50+
public function assertIsRedirectResponse($response)
51+
{
52+
PHPUnit_Framework_Assert::assertInstanceOf(RedirectResponse::class, $response);
53+
}
54+
55+
/**
56+
* Assert that a response is an instance of Illuminate Json Response.
57+
*
58+
* @param mixed $response
59+
*/
60+
public function assertIsJsonResponse($response)
61+
{
62+
PHPUnit_Framework_Assert::assertInstanceOf(JsonResponse::class, $response);
63+
}
64+
65+
/**
66+
* Assert that a response is an instance of Illuminate Response.
67+
*
68+
* @param mixed $response
69+
*/
70+
public function assertIsResponse($response)
71+
{
72+
PHPUnit_Framework_Assert::assertInstanceOf(Response::class, $response);
73+
}
74+
3375
/**
3476
* Assert that a view name equals the passed name.
3577
*
36-
* @param string $name
37-
* @param \Illuminate\View\View $view
78+
* @param string $name
79+
* @param mixed $view
3880
*/
3981
public function assertViewNameEquals($name, $view)
4082
{
41-
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
4283
PHPUnit_Framework_Assert::assertEquals($name, $view->getName());
4384
}
4485

4586
/**
4687
* Assert that a view name does not equal a provided name.
4788
*
48-
* @param string $name
49-
* @param \Illuminate\View\View $view
89+
* @param string $name
90+
* @param mixed $view
5091
*/
5192
public function assertViewNameNotEquals($name, $view)
5293
{
53-
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
5494
PHPUnit_Framework_Assert::assertNotEquals($name, $view->getName());
5595
}
5696

5797
/**
5898
* Assert that a view has an attribute passed into it.
5999
*
60-
* @param string $attribute
61-
* @param \Illuminate\View\View $view
100+
* @param string $attribute
101+
* @param mixed $view
62102
*/
63103
public function assertViewHasKey($attribute, $view)
64104
{
65-
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
66-
67105
if (str_contains($attribute, '.')) {
68106
PHPUnit_Framework_Assert::assertNotEquals(
69107
'__TEST__FAIL',
@@ -77,13 +115,11 @@ public function assertViewHasKey($attribute, $view)
77115
/**
78116
* Assert that a view does not have a specific attribute passed in.
79117
*
80-
* @param string $attribute
81-
* @param \Illuminate\View\View $view
118+
* @param string $attribute
119+
* @param mixed $view
82120
*/
83121
public function assertViewNotHasKey($attribute, $view)
84122
{
85-
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
86-
87123
if (str_contains($attribute, '.')) {
88124
PHPUnit_Framework_Assert::assertEquals(
89125
'__TEST__PASS',
@@ -97,36 +133,78 @@ public function assertViewNotHasKey($attribute, $view)
97133
/**
98134
* Assert that a view attribute equals a given parameter.
99135
*
100-
* @param string $attribute
101-
* @param mixed $value
102-
* @param \Illuminate\View\View $view
136+
* @param string $attribute
137+
* @param mixed $value
138+
* @param mixed $view
103139
*/
104140
public function assertViewKeyEquals($attribute, $value, $view)
105141
{
106-
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
107142
PHPUnit_Framework_Assert::assertEquals($value, array_get($view->getData(), $attribute, '__TEST__FAIL'));
108143
}
109144

110145
/**
111146
* Assert that a view attribute does not equal a given parameter.
112147
*
113-
* @param string $attribute
114-
* @param mixed $value
115-
* @param \Illuminate\View\View $view
148+
* @param string $attribute
149+
* @param mixed $value
150+
* @param mixed $view
116151
*/
117152
public function assertViewKeyNotEquals($attribute, $value, $view)
118153
{
119-
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
120154
PHPUnit_Framework_Assert::assertNotEquals($value, array_get($view->getData(), $attribute, '__TEST__FAIL'));
121155
}
122156

123157
/**
124-
* @param string $route
125-
* @param \Illuminate\Http\RedirectResponse $response
158+
* Assert that a route redirect equals a given route name.
159+
*
160+
* @param string $route
161+
* @param mixed $response
126162
*/
127163
public function assertRouteRedirectEquals($route, $response)
128164
{
129-
PHPUnit_Framework_Assert::assertInstanceOf(RedirectResponse::class, $response);
130165
PHPUnit_Framework_Assert::assertEquals(route($route), $response->getTargetUrl());
131166
}
167+
168+
/**
169+
* Assert that a response code equals a given code.
170+
*
171+
* @param int $code
172+
* @param mixed $response
173+
*/
174+
public function assertResponseCodeEquals($code, $response)
175+
{
176+
PHPUnit_Framework_Assert::assertEquals($code, $response->getStatusCode());
177+
}
178+
179+
/**
180+
* Assert that a response code does not equal a given code.
181+
*
182+
* @param int $code
183+
* @param mixed $response
184+
*/
185+
public function assertResponseCodeNotEquals($code, $response)
186+
{
187+
PHPUnit_Framework_Assert::assertNotEquals($code, $response->getStatusCode());
188+
}
189+
190+
/**
191+
* Assert that a response is in a JSON format.
192+
*
193+
* @param mixed $response
194+
*/
195+
public function assertResponseHasJsonHeaders($response)
196+
{
197+
PHPUnit_Framework_Assert::assertEquals('application/json', $response->headers->get('content-type'));
198+
}
199+
200+
/**
201+
* Assert that response JSON matches a given JSON string.
202+
*
203+
* @param array|string $json
204+
* @param mixed $response
205+
*/
206+
public function assertResponseJsonEquals($json, $response)
207+
{
208+
PHPUnit_Framework_Assert::assertEquals(is_array($json) ? json_encode($json) : $json, $response->getContent());
209+
}
132210
}

tests/Unit/Http/Controllers/Admin/DatabaseControllerTest.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ public function testIndexController()
9090
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
9191
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn('getWithViewDetails');
9292

93-
$view = $this->controller->index();
94-
95-
$this->assertViewNameEquals('admin.databases.index', $view);
96-
$this->assertViewHasKey('locations', $view);
97-
$this->assertViewHasKey('hosts', $view);
98-
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $view);
99-
$this->assertViewKeyEquals('hosts', 'getWithViewDetails', $view);
93+
$response = $this->controller->index();
94+
95+
$this->assertIsViewResponse($response);
96+
$this->assertViewNameEquals('admin.databases.index', $response);
97+
$this->assertViewHasKey('locations', $response);
98+
$this->assertViewHasKey('hosts', $response);
99+
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $response);
100+
$this->assertViewKeyEquals('hosts', 'getWithViewDetails', $response);
100101
}
101102

102103
/**
@@ -107,12 +108,13 @@ public function testViewController()
107108
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
108109
$this->repository->shouldReceive('getWithServers')->with(1)->once()->andReturn('getWithServers');
109110

110-
$view = $this->controller->view(1);
111+
$response = $this->controller->view(1);
111112

112-
$this->assertViewNameEquals('admin.databases.view', $view);
113-
$this->assertViewHasKey('locations', $view);
114-
$this->assertViewHasKey('host', $view);
115-
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $view);
116-
$this->assertViewKeyEquals('host', 'getWithServers', $view);
113+
$this->assertIsViewResponse($response);
114+
$this->assertViewNameEquals('admin.databases.view', $response);
115+
$this->assertViewHasKey('locations', $response);
116+
$this->assertViewHasKey('host', $response);
117+
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $response);
118+
$this->assertViewKeyEquals('host', 'getWithServers', $response);
117119
}
118120
}

tests/Unit/Http/Controllers/Base/APIControllerTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Tests\TestCase;
2929
use Illuminate\Http\Request;
3030
use Pterodactyl\Models\User;
31-
use Illuminate\Http\Response;
3231
use Prologue\Alerts\AlertsMessageBag;
3332
use Tests\Assertions\ControllerAssertionsTrait;
3433
use Pterodactyl\Services\Api\KeyCreationService;
@@ -91,6 +90,7 @@ public function testIndexController()
9190
$this->repository->shouldReceive('findWhere')->with([['user_id', '=', $model->id]])->once()->andReturn(['testkeys']);
9291

9392
$response = $this->controller->index($this->request);
93+
$this->assertIsViewResponse($response);
9494
$this->assertViewNameEquals('base.api.index', $response);
9595
$this->assertViewHasKey('keys', $response);
9696
$this->assertViewKeyEquals('keys', ['testkeys'], $response);
@@ -107,6 +107,7 @@ public function testCreateController($admin)
107107
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model);
108108

109109
$response = $this->controller->create($this->request);
110+
$this->assertIsViewResponse($response);
110111
$this->assertViewNameEquals('base.api.new', $response);
111112
$this->assertViewHasKey('permissions.user', $response);
112113
$this->assertViewHasKey('permissions.admin', $response);
@@ -147,6 +148,7 @@ public function testStoreController($admin)
147148
->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
148149

149150
$response = $this->controller->store($this->request);
151+
$this->assertIsRedirectResponse($response);
150152
$this->assertRouteRedirectEquals('account.api', $response);
151153
}
152154

@@ -164,9 +166,9 @@ public function testRevokeController()
164166
])->once()->andReturnNull();
165167

166168
$response = $this->controller->revoke($this->request, 'testKey123');
167-
$this->assertInstanceOf(Response::class, $response);
169+
$this->assertIsResponse($response);
168170
$this->assertEmpty($response->getContent());
169-
$this->assertEquals(204, $response->getStatusCode());
171+
$this->assertResponseCodeEquals(204, $response);
170172
}
171173

172174
/**

tests/Unit/Http/Controllers/Base/AccountControllerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function testIndexController()
7777
{
7878
$response = $this->controller->index();
7979

80+
$this->assertIsViewResponse($response);
8081
$this->assertViewNameEquals('base.account', $response);
8182
}
8283

@@ -93,6 +94,7 @@ public function testUpdateControllerForPassword()
9394
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
9495

9596
$response = $this->controller->update($this->request);
97+
$this->assertIsRedirectResponse($response);
9698
$this->assertRouteRedirectEquals('account', $response);
9799
}
98100

@@ -109,6 +111,7 @@ public function testUpdateControllerForEmail()
109111
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
110112

111113
$response = $this->controller->update($this->request);
114+
$this->assertIsRedirectResponse($response);
112115
$this->assertRouteRedirectEquals('account', $response);
113116
}
114117

@@ -127,6 +130,7 @@ public function testUpdateControllerForIdentity()
127130
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
128131

129132
$response = $this->controller->update($this->request);
133+
$this->assertIsRedirectResponse($response);
130134
$this->assertRouteRedirectEquals('account', $response);
131135
}
132136
}

0 commit comments

Comments
 (0)