Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatabaseTest should not depend on database server id #135

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Api/Operator/DatabaseServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public function getAll(): array
return $this->getBy();
}

public function getDefault(string $type): Struct\Info
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get-default');
$filterTag = $getTag->addChild('filter');
/** @psalm-suppress UndefinedPropertyAssignment */
$filterTag->type = $type;

$response = $this->client->request($packet);

return new Struct\Info($response);
}

/**
* @param string|null $field
* @param int|string|null $value
Expand Down
7 changes: 7 additions & 0 deletions tests/DatabaseServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public function testGetAll()
$this->assertGreaterThan(0, count($dbServers));
$this->assertEquals('localhost', $dbServers[0]->host);
}

public function testGetDefault()
{
$dbServer = static::$client->databaseServer()->getDefault('mysql');
$this->assertEquals('mysql', $dbServer->type);
$this->assertGreaterThan(0, $dbServer->id);
}
}
52 changes: 27 additions & 25 deletions tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
class DatabaseTest extends AbstractTestCase
{
private static \PleskX\Api\Struct\Webspace\Info $webspace;
private static \PleskX\Api\Struct\DatabaseServer\Info $databaseServer;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
static::$webspace = static::createWebspace();
static::$databaseServer = static::$client->databaseServer()->getDefault('mysql');
}

public function testCreate()
{
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
static::$client->database()->delete('id', $database->id);
}
Expand All @@ -31,8 +33,8 @@ public function testCreateUser()
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user = $this->createUser([
'db-id' => $database->id,
Expand All @@ -48,8 +50,8 @@ public function testUpdateUser()
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user = $this->createUser([
'db-id' => $database->id,
Expand All @@ -71,15 +73,15 @@ public function testGetById()
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);

$db = static::$client->database()->get('id', $database->id);
$this->assertEquals('test1', $db->name);
$this->assertEquals('mysql', $db->type);
$this->assertEquals(static::$databaseServer->type, $db->type);
$this->assertEquals(static::$webspace->id, $db->webspaceId);
$this->assertEquals(1, $db->dbServerId);
$this->assertEquals(static::$databaseServer->id, $db->dbServerId);

static::$client->database()->delete('id', $database->id);
}
Expand All @@ -89,20 +91,20 @@ public function testGetAllByWebspaceId()
$db1 = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$db2 = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test2',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$databases = static::$client->database()->getAll('webspace-id', static::$webspace->id);
$this->assertEquals('test1', $databases[0]->name);
$this->assertEquals('test2', $databases[1]->name);
$this->assertEquals(static::$webspace->id, $databases[0]->webspaceId);
$this->assertEquals(1, $databases[1]->dbServerId);
$this->assertEquals(static::$databaseServer->id, $databases[1]->dbServerId);

static::$client->database()->delete('id', $db1->id);
static::$client->database()->delete('id', $db2->id);
Expand All @@ -113,8 +115,8 @@ public function testGetUserById()
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);

$user = $this->createUser([
Expand All @@ -136,14 +138,14 @@ public function testGetAllUsersByDbId()
$db1 = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$db2 = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test2',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user1 = $this->createUser([
'db-id' => $db1->id,
Expand Down Expand Up @@ -180,8 +182,8 @@ public function testDelete()
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$result = static::$client->database()->delete('id', $database->id);
$this->assertTrue($result);
Expand All @@ -192,8 +194,8 @@ public function testDeleteUser()
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => 'mysql',
'db-server-id' => 1,
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user = $this->createUser([
'db-id' => $database->id,
Expand Down