Fix failing tests

This commit is contained in:
Dane Everitt 2017-10-03 23:54:24 -05:00
parent 8952043600
commit 29ac1662b6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 73 additions and 18 deletions

View file

@ -12,7 +12,9 @@ namespace Tests\Unit\Services\Services\Options;
use Exception; use Exception;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\ServiceOption; use Pterodactyl\Models\ServiceOption;
use Illuminate\Contracts\Config\Repository;
use Pterodactyl\Services\Services\Options\OptionCreationService; use Pterodactyl\Services\Services\Options\OptionCreationService;
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface; use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
use Pterodactyl\Exceptions\Service\ServiceOption\NoParentConfigurationFoundException; use Pterodactyl\Exceptions\Service\ServiceOption\NoParentConfigurationFoundException;
@ -20,12 +22,12 @@ use Pterodactyl\Exceptions\Service\ServiceOption\NoParentConfigurationFoundExcep
class OptionCreationServiceTest extends TestCase class OptionCreationServiceTest extends TestCase
{ {
/** /**
* @var \Pterodactyl\Models\ServiceOption * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/ */
protected $model; protected $config;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface * @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
@ -34,6 +36,11 @@ class OptionCreationServiceTest extends TestCase
*/ */
protected $service; protected $service;
/**
* @var \Ramsey\Uuid\Uuid|\Mockery\Mock
*/
protected $uuid;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -41,10 +48,11 @@ class OptionCreationServiceTest extends TestCase
{ {
parent::setUp(); parent::setUp();
$this->model = factory(ServiceOption::class)->make(); $this->config = m::mock(Repository::class);
$this->repository = m::mock(ServiceOptionRepositoryInterface::class); $this->repository = m::mock(ServiceOptionRepositoryInterface::class);
$this->uuid = m::mock('overload:' . Uuid::class);
$this->service = new OptionCreationService($this->repository); $this->service = new OptionCreationService($this->config, $this->repository);
} }
/** /**
@ -52,14 +60,48 @@ class OptionCreationServiceTest extends TestCase
*/ */
public function testCreateNewModelWithoutUsingConfigFrom() public function testCreateNewModelWithoutUsingConfigFrom()
{ {
$this->repository->shouldReceive('create')->with(['name' => $this->model->name, 'config_from' => null]) $model = factory(ServiceOption::class)->make();
->once()->andReturn($this->model);
$response = $this->service->handle(['name' => $this->model->name]); $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
$this->repository->shouldReceive('create')->with([
'name' => $model->name,
'config_from' => null,
'tag' => 'test@example.com:' . $model->tag,
'uuid' => 'uuid-string',
], true, true)->once()->andReturn($model);
$response = $this->service->handle(['name' => $model->name, 'tag' => $model->tag]);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$this->assertNull(object_get($response, 'config_from')); $this->assertNull(object_get($response, 'config_from'));
$this->assertEquals($this->model->name, $response->name); $this->assertEquals($model->name, $response->name);
}
/**
* Test that passing a bad tag into the function will set the correct tag.
*/
public function testCreateNewModelUsingLongTagForm()
{
$model = factory(ServiceOption::class)->make([
'tag' => 'test@example.com:tag',
]);
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
$this->repository->shouldReceive('create')->with([
'name' => $model->name,
'config_from' => null,
'tag' => $model->tag,
'uuid' => 'uuid-string',
], true, true)->once()->andReturn($model);
$response = $this->service->handle(['name' => $model->name, 'tag' => 'bad@example.com:tag']);
$this->assertNotEmpty($response);
$this->assertNull(object_get($response, 'config_from'));
$this->assertEquals($model->name, $response->name);
$this->assertEquals('test@example.com:tag', $response->tag);
} }
/** /**
@ -67,10 +109,14 @@ class OptionCreationServiceTest extends TestCase
*/ */
public function testCreateNewModelUsingConfigFrom() public function testCreateNewModelUsingConfigFrom()
{ {
$model = factory(ServiceOption::class)->make();
$data = [ $data = [
'name' => $this->model->name, 'name' => $model->name,
'service_id' => $this->model->service_id, 'service_id' => $model->service_id,
'tag' => 'test@example.com:tag',
'config_from' => 1, 'config_from' => 1,
'uuid' => 'uuid-string',
]; ];
$this->repository->shouldReceive('findCountWhere')->with([ $this->repository->shouldReceive('findCountWhere')->with([
@ -78,13 +124,14 @@ class OptionCreationServiceTest extends TestCase
['id', '=', $data['config_from']], ['id', '=', $data['config_from']],
])->once()->andReturn(1); ])->once()->andReturn(1);
$this->repository->shouldReceive('create')->with($data) $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
->once()->andReturn($this->model); $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
$this->repository->shouldReceive('create')->with($data, true, true)->once()->andReturn($model);
$response = $this->service->handle($data); $response = $this->service->handle($data);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$this->assertEquals($response, $this->model); $this->assertEquals($response, $model);
} }
/** /**

View file

@ -11,6 +11,7 @@ namespace Tests\Unit\Services\Services;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\Service; use Pterodactyl\Models\Service;
use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Config\Repository;
use Pterodactyl\Traits\Services\CreatesServiceIndex; use Pterodactyl\Traits\Services\CreatesServiceIndex;
@ -22,12 +23,12 @@ class ServiceCreationServiceTest extends TestCase
use CreatesServiceIndex; use CreatesServiceIndex;
/** /**
* @var \Illuminate\Contracts\Config\Repository * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/ */
protected $config; protected $config;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface * @var \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
@ -36,6 +37,11 @@ class ServiceCreationServiceTest extends TestCase
*/ */
protected $service; protected $service;
/**
* @var \Ramsey\Uuid\Uuid|\Mockery\Mock
*/
protected $uuid;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -45,6 +51,7 @@ class ServiceCreationServiceTest extends TestCase
$this->config = m::mock(Repository::class); $this->config = m::mock(Repository::class);
$this->repository = m::mock(ServiceRepositoryInterface::class); $this->repository = m::mock(ServiceRepositoryInterface::class);
$this->uuid = m::mock('overload:' . Uuid::class);
$this->service = new ServiceCreationService($this->config, $this->repository); $this->service = new ServiceCreationService($this->config, $this->repository);
} }
@ -62,15 +69,16 @@ class ServiceCreationServiceTest extends TestCase
'startup' => $model->startup, 'startup' => $model->startup,
]; ];
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000');
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('0000-author'); $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('0000-author');
$this->repository->shouldReceive('create')->with([ $this->repository->shouldReceive('create')->with([
'uuid' => 'uuid-0000',
'author' => '0000-author', 'author' => '0000-author',
'name' => $data['name'], 'name' => $data['name'],
'description' => $data['description'], 'description' => $data['description'],
'folder' => $data['folder'],
'startup' => $data['startup'], 'startup' => $data['startup'],
'index_file' => $this->getIndexScript(), 'index_file' => $this->getIndexScript(),
])->once()->andReturn($model); ], true, true)->once()->andReturn($model);
$response = $this->service->handle($data); $response = $this->service->handle($data);
$this->assertInstanceOf(Service::class, $response); $this->assertInstanceOf(Service::class, $response);