Update a batch of failing tests

This commit is contained in:
Dane Everitt 2017-10-07 23:29:08 -05:00
parent c19c423568
commit 787346525b
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
13 changed files with 218 additions and 195 deletions

View file

@ -0,0 +1,76 @@
<?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\Unit\Services\Services;
use Mockery as m;
use Tests\TestCase;
use Ramsey\Uuid\Uuid;
use Tests\Traits\KnownUuid;
use Pterodactyl\Models\Nest;
use Illuminate\Contracts\Config\Repository;
use Pterodactyl\Services\Nests\NestCreationService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
class NestCreationServiceTest extends TestCase
{
use KnownUuid;
/**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/
protected $config;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Nests\NestCreationService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->config = m::mock(Repository::class);
$this->repository = m::mock(NestRepositoryInterface::class);
$this->service = new NestCreationService($this->config, $this->repository);
}
/**
* Test that a new service can be created using the correct data.
*/
public function testCreateNewService()
{
$model = factory(Nest::class)->make();
$data = [
'name' => $model->name,
'description' => $model->description,
];
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('testauthor@example.com');
$this->repository->shouldReceive('create')->with([
'uuid' => $this->getKnownUuid(),
'author' => 'testauthor@example.com',
'name' => $data['name'],
'description' => $data['description'],
], true, true)->once()->andReturn($model);
$response = $this->service->handle($data);
$this->assertInstanceOf(Nest::class, $response);
$this->assertEquals($model, $response);
}
}

View file

@ -0,0 +1,92 @@
<?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\Unit\Services\Services;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Nests\NestDeletionService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class NestDeletionServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/
protected $serverRepository;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Nests\NestDeletionService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->repository = m::mock(NestRepositoryInterface::class);
$this->service = new NestDeletionService($this->serverRepository, $this->repository);
}
/**
* Test that a service is deleted when there are no servers attached to a service.
*/
public function testServiceIsDeleted()
{
$this->serverRepository->shouldReceive('findCountWhere')->with([['nest_id', '=', 1]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1);
$this->assertEquals(1, $this->service->handle(1));
}
/**
* Test that an exception is thrown when there are servers attached to a service.
*
* @dataProvider serverCountProvider
*
* @param int $count
*/
public function testExceptionIsThrownIfServersAreAttached(int $count)
{
$this->serverRepository->shouldReceive('findCountWhere')->with([['nest_id', '=', 1]])->once()->andReturn($count);
try {
$this->service->handle(1);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(HasActiveServersException::class, $exception);
$this->assertEquals(trans('exceptions.service.delete_has_servers'), $exception->getMessage());
}
}
/**
* Provide assorted server counts to ensure that an exception is always thrown when more than 0 servers are found.
*
* @return array
*/
public function serverCountProvider()
{
return [
[1], [2], [5], [10],
];
}
}

View file

@ -0,0 +1,62 @@
<?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\Unit\Services\Services;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Services\Nests\NestUpdateService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
class NestUpdateServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Nests\NestUpdateService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->repository = m::mock(NestRepositoryInterface::class);
$this->service = new NestUpdateService($this->repository);
}
/**
* Test that the author key is removed from the data array before updating the record.
*/
public function testAuthorArrayKeyIsRemovedIfPassed()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with(1, ['otherfield' => 'value'])->once()->andReturnNull();
$this->service->handle(1, ['author' => 'author1', 'otherfield' => 'value']);
}
/**
* Test that the function continues to work when no author key is passed.
*/
public function testServiceIsUpdatedWhenNoAuthorKeyIsPassed()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with(1, ['otherfield' => 'value'])->once()->andReturnNull();
$this->service->handle(1, ['otherfield' => 'value']);
}
}