misc_pterodactyl-panel/tests/Unit/Http/Controllers/Admin/DatabaseControllerTest.php

144 lines
4.9 KiB
PHP
Raw Normal View History

<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Http\Controllers\Admin;
use Mockery as m;
2017-08-16 04:21:01 +00:00
use Tests\TestCase;
use Pterodactyl\Models\DatabaseHost;
use Prologue\Alerts\AlertsMessageBag;
2018-03-04 00:24:40 +00:00
use Illuminate\Pagination\LengthAwarePaginator;
use Tests\Assertions\ControllerAssertionsTrait;
2017-08-16 04:21:01 +00:00
use Pterodactyl\Http\Controllers\Admin\DatabaseController;
2017-10-24 01:12:15 +00:00
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
2018-03-04 00:24:40 +00:00
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
2017-08-16 04:21:01 +00:00
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class DatabaseControllerTest extends TestCase
{
use ControllerAssertionsTrait;
/**
2017-10-24 01:12:15 +00:00
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
*/
2017-10-24 01:12:15 +00:00
private $alert;
/**
2017-10-24 01:12:15 +00:00
* @var \Pterodactyl\Services\Databases\Hosts\HostCreationService|\Mockery\Mock
*/
2017-10-24 01:12:15 +00:00
private $creationService;
2018-03-04 00:24:40 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
*/
private $databaseRepository;
/**
2017-10-24 01:12:15 +00:00
* @var \Pterodactyl\Services\Databases\Hosts\HostDeletionService|\Mockery\Mock
*/
2017-10-24 01:12:15 +00:00
private $deletionService;
/**
2017-10-24 01:12:15 +00:00
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface|\Mockery\Mock
*/
2017-10-24 01:12:15 +00:00
private $locationRepository;
/**
2017-10-24 01:12:15 +00:00
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock
*/
2017-10-24 01:12:15 +00:00
private $repository;
/**
* @var \Pterodactyl\Services\Databases\Hosts\HostUpdateService|\Mockery\Mock
*/
private $updateService;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->alert = m::mock(AlertsMessageBag::class);
2017-10-24 01:12:15 +00:00
$this->creationService = m::mock(HostCreationService::class);
2018-03-04 00:24:40 +00:00
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
2017-10-24 01:12:15 +00:00
$this->deletionService = m::mock(HostDeletionService::class);
$this->locationRepository = m::mock(LocationRepositoryInterface::class);
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
2017-10-24 01:12:15 +00:00
$this->updateService = m::mock(HostUpdateService::class);
}
/**
* Test the index controller.
*/
public function testIndexController()
{
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn(collect(['getAllWithNodes']));
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn(collect(['getWithViewDetails']));
2017-10-24 01:12:15 +00:00
$response = $this->getController()->index();
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('admin.databases.index', $response);
$this->assertViewHasKey('locations', $response);
$this->assertViewHasKey('hosts', $response);
$this->assertViewKeyEquals('locations', collect(['getAllWithNodes']), $response);
$this->assertViewKeyEquals('hosts', collect(['getWithViewDetails']), $response);
}
/**
* Test the view controller for displaying a specific database host.
*/
public function testViewController()
{
$model = factory(DatabaseHost::class)->make();
2018-03-04 00:24:40 +00:00
$paginator = new LengthAwarePaginator([], 1, 1);
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn(collect(['getAllWithNodes']));
2018-03-04 00:24:40 +00:00
$this->repository->shouldReceive('find')->with(1)->once()->andReturn($model);
$this->databaseRepository->shouldReceive('getDatabasesForHost')
->once()
->with(1)
->andReturn($paginator);
2017-10-24 01:12:15 +00:00
$response = $this->getController()->view(1);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('admin.databases.view', $response);
$this->assertViewHasKey('locations', $response);
$this->assertViewHasKey('host', $response);
2018-03-04 00:24:40 +00:00
$this->assertViewHasKey('databases', $response);
$this->assertViewKeyEquals('locations', collect(['getAllWithNodes']), $response);
$this->assertViewKeyEquals('host', $model, $response);
2018-03-04 00:24:40 +00:00
$this->assertViewKeyEquals('databases', $paginator, $response);
}
2017-10-24 01:12:15 +00:00
/**
* Return an instance of the DatabaseController with mock dependencies.
*
* @return \Pterodactyl\Http\Controllers\Admin\DatabaseController
*/
private function getController(): DatabaseController
{
return new DatabaseController(
$this->alert,
$this->repository,
2018-03-04 00:24:40 +00:00
$this->databaseRepository,
2017-10-24 01:12:15 +00:00
$this->creationService,
$this->deletionService,
$this->updateService,
$this->locationRepository
);
}
}