Skip to content

Commit a5d9faf

Browse files
committed
Get database unit tests back into passing shape
1 parent 756a21f commit a5d9faf

File tree

4 files changed

+20
-73
lines changed

4 files changed

+20
-73
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ coverage.xml
3232
resources/lang/locales.js
3333
resources/assets/pterodactyl/scripts/helpers/ziggy.js
3434
resources/assets/scripts/helpers/ziggy.js
35+
.phpunit.result.cache

.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/Unit/Services/Databases/DatabasePasswordServiceTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ public function setUp(): void
5151
*/
5252
public function testPasswordIsChanged()
5353
{
54-
$model = factory(Database::class)->make();
54+
/** @var \Pterodactyl\Models\Database $model */
55+
$model = factory(Database::class)->make(['max_connections' => 0]);
5556

5657
$this->connection->expects('transaction')->with(m::on(function ($closure) {
5758
return is_null($closure());
5859
}));
5960

60-
$this->dynamic->shouldReceive('set')->with('dynamic', $model->database_host_id)->once()->andReturnNull();
61+
$this->dynamic->expects('set')->with('dynamic', $model->database_host_id)->andReturnNull();
6162

6263
$this->encrypter->expects('encrypt')->with(m::on(function ($string) {
6364
preg_match_all('/[!@+=.^-]/', $string, $matches, PREG_SET_ORDER);
@@ -67,13 +68,13 @@ public function testPasswordIsChanged()
6768
return true;
6869
}))->andReturn('enc123');
6970

70-
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
71-
$this->repository->shouldReceive('update')->with($model->id, ['password' => 'enc123'])->once()->andReturn(true);
71+
$this->repository->expects('withoutFreshModel')->withNoArgs()->andReturnSelf();
72+
$this->repository->expects('update')->with($model->id, ['password' => 'enc123'])->andReturn(true);
7273

73-
$this->repository->shouldReceive('dropUser')->with($model->username, $model->remote)->once()->andReturn(true);
74-
$this->repository->shouldReceive('createUser')->with($model->username, $model->remote, m::any())->once()->andReturn(true);
75-
$this->repository->shouldReceive('assignUserToDatabase')->with($model->database, $model->username, $model->remote)->once()->andReturn(true);
76-
$this->repository->shouldReceive('flush')->withNoArgs()->once()->andReturn(true);
74+
$this->repository->expects('dropUser')->with($model->username, $model->remote)->andReturn(true);
75+
$this->repository->expects('createUser')->with($model->username, $model->remote, m::any(), 0)->andReturn(true);
76+
$this->repository->expects('assignUserToDatabase')->with($model->database, $model->username, $model->remote)->andReturn(true);
77+
$this->repository->expects('flush')->withNoArgs()->andReturn(true);
7778

7879
$response = $this->getService()->handle($model);
7980
$this->assertNotEmpty($response);

tests/Unit/Services/Databases/DeployServerDatabaseServiceTest.php

Lines changed: 10 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
1111
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
1212
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
13+
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
1314

1415
class DeployServerDatabaseServiceTest extends TestCase
1516
{
@@ -51,24 +52,17 @@ public function setUp(): void
5152
*/
5253
public function testNonRandomFoundHost($limit, $count)
5354
{
54-
config()->set('pterodactyl.client_features.databases.allow_random', false);
55-
5655
$server = factory(Server::class)->make(['database_limit' => $limit]);
5756
$model = factory(Database::class)->make();
5857

59-
$this->repository->shouldReceive('findCountWhere')
60-
->once()
61-
->with([['server_id', '=', $server->id]])
62-
->andReturn($count);
63-
6458
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
6559
->once()
6660
->with([['node_id', '=', $server->node_id]])
6761
->andReturn(collect([$model]));
6862

6963
$this->managementService->shouldReceive('create')
7064
->once()
71-
->with($server->id, [
65+
->with($server, [
7266
'database_host_id' => $model->id,
7367
'database' => 'testdb',
7468
'remote' => null,
@@ -83,25 +77,20 @@ public function testNonRandomFoundHost($limit, $count)
8377

8478
/**
8579
* Test that an exception is thrown if in non-random mode and no host is found.
86-
*
87-
* @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException
8880
*/
8981
public function testNonRandomNoHost()
9082
{
91-
config()->set('pterodactyl.client_features.databases.allow_random', false);
83+
$this->expectException(NoSuitableDatabaseHostException::class);
9284

9385
$server = factory(Server::class)->make(['database_limit' => 1]);
9486

95-
$this->repository->shouldReceive('findCountWhere')
96-
->once()
97-
->with([['server_id', '=', $server->id]])
98-
->andReturn(0);
99-
10087
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
10188
->once()
10289
->with([['node_id', '=', $server->node_id]])
10390
->andReturn(collect());
10491

92+
$this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect());
93+
10594
$this->getService()->handle($server, []);
10695
}
10796

@@ -113,11 +102,6 @@ public function testRandomFoundHost()
113102
$server = factory(Server::class)->make(['database_limit' => 1]);
114103
$model = factory(Database::class)->make();
115104

116-
$this->repository->shouldReceive('findCountWhere')
117-
->once()
118-
->with([['server_id', '=', $server->id]])
119-
->andReturn(0);
120-
121105
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
122106
->once()
123107
->with([['node_id', '=', $server->node_id]])
@@ -129,7 +113,7 @@ public function testRandomFoundHost()
129113

130114
$this->managementService->shouldReceive('create')
131115
->once()
132-
->with($server->id, [
116+
->with($server, [
133117
'database_host_id' => $model->id,
134118
'database' => 'testdb',
135119
'remote' => null,
@@ -144,60 +128,22 @@ public function testRandomFoundHost()
144128

145129
/**
146130
* Test that an exception is thrown when no host is found and random is allowed.
147-
*
148-
* @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException
149131
*/
150132
public function testRandomNoHost()
151133
{
152-
$server = factory(Server::class)->make(['database_limit' => 1]);
134+
$this->expectException(NoSuitableDatabaseHostException::class);
153135

154-
$this->repository->shouldReceive('findCountWhere')
155-
->once()
156-
->with([['server_id', '=', $server->id]])
157-
->andReturn(0);
136+
$server = factory(Server::class)->make(['database_limit' => 1]);
158137

159-
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
160-
->once()
138+
$this->databaseHostRepository->expects('setColumns->findWhere')
161139
->with([['node_id', '=', $server->node_id]])
162140
->andReturn(collect());
163141

164-
$this->databaseHostRepository->shouldReceive('setColumns->all')
165-
->once()
166-
->andReturn(collect());
167-
168-
$this->getService()->handle($server, []);
169-
}
170-
171-
/**
172-
* Test that a server over the database limit throws an exception.
173-
*
174-
* @dataProvider databaseExceedingLimitDataProvider
175-
* @expectedException \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
176-
*/
177-
public function testServerOverDatabaseLimit($limit, $count)
178-
{
179-
$server = factory(Server::class)->make(['database_limit' => $limit]);
180-
181-
$this->repository->shouldReceive('findCountWhere')
182-
->once()
183-
->with([['server_id', '=', $server->id]])
184-
->andReturn($count);
142+
$this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect());
185143

186144
$this->getService()->handle($server, []);
187145
}
188146

189-
/**
190-
* Test that an exception is thrown if the feature is not enabled.
191-
*
192-
* @expectedException \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
193-
*/
194-
public function testFeatureNotEnabled()
195-
{
196-
config()->set('pterodactyl.client_features.databases.enabled', false);
197-
198-
$this->getService()->handle(factory(Server::class)->make(), []);
199-
}
200-
201147
/**
202148
* Provide limits and current database counts for testing.
203149
*

0 commit comments

Comments
 (0)