Path: blob/1.0-develop/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php
7460 views
<?php12namespace Pterodactyl\Tests\Integration\Services\Databases;34use Mockery\MockInterface;5use Pterodactyl\Models\Node;6use Pterodactyl\Models\Database;7use Pterodactyl\Models\DatabaseHost;8use Pterodactyl\Tests\Integration\IntegrationTestCase;9use Pterodactyl\Services\Databases\DatabaseManagementService;10use Pterodactyl\Services\Databases\DeployServerDatabaseService;11use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;1213class DeployServerDatabaseServiceTest extends IntegrationTestCase14{15private MockInterface $managementService;1617/**18* Setup tests.19*/20public function setUp(): void21{22parent::setUp();2324$this->managementService = \Mockery::mock(DatabaseManagementService::class);25$this->swap(DatabaseManagementService::class, $this->managementService);26}2728/**29* Ensure we reset the config to the expected value.30*/31protected function tearDown(): void32{33config()->set('pterodactyl.client_features.databases.allow_random', true);3435Database::query()->delete();36DatabaseHost::query()->delete();3738parent::tearDown();39}4041/**42* Test that an error is thrown if either the database name or the remote host are empty.43*/44#[\PHPUnit\Framework\Attributes\DataProvider('invalidDataProvider')]45public function testErrorIsThrownIfDatabaseNameIsEmpty(array $data)46{47$server = $this->createServerModel();4849$this->expectException(\InvalidArgumentException::class);50$this->expectExceptionMessageMatches('/^Expected a non-empty value\. Got: /');51$this->getService()->handle($server, $data);52}5354/**55* Test that an error is thrown if there are no database hosts on the same node as the56* server and the allow_random config value is false.57*/58public function testErrorIsThrownIfNoDatabaseHostsExistOnNode()59{60$server = $this->createServerModel();6162$node = Node::factory()->create(['location_id' => $server->location->id]);63DatabaseHost::factory()->create(['node_id' => $node->id]);6465config()->set('pterodactyl.client_features.databases.allow_random', false);6667$this->expectException(NoSuitableDatabaseHostException::class);6869$this->getService()->handle($server, [70'database' => 'something',71'remote' => '%',72]);73}7475/**76* Test that an error is thrown if no database hosts exist at all on the system.77*/78public function testErrorIsThrownIfNoDatabaseHostsExistOnSystem()79{80$server = $this->createServerModel();8182$this->expectException(NoSuitableDatabaseHostException::class);8384$this->getService()->handle($server, [85'database' => 'something',86'remote' => '%',87]);88}8990/**91* Test that a database host on the same node as the server is preferred.92*/93public function testDatabaseHostOnSameNodeIsPreferred()94{95$server = $this->createServerModel();9697$node = Node::factory()->create(['location_id' => $server->location->id]);98DatabaseHost::factory()->create(['node_id' => $node->id]);99$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);100101$this->managementService->expects('create')->with($server, [102'database_host_id' => $host->id,103'database' => "s{$server->id}_something",104'remote' => '%',105])->andReturns(new Database());106107$response = $this->getService()->handle($server, [108'database' => 'something',109'remote' => '%',110]);111112$this->assertInstanceOf(Database::class, $response);113}114115/**116* Test that a database host not assigned to the same node as the server is used if117* there are no same-node hosts and the allow_random configuration value is set to118* true.119*/120public function testDatabaseHostIsSelectedIfNoSuitableHostExistsOnSameNode()121{122$server = $this->createServerModel();123124$node = Node::factory()->create(['location_id' => $server->location->id]);125$host = DatabaseHost::factory()->create(['node_id' => $node->id]);126127$this->managementService->expects('create')->with($server, [128'database_host_id' => $host->id,129'database' => "s{$server->id}_something",130'remote' => '%',131])->andReturns(new Database());132133$response = $this->getService()->handle($server, [134'database' => 'something',135'remote' => '%',136]);137138$this->assertInstanceOf(Database::class, $response);139}140141public static function invalidDataProvider(): array142{143return [144[['remote' => '%']],145[['database' => null, 'remote' => '%']],146[['database' => '', 'remote' => '%']],147[['database' => '']],148[['database' => '', 'remote' => '']],149];150}151152private function getService(): DeployServerDatabaseService153{154return $this->app->make(DeployServerDatabaseService::class);155}156}157158159