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

113 lines
3.4 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 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()
{
parent::setUp();
$this->model = factory(Egg::class)->make();
$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
{
2017-10-08 20:29:46 +00:00
$this->repository->shouldReceive('withoutFresh->update')
->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);
2017-10-08 20:29:46 +00:00
$this->repository->shouldReceive('withoutFresh->update')
->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
}
}
/**
2017-10-08 20:29:46 +00:00
* Test that an integer linking to a model can be passed in place of the Egg model.
2017-08-12 21:30:27 +00:00
*/
public function testIntegerCanBePassedInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
2017-10-08 20:29:46 +00:00
$this->repository->shouldReceive('withoutFresh->update')
->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull();
2017-08-12 21:30:27 +00:00
$this->service->handle($this->model->id, ['test_field' => 'field_value']);
2017-10-08 20:29:46 +00:00
$this->assertTrue(true);
2017-08-12 21:30:27 +00:00
}
}