Skip to content

Commit b629742

Browse files
committed
Improve db connection message and adherence
1 parent 29ff3d2 commit b629742

File tree

5 files changed

+110
-28
lines changed

5 files changed

+110
-28
lines changed

src/Checks/Database.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ class Database extends PreflightCheck
1919
public function check(Result $result): Result
2020
{
2121
try {
22-
$pdo = DB::getPdo();
22+
$pdo = DB::connection($this->getConnection())->getPdo();
2323
} catch (Exception $e) {
2424
return $result->fail($e->getMessage(), $e);
25+
} catch (\Exception $e) {
26+
return $result->fail('General failure: ' . $e->getMessage(), $e);
2527
}
2628

2729
$attributes = [];
@@ -60,4 +62,12 @@ protected function getConnection(): string
6062
{
6163
return $this->options['connection'] ?? config('database.default');
6264
}
65+
66+
/**
67+
* Gets the message for successful key check.
68+
*/
69+
protected function getConfigPassMessage()
70+
{
71+
return sprintf('DB Config keys are set! (%s)', $this->getConnection());
72+
}
6373
}

src/Checks/PreflightCheck.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ protected function checkConfig(Result $result): Result
115115
return $result->fail('Missing configuration key(s).', $missingKeys);
116116
}
117117

118-
return $result->pass('Config keys are set!', $this->requiredConfig);
118+
return $result->pass($this->getConfigPassMessage(), $this->requiredConfig);
119+
}
120+
121+
/**
122+
* Gets the message for successful key check.
123+
*/
124+
protected function getConfigPassMessage()
125+
{
126+
return 'Config keys are set!';
119127
}
120128
}

tests/Checks/BasePreflightCheckTest.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,22 @@ abstract class BasePreflightCheckTest extends TestCase
1717
*/
1818
protected $preflightCheckClass;
1919

20-
protected PreflightCheck $preflightCheck;
21-
2220
protected function getPackageProviders($app)
2321
{
2422
return [PreflightChecksServiceProvider::class];
2523
}
2624

27-
protected function setUp(): void
28-
{
29-
parent::setUp();
30-
31-
$this->preflightCheck = new $this->preflightCheckClass();
32-
}
33-
34-
/**
35-
* @test
36-
*/
37-
public function testChecksConfigValues()
25+
public function checkConfigValues(PreflightCheck $preflightCheck)
3826
{
39-
$config = $this->getProtectedProperty($this->preflightCheck, 'requiredConfig');
27+
$config = $this->getProtectedProperty($preflightCheck, 'requiredConfig');
4028

4129
if (empty($config)) {
4230
// No config values, so this technically passes.
4331
$this->assertTrue(true);
4432
}
4533

4634
foreach ($config as $configKey) {
47-
$this->assertConfigKeyChecked($configKey);
35+
$this->assertConfigKeyChecked($preflightCheck, $configKey);
4836
}
4937
}
5038

@@ -60,13 +48,13 @@ protected function assertFailed(Result $result): void
6048
$this->assertTrue($result->failed());
6149
}
6250

