From ebe3f6a32a31b369f9cc91aa957389725b04c6b1 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 11 Oct 2020 12:35:54 -0700 Subject: [PATCH] Fix unit tests --- .../DeployServerDatabaseServiceTest.php | 182 ------------------ 1 file changed, 182 deletions(-) delete mode 100644 tests/Unit/Services/Databases/DeployServerDatabaseServiceTest.php diff --git a/tests/Unit/Services/Databases/DeployServerDatabaseServiceTest.php b/tests/Unit/Services/Databases/DeployServerDatabaseServiceTest.php deleted file mode 100644 index fc86cdcfc..000000000 --- a/tests/Unit/Services/Databases/DeployServerDatabaseServiceTest.php +++ /dev/null @@ -1,182 +0,0 @@ -databaseHostRepository = m::mock(DatabaseHostRepositoryInterface::class); - $this->managementService = m::mock(DatabaseManagementService::class); - $this->repository = m::mock(DatabaseRepositoryInterface::class); - - // Set configs for testing instances. - config()->set('pterodactyl.client_features.databases.enabled', true); - config()->set('pterodactyl.client_features.databases.allow_random', true); - } - - /** - * Test handling of non-random hosts when a host is found. - * - * @dataProvider databaseLimitDataProvider - */ - public function testNonRandomFoundHost($limit, $count) - { - $server = factory(Server::class)->make(['database_limit' => $limit]); - $model = factory(Database::class)->make(); - - $this->databaseHostRepository->shouldReceive('setColumns->findWhere') - ->once() - ->with([['node_id', '=', $server->node_id]]) - ->andReturn(collect([$model])); - - $this->managementService->shouldReceive('create') - ->once() - ->with($server, [ - 'database_host_id' => $model->id, - 'database' => 'testdb', - 'remote' => null, - ]) - ->andReturn($model); - - $response = $this->getService()->handle($server, ['database' => 'testdb']); - - $this->assertInstanceOf(Database::class, $response); - $this->assertSame($model, $response); - } - - /** - * Test that an exception is thrown if in non-random mode and no host is found. - */ - public function testNonRandomNoHost() - { - $this->expectException(NoSuitableDatabaseHostException::class); - - $server = factory(Server::class)->make(['database_limit' => 1]); - - $this->databaseHostRepository->shouldReceive('setColumns->findWhere') - ->once() - ->with([['node_id', '=', $server->node_id]]) - ->andReturn(collect()); - - $this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect()); - - $this->getService()->handle($server, []); - } - - /** - * Test handling of random host selection. - */ - public function testRandomFoundHost() - { - $server = factory(Server::class)->make(['database_limit' => 1]); - $model = factory(Database::class)->make(); - - $this->databaseHostRepository->shouldReceive('setColumns->findWhere') - ->once() - ->with([['node_id', '=', $server->node_id]]) - ->andReturn(collect()); - - $this->databaseHostRepository->shouldReceive('setColumns->all') - ->once() - ->andReturn(collect([$model])); - - $this->managementService->shouldReceive('create') - ->once() - ->with($server, [ - 'database_host_id' => $model->id, - 'database' => 'testdb', - 'remote' => null, - ]) - ->andReturn($model); - - $response = $this->getService()->handle($server, ['database' => 'testdb']); - - $this->assertInstanceOf(Database::class, $response); - $this->assertSame($model, $response); - } - - /** - * Test that an exception is thrown when no host is found and random is allowed. - */ - public function testRandomNoHost() - { - $this->expectException(NoSuitableDatabaseHostException::class); - - $server = factory(Server::class)->make(['database_limit' => 1]); - - $this->databaseHostRepository->expects('setColumns->findWhere') - ->with([['node_id', '=', $server->node_id]]) - ->andReturn(collect()); - - $this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect()); - - $this->getService()->handle($server, []); - } - - /** - * Provide limits and current database counts for testing. - * - * @return array - */ - public function databaseLimitDataProvider(): array - { - return [ - [null, 10], - [1, 0], - ]; - } - - /** - * Provide data for servers over their database limit. - * - * @return array - */ - public function databaseExceedingLimitDataProvider(): array - { - return [ - [2, 2], - [2, 3], - ]; - } - - /** - * Return an instance of the service with mocked dependencies for testing. - * - * @return \Pterodactyl\Services\Databases\DeployServerDatabaseService - */ - private function getService(): DeployServerDatabaseService - { - return new DeployServerDatabaseService($this->repository, $this->databaseHostRepository, $this->managementService); - } -}