misc_pterodactyl-panel/tests/Unit/Services/Eggs/EggCreationServiceTest.php

147 lines
4.8 KiB
PHP
Raw Normal View History

2017-08-12 21:30:27 +00:00
<?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
2017-08-12 21:30:27 +00:00
*/
namespace Tests\Unit\Services\Services\Options;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
2017-10-08 20:29:46 +00:00
use Tests\Traits\MocksUuids;
2017-10-04 04:54:24 +00:00
use Illuminate\Contracts\Config\Repository;
2017-10-08 20:29:46 +00:00
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Eggs\EggCreationService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
2017-10-08 20:29:46 +00:00
use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException;
2017-08-12 21:30:27 +00:00
2017-10-08 20:29:46 +00:00
class EggCreationServiceTest extends TestCase
2017-08-12 21:30:27 +00:00
{
2017-10-08 20:29:46 +00:00
use MocksUuids;
2017-08-12 21:30:27 +00:00
/**
2017-10-04 04:54:24 +00:00
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2017-08-12 21:30:27 +00:00
*/
2017-10-04 04:54:24 +00:00
protected $config;
2017-08-12 21:30:27 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
2017-08-12 21:30:27 +00:00
*/
protected $repository;
/**
2017-10-08 20:29:46 +00:00
* @var \Pterodactyl\Services\Eggs\EggCreationService
2017-08-12 21:30:27 +00:00
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
2017-10-04 04:54:24 +00:00
$this->config = m::mock(Repository::class);
$this->repository = m::mock(EggRepositoryInterface::class);
2017-08-12 21:30:27 +00:00
$this->service = new EggCreationService($this->config, $this->repository);
2017-08-12 21:30:27 +00:00
}
/**
* Test that a new model is created when not using the config from attribute.
*/
public function testCreateNewModelWithoutUsingConfigFrom()
{
2017-10-08 20:29:46 +00:00
$model = factory(Egg::class)->make();
2017-10-04 04:54:24 +00:00
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
$this->repository->shouldReceive('create')->with([
2017-10-08 20:29:46 +00:00
'uuid' => $this->getKnownUuid(),
'author' => 'test@example.com',
2017-10-05 04:52:25 +00:00
'config_from' => null,
2017-10-08 20:29:46 +00:00
'name' => $model->name,
2017-10-04 04:54:24 +00:00
], true, true)->once()->andReturn($model);
2017-08-12 21:30:27 +00:00
2017-10-08 20:29:46 +00:00
$response = $this->service->handle(['name' => $model->name]);
2017-08-12 21:30:27 +00:00
$this->assertNotEmpty($response);
$this->assertNull(object_get($response, 'config_from'));
2017-10-04 04:54:24 +00:00
$this->assertEquals($model->name, $response->name);
}
/**
2017-10-08 20:29:46 +00:00
* Test that a new model is created when using the config from attribute.
2017-10-04 04:54:24 +00:00
*/
2017-10-08 20:29:46 +00:00
public function testCreateNewModelUsingConfigFrom()
2017-10-04 04:54:24 +00:00
{
2017-10-08 20:29:46 +00:00
$model = factory(Egg::class)->make();
$this->repository->shouldReceive('findCountWhere')->with([
['nest_id', '=', $model->nest_id],
['id', '=', 12345],
])->once()->andReturn(1);
2017-10-04 04:54:24 +00:00
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
$this->repository->shouldReceive('create')->with([
2017-10-08 20:29:46 +00:00
'nest_id' => $model->nest_id,
'config_from' => 12345,
'uuid' => $this->getKnownUuid(),
'author' => 'test@example.com',
2017-10-04 04:54:24 +00:00
], true, true)->once()->andReturn($model);
2017-10-08 20:29:46 +00:00
$response = $this->service->handle([
'nest_id' => $model->nest_id,
'config_from' => 12345,
]);
2017-10-04 04:54:24 +00:00
$this->assertNotEmpty($response);
2017-10-08 20:29:46 +00:00
$this->assertEquals($response, $model);
2017-08-12 21:30:27 +00:00
}
/**
2017-10-08 20:29:46 +00:00
* Test that certain data, such as the UUID or author takes priority over data
* that is passed into the function.
2017-08-12 21:30:27 +00:00
*/
2017-10-08 20:29:46 +00:00
public function testDataProvidedByHandlerTakesPriorityOverPassedData()
2017-08-12 21:30:27 +00:00
{
$model = factory(Egg::class)->make();
2017-10-04 04:54:24 +00:00
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
2017-10-08 20:29:46 +00:00
$this->repository->shouldReceive('create')->with([
'uuid' => $this->getKnownUuid(),
'author' => 'test@example.com',
'config_from' => null,
'name' => $model->name,
], true, true)->once()->andReturn($model);
2017-08-12 21:30:27 +00:00
2017-10-08 20:29:46 +00:00
$response = $this->service->handle(['name' => $model->name, 'uuid' => 'should-be-ignored', 'author' => 'should-be-ignored']);
2017-08-12 21:30:27 +00:00
$this->assertNotEmpty($response);
2017-10-08 20:29:46 +00:00
$this->assertNull(object_get($response, 'config_from'));
$this->assertEquals($model->name, $response->name);
2017-08-12 21:30:27 +00:00
}
/**
* Test that an exception is thrown if no parent configuration can be located.
*/
public function testExceptionIsThrownIfNoParentConfigurationIsFound()
{
$this->repository->shouldReceive('findCountWhere')->with([
2017-10-08 20:29:46 +00:00
['nest_id', '=', null],
2017-08-16 04:21:01 +00:00
['id', '=', 1],
2017-08-12 21:30:27 +00:00
])->once()->andReturn(0);
try {
$this->service->handle(['config_from' => 1]);
2017-10-08 20:29:46 +00:00
} catch (PterodactylException $exception) {
2017-08-12 21:30:27 +00:00
$this->assertInstanceOf(NoParentConfigurationFoundException::class, $exception);
2017-10-08 20:29:46 +00:00
$this->assertEquals(trans('exceptions.nest.egg.must_be_child'), $exception->getMessage());
2017-08-12 21:30:27 +00:00
}
}
}