|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Services\Servers; |
| 4 | + |
| 5 | +use Mockery as m; |
| 6 | +use Tests\TestCase; |
| 7 | +use Pterodactyl\Models\Egg; |
| 8 | +use Pterodactyl\Models\Server; |
| 9 | +use Pterodactyl\Models\Allocation; |
| 10 | +use Pterodactyl\Services\Servers\EnvironmentService; |
| 11 | +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; |
| 12 | +use Pterodactyl\Services\Servers\ServerConfigurationStructureService; |
| 13 | + |
| 14 | +class ServerConfigurationStructureServiceTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Pterodactyl\Services\Servers\EnvironmentService|\Mockery\Mock |
| 18 | + */ |
| 19 | + private $environment; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock |
| 23 | + */ |
| 24 | + private $repository; |
| 25 | + |
| 26 | + /** |
| 27 | + * Setup tests. |
| 28 | + */ |
| 29 | + public function setUp() |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->environment = m::mock(EnvironmentService::class); |
| 34 | + $this->repository = m::mock(ServerRepositoryInterface::class); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Test that a configuration is returned in the proper format when passed a |
| 39 | + * server model that is missing required relationships. |
| 40 | + */ |
| 41 | + public function testCorrectStructureIsReturned() |
| 42 | + { |
| 43 | + $model = factory(Server::class)->make(); |
| 44 | + $model->allocation = factory(Allocation::class)->make(); |
| 45 | + $model->allocations = collect(factory(Allocation::class)->times(2)->make()); |
| 46 | + $model->egg = factory(Egg::class)->make(); |
| 47 | + |
| 48 | + $portListing = $model->allocations->groupBy('ip')->map(function ($item) { |
| 49 | + return $item->pluck('port'); |
| 50 | + })->toArray(); |
| 51 | + |
| 52 | + $this->repository->shouldReceive('getDataForCreation')->with($model)->once()->andReturn($model); |
| 53 | + $this->environment->shouldReceive('process')->with($model)->once()->andReturn(['environment_array']); |
| 54 | + |
| 55 | + $response = $this->getService()->handle($model); |
| 56 | + $this->assertNotEmpty($response); |
| 57 | + $this->assertArrayNotHasKey('user', $response); |
| 58 | + $this->assertArrayNotHasKey('keys', $response); |
| 59 | + $this->assertArrayHasKey('uuid', $response); |
| 60 | + $this->assertArrayHasKey('build', $response); |
| 61 | + $this->assertArrayHasKey('service', $response); |
| 62 | + $this->assertArrayHasKey('rebuild', $response); |
| 63 | + $this->assertArrayHasKey('suspended', $response); |
| 64 | + |
| 65 | + $this->assertArraySubset([ |
| 66 | + 'default' => [ |
| 67 | + 'ip' => $model->allocation->ip, |
| 68 | + 'port' => $model->allocation->port, |
| 69 | + ], |
| 70 | + ], $response['build'], true, 'Assert server default allocation is correct.'); |
| 71 | + $this->assertArraySubset(['ports' => $portListing], $response['build'], true, 'Assert server ports are correct.'); |
| 72 | + $this->assertArraySubset([ |
| 73 | + 'env' => ['environment_array'], |
| 74 | + 'swap' => (int) $model->swap, |
| 75 | + 'io' => (int) $model->io, |
| 76 | + 'cpu' => (int) $model->cpu, |
| 77 | + 'disk' => (int) $model->disk, |
| 78 | + 'image' => $model->image, |
| 79 | + ], $response['build'], true, 'Assert server build data is correct.'); |
| 80 | + |
| 81 | + $this->assertArraySubset([ |
| 82 | + 'egg' => $model->egg->uuid, |
| 83 | + 'pack' => null, |
| 84 | + 'skip_scripts' => $model->skip_scripts, |
| 85 | + ], $response['service']); |
| 86 | + |
| 87 | + $this->assertFalse($response['rebuild']); |
| 88 | + $this->assertSame((int) $model->suspended, $response['suspended']); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Return an instance of the service with mocked dependencies. |
| 93 | + * |
| 94 | + * @return \Pterodactyl\Services\Servers\ServerConfigurationStructureService |
| 95 | + */ |
| 96 | + private function getService(): ServerConfigurationStructureService |
| 97 | + { |
| 98 | + return new ServerConfigurationStructureService($this->repository, $this->environment); |
| 99 | + } |
| 100 | +} |
0 commit comments