63-
private function assertConfigKeyChecked(string $configKey): void
51+
private function assertConfigKeyChecked(PreflightCheck $preflightCheck, string $configKey): void
6452
{
6553
$originalValue = config($configKey);
6654

6755
config([$configKey => null]);
6856

69-
$configResult = $this->invokeMethod($this->preflightCheck, 'checkConfig', [new Result('Test\Config')]);
57+
$configResult = $this->invokeMethod($preflightCheck, 'checkConfig', [new Result('Test\Config')]);
7058
$this->assertTrue(in_array($configKey, $configResult->getRawData()));
7159

7260
config([$configKey => $originalValue]);

tests/Checks/DatabaseTest.php

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,32 @@ class DatabaseTest extends BasePreflightCheckTest
1515
{
1616
protected $preflightCheckClass = Database::class;
1717

18+
private const TEST_DEFAULT_DB_CONNECTION = 'test_default';
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
config(['database.default' => static::TEST_DEFAULT_DB_CONNECTION]);
25+
}
26+
1827
/**
1928
* @test
29+
* @dataProvider providesDatabaseScenarios
2030
*/
21-
public function testChecksDatabaseAccessible()
31+
public function testChecksDatabaseAccessible(?array $options, string $expectedConnection)
2232
{
2333
$mockPdo = Mockery::mock(PDOConnection::class);
24-
DB::shouldReceive('getPdo')->once()->andReturn($mockPdo);
34+
DB::shouldReceive('connection')
35+
->once()
36+
->with($expectedConnection)
37+
->andReturn(
38+
Mockery::mock(Connection::class)
39+
->shouldReceive('getPdo')
40+
->once()
41+
->andReturn($mockPdo)
42+
->getMock()
43+
);
2544

2645
$attributes = [
2746
PDO::ATTR_CLIENT_VERSION => 'test-version',
@@ -31,18 +50,64 @@ public function testChecksDatabaseAccessible()
3150
];
3251
$mockPdo->shouldReceive('getAttribute')->andReturnUsing(fn ($arg) => $attributes[$arg]);
3352

34-
$result = $this->preflightCheck->check(new Result('Test\Test'));
53+
$preflightCheck = is_null($options) ? new $this->preflightCheckClass : new $this->preflightCheckClass($options);
54+
$result = $preflightCheck->check(new Result('Test\Test'));
3555
$this->assertPassed($result);
3656
}
3757

58+
public function providesDatabaseScenarios()
59+
{
60+
yield 'No options checks default' => [
61+
null,
62+
static::TEST_DEFAULT_DB_CONNECTION,
63+
];
64+
65+
yield 'Empty options checks default' => [
66+
[],
67+
static::TEST_DEFAULT_DB_CONNECTION,
68+
];
69+
70+
yield 'Default checks default' => [
71+
['connection' => static::TEST_DEFAULT_DB_CONNECTION],
72+
static::TEST_DEFAULT_DB_CONNECTION,
73+
];
74+
75+
$testConnection = 'test_connection_'.mt_rand(100, 99999);
76+
77+
yield 'Connection checks connection' => [
78+
['connection' => $testConnection],
79+
$testConnection,
80+
];
81+
}
82+
3883
/**
3984
* @test
4085
*/
41-
public function testChecksDatabaseInAccessible()
86+
public function testChecksDatabaseInaccessible()
4287
{
43-
DB::shouldReceive('getPdo')->once()->andThrow(new Exception(Mockery::mock(PDOException::class)));
88+
DB::shouldReceive('connection')
89+
->once()
90+
->with(static::TEST_DEFAULT_DB_CONNECTION)
91+
->andReturn(
92+
Mockery::mock(Connection::class)
93+
->shouldReceive('getPdo')
94+
->once()
95+
->andThrow(new Exception(Mockery::mock(PDOException::class)))
96+
->getMock()
97+
);
4498

45-
$result = $this->preflightCheck->check(new Result('Test\Test'));
99+
$preflightCheck = new $this->preflightCheckClass();
100+
$result = $preflightCheck->check(new Result('Test\Test'));
46101
$this->assertFailed($result);
47102
}
103+
104+
/**
105+
* @test
106+
* @dataProvider providesDatabaseScenarios
107+
*/
108+
public function testChecksConfigValues(?array $options, string $expectedConnection)
109+
{
110+
$preflight = is_null($options) ? new $this->preflightCheckClass : new $this->preflightCheckClass($options);
111+
$this->checkConfigValues($preflight);
112+
}
48113
}

tests/Checks/RedisTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function testChecksRedisIsAccessible()
3131
$mockConnection->shouldReceive('getName')->once()->andReturn($connectionName);
3232
$mockConnection->shouldReceive('client->info')->once()->andReturn($connectionInfo);
3333

34-
$result = $this->preflightCheck->check(new Result('Test\Test'));
34+
$preflightCheck = new $this->preflightCheckClass();
35+
$result = $preflightCheck->check(new Result('Test\Test'));
3536

3637
$this->assertPassed($result);
3738
$resultData = $result->getRawData();
@@ -49,7 +50,8 @@ public function testChecksRedisIsDown()
4950
->once()
5051
->andThrow(\Exception::class);
5152

52-
$result = $this->preflightCheck->check(new Result('Test\Test'));
53+
$preflightCheck = new $this->preflightCheckClass();
54+
$result = $preflightCheck->check(new Result('Test\Test'));
5355

5456
$this->assertFailed($result);
5557
}
@@ -70,8 +72,17 @@ public function testChecksRedisIsNotConnected()
7072
$mockConnection->shouldNotReceive('getName');
7173
$mockConnection->shouldNotReceive('client->info');
7274

73-
$result = $this->preflightCheck->check(new Result('Test\Test'));
75+
$preflightCheck = new $this->preflightCheckClass();
76+
$result = $preflightCheck->check(new Result('Test\Test'));
7477

7578
$this->assertFailed($result);
7679
}
80+
81+
/**
82+
* @test
83+
*/
84+
public function testChecksConfigValues()
85+
{
86+
$this->checkConfigValues(new $this->preflightCheckClass());
87+
}
7788
}

0 commit comments

Comments
 (0)