connection = m::mock(ConnectionInterface::class); $this->daemonRepository = m::mock(DaemonRepositoryInterface::class); $this->repository = m::mock(AllocationRepositoryInterface::class); $this->serverRepository = m::mock(ServerRepositoryInterface::class); } /** * Test that an allocation can be updated. * * @dataProvider useModelDataProvider */ public function testAllocationIsUpdated(bool $useModel) { $allocations = factory(Allocation::class)->times(2)->make(); $model = factory(Server::class)->make(); if (! $useModel) { $this->serverRepository->shouldReceive('find')->with(1234)->once()->andReturn($model); } $this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn($allocations); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf(); $this->serverRepository->shouldReceive('update')->with($model->id, [ 'allocation_id' => $allocations->first()->id, ])->once()->andReturnNull(); $this->daemonRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf(); $this->daemonRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf(); $this->daemonRepository->shouldReceive('update')->with([ 'build' => [ 'default' => [ 'ip' => $allocations->first()->ip, 'port' => $allocations->first()->port, ], 'ports|overwrite' => $allocations->groupBy('ip')->map(function ($item) { return $item->pluck('port'); })->toArray(), ], ])->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $response = $this->getService()->handle($useModel ? $model : 1234, $allocations->first()->id); $this->assertNotEmpty($response); $this->assertSame($allocations->first(), $response); } /** * Test that an allocation that doesn't belong to a server throws an exception. * * @expectedException \Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException */ public function testAllocationNotBelongingToServerThrowsException() { $model = factory(Server::class)->make(); $this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect()); $this->getService()->handle($model, 1234); } /** * Test that an exception thrown by guzzle is handled properly. */ public function testExceptionThrownByGuzzleIsHandled() { $this->configureExceptionMock(); $allocation = factory(Allocation::class)->make(); $model = factory(Server::class)->make(); $this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect([$allocation])); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf(); $this->serverRepository->shouldReceive('update')->with($model->id, [ 'allocation_id' => $allocation->id, ])->once()->andReturnNull(); $this->daemonRepository->shouldReceive('setAccessServer->setNode->update')->once()->andThrow($this->getExceptionMock()); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); try { $this->getService()->handle($model, $allocation->id); } catch (PterodactylException $exception) { $this->assertInstanceOf(DaemonConnectionException::class, $exception); $this->assertInstanceOf(RequestException::class, $exception->getPrevious()); } } /** * Data provider to determine if a model should be passed or an int. * * @return array */ public function useModelDataProvider(): array { return [[false], [true]]; } /** * Return an instance of the service with mocked dependencies. * * @return \Pterodactyl\Services\Allocations\SetDefaultAllocationService */ private function getService(): SetDefaultAllocationService { return new SetDefaultAllocationService($this->repository, $this->connection, $this->daemonRepository, $this->serverRepository); } }