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

125 lines
4.1 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 Prologue\Alerts\AlertsMessageBag;
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;
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;
/**
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);
$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('getAllWithNodes');
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn('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', 'getAllWithNodes', $response);
$this->assertViewKeyEquals('hosts', 'getWithViewDetails', $response);
}
/**
* Test the view controller for displaying a specific database host.
*/
public function testViewController()
{
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
$this->repository->shouldReceive('getWithServers')->with(1)->once()->andReturn('getWithServers');
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);
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $response);
$this->assertViewKeyEquals('host', 'getWithServers', $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,
$this->creationService,
$this->deletionService,
$this->updateService,
$this->locationRepository
);
}
}