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

92 lines
2.7 KiB
PHP
Raw Normal View History

2017-08-12 21:30:27 +00:00
<?php
namespace Tests\Unit\Services\Services\Options;
use Mockery as m;
2017-08-16 04:21:01 +00:00
use Tests\TestCase;
use Pterodactyl\Models\Egg;
2017-10-08 20:29:46 +00:00
use Pterodactyl\Services\Eggs\EggUpdateService;
use Pterodactyl\Exceptions\PterodactylException;
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 EggUpdateServiceTest extends TestCase
2017-08-12 21:30:27 +00:00
{
/**
* @var \Pterodactyl\Models\Egg
2017-08-12 21:30:27 +00:00
*/
protected $model;
/**
2017-10-08 20:29:46 +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\EggUpdateService
2017-08-12 21:30:27 +00:00
*/
protected $service;
/**
* Setup tests.
*/
public function setUp(): void
2017-08-12 21:30:27 +00:00
{
parent::setUp();
2020-10-06 04:29:35 +00:00
$this->model = factory(Egg::class)->make(['id' => 123]);
$this->repository = m::mock(EggRepositoryInterface::class);
2017-08-12 21:30:27 +00:00
$this->service = new EggUpdateService($this->repository);
2017-08-12 21:30:27 +00:00
}
/**
2017-10-08 20:29:46 +00:00
* Test that an Egg is updated when no config_from attribute is passed.
2017-08-12 21:30:27 +00:00
*/
2017-10-08 20:29:46 +00:00
public function testEggIsUpdatedWhenNoConfigFromIsProvided()
2017-08-12 21:30:27 +00:00
{
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel->update')
2017-10-08 20:29:46 +00:00
->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull();
2017-08-12 21:30:27 +00:00
$this->service->handle($this->model, ['test_field' => 'field_value']);
2017-10-08 20:29:46 +00:00
$this->assertTrue(true);
2017-08-12 21:30:27 +00:00
}
/**
2017-10-08 20:29:46 +00:00
* Test that Egg is updated when a valid config_from attribute is passed.
2017-08-12 21:30:27 +00:00
*/
public function testOptionIsUpdatedWhenValidConfigFromIsPassed()
{
$this->repository->shouldReceive('findCountWhere')->with([
2017-10-08 20:29:46 +00:00
['nest_id', '=', $this->model->nest_id],
2017-08-12 21:30:27 +00:00
['id', '=', 1],
])->once()->andReturn(1);
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel->update')
2017-10-08 20:29:46 +00:00
->with($this->model->id, ['config_from' => 1])->once()->andReturnNull();
2017-08-12 21:30:27 +00:00
$this->service->handle($this->model, ['config_from' => 1]);
2017-10-08 20:29:46 +00:00
$this->assertTrue(true);
2017-08-12 21:30:27 +00:00
}
/**
* Test that an exception is thrown if an invalid config_from attribute is passed.
*/
public function testExceptionIsThrownIfInvalidParentConfigIsPassed()
{
$this->repository->shouldReceive('findCountWhere')->with([
2017-10-08 20:29:46 +00:00
['nest_id', '=', $this->model->nest_id],
2017-08-12 21:30:27 +00:00
['id', '=', 1],
])->once()->andReturn(0);
try {
$this->service->handle($this->model, ['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
}
}
}