Skip to content

Commit

Permalink
DatabaseTest should not depend on database server id
Browse files Browse the repository at this point in the history
  • Loading branch information
xgin committed Nov 23, 2023
1 parent 726701d commit 91aa445
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
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

0 comments on commit 91aa445

Please sign in to comment.