From 7022ec788fb089d823e951567fe70d259c557fda Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 26 Oct 2017 20:23:43 -0500 Subject: [PATCH] Test for server config structure --- ...erverConfigurationStructureServiceTest.php | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 tests/Unit/Services/Servers/ServerConfigurationStructureServiceTest.php diff --git a/tests/Unit/Services/Servers/ServerConfigurationStructureServiceTest.php b/tests/Unit/Services/Servers/ServerConfigurationStructureServiceTest.php new file mode 100644 index 000000000..e3b51693a --- /dev/null +++ b/tests/Unit/Services/Servers/ServerConfigurationStructureServiceTest.php @@ -0,0 +1,100 @@ +environment = m::mock(EnvironmentService::class); + $this->repository = m::mock(ServerRepositoryInterface::class); + } + + /** + * Test that a configuration is returned in the proper format when passed a + * server model that is missing required relationships. + */ + public function testCorrectStructureIsReturned() + { + $model = factory(Server::class)->make(); + $model->allocation = factory(Allocation::class)->make(); + $model->allocations = collect(factory(Allocation::class)->times(2)->make()); + $model->egg = factory(Egg::class)->make(); + + $portListing = $model->allocations->groupBy('ip')->map(function ($item) { + return $item->pluck('port'); + })->toArray(); + + $this->repository->shouldReceive('getDataForCreation')->with($model)->once()->andReturn($model); + $this->environment->shouldReceive('process')->with($model)->once()->andReturn(['environment_array']); + + $response = $this->getService()->handle($model); + $this->assertNotEmpty($response); + $this->assertArrayNotHasKey('user', $response); + $this->assertArrayNotHasKey('keys', $response); + $this->assertArrayHasKey('uuid', $response); + $this->assertArrayHasKey('build', $response); + $this->assertArrayHasKey('service', $response); + $this->assertArrayHasKey('rebuild', $response); + $this->assertArrayHasKey('suspended', $response); + + $this->assertArraySubset([ + 'default' => [ + 'ip' => $model->allocation->ip, + 'port' => $model->allocation->port, + ], + ], $response['build'], true, 'Assert server default allocation is correct.'); + $this->assertArraySubset(['ports' => $portListing], $response['build'], true, 'Assert server ports are correct.'); + $this->assertArraySubset([ + 'env' => ['environment_array'], + 'swap' => (int) $model->swap, + 'io' => (int) $model->io, + 'cpu' => (int) $model->cpu, + 'disk' => (int) $model->disk, + 'image' => $model->image, + ], $response['build'], true, 'Assert server build data is correct.'); + + $this->assertArraySubset([ + 'egg' => $model->egg->uuid, + 'pack' => null, + 'skip_scripts' => $model->skip_scripts, + ], $response['service']); + + $this->assertFalse($response['rebuild']); + $this->assertSame((int) $model->suspended, $response['suspended']); + } + + /** + * Return an instance of the service with mocked dependencies. + * + * @return \Pterodactyl\Services\Servers\ServerConfigurationStructureService + */ + private function getService(): ServerConfigurationStructureService + { + return new ServerConfigurationStructureService($this->repository, $this->environment); + } +}