Skip to content

Commit 038e5e1

Browse files
committed
Add exception tests
1 parent 5f6ee45 commit 038e5e1

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99

1010
namespace Tests\Unit\Http\Controllers\Base;
1111

12+
use GuzzleHttp\Exception\ConnectException;
13+
use GuzzleHttp\Exception\RequestException;
14+
use GuzzleHttp\Psr7\ServerRequest;
1215
use Mockery as m;
1316
use Pterodactyl\Models\User;
1417
use GuzzleHttp\Psr7\Response;
1518
use Pterodactyl\Models\Server;
19+
use Symfony\Component\HttpKernel\Exception\HttpException;
1620
use Tests\Assertions\ControllerAssertionsTrait;
1721
use Tests\Unit\Http\Controllers\ControllerTestCase;
1822
use Pterodactyl\Http\Controllers\Base\IndexController;
1923
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
2024
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
2125
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
2226
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
27+
use Psr\Http\Message\RequestInterface;
2328

2429
class IndexControllerTest extends ControllerTestCase
2530
{
@@ -134,4 +139,44 @@ public function testStatusControllerWhenServerIsSuspended()
134139
$this->assertResponseCodeEquals(200, $response);
135140
$this->assertResponseJsonEquals(['status' => 30], $response);
136141
}
142+
143+
/**
144+
* Test the status controller when we can't connect to a server.
145+
*/
146+
public function testStatusControllerWithServerConnectionException()
147+
{
148+
$user = factory(User::class)->make();
149+
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
150+
151+
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
152+
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
153+
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
154+
155+
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
156+
->shouldReceive('setToken')->with('test123')->once()->andReturnSelf()
157+
->shouldReceive('details')->withNoArgs()->once()->andThrow(new ConnectException('bad connection', new ServerRequest('', '')));
158+
159+
$this->expectExceptionObject(new HttpException(500, 'bad connection'));
160+
$this->controller->status($this->request, $server->uuidShort);
161+
}
162+
163+
/**
164+
* Test the status controller when we can't connect to a server.
165+
*/
166+
public function testStatusControllerWithRequestException()
167+
{
168+
$user = factory(User::class)->make();
169+
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
170+
171+
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
172+
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
173+
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
174+
175+
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
176+
->shouldReceive('setToken')->with('test123')->once()->andReturnSelf()
177+
->shouldReceive('details')->withNoArgs()->once()->andThrow(new RequestException('bad request', new ServerRequest('', '')));
178+
179+
$this->expectExceptionObject(new HttpException(500, 'bad request'));
180+
$this->controller->status($this->request, $server->uuidShort);
181+
}
137182
}

0 commit comments

Comments
 (0)