-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
112 lines (95 loc) · 3.22 KB
/
Copy pathDatabase.php
File metadata and controls
112 lines (95 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* Italix Testing - Database
*
* @package Italix\Testing
*/
declare(strict_types=1);
namespace Italix\Testing;
use PDO;
use Throwable;
/**
* The test database, from the environment and from nowhere else.
*
* A library suite that reaches into an application's configuration directory
* for credentials cannot be run by anyone else — it stops being a test of the
* library and becomes a test of one deployment. So the connection comes from
* three environment variables and the suite skips cleanly when they are absent:
*
* ITALIX_TEST_DSN=mysql:host=127.0.0.1;dbname=italix_test;charset=utf8mb4
* ITALIX_TEST_USER=…
* ITALIX_TEST_PASSWORD=…
*
* How they get set is the *consumer's* problem. In this repository `ix test`
* exports them from the site's own `.env` before running the suites, which
* keeps the coverage without putting the coupling in the library.
*
* **Skipped is not passed.** `skip_notice()` prints why, and the caller reports
* nothing as green — a suite that silently pretends to have run is worse than
* one that admits it did not.
*/
final class Database
{
public const DSN_VAR = 'ITALIX_TEST_DSN';
public const USER_VAR = 'ITALIX_TEST_USER';
public const PASSWORD_VAR = 'ITALIX_TEST_PASSWORD';
private function __construct()
{
}
/**
* A connection, or null when the environment does not describe one.
*/
public static function connect(): ?PDO
{
$dsn = self::env(self::DSN_VAR);
if ($dsn === null) {
return null;
}
try {
return new PDO(
$dsn,
self::env(self::USER_VAR) ?? '',
self::env(self::PASSWORD_VAR) ?? '',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
} catch (Throwable $e) {
self::$last_error_c = $e->getMessage();
return null;
}
}
public static function is_available(): bool
{
return self::env(self::DSN_VAR) !== null;
}
/**
* The line a suite prints instead of pretending it ran.
*/
public static function skip_notice(string $what): string
{
$why = self::env(self::DSN_VAR) === null
? self::DSN_VAR . ' is not set'
: 'could not connect: ' . (self::$last_error_c ?? 'unknown error');
return " SKIPPED — {$what} needs a database ({$why}).\n"
. " Nothing here is reported as passing.\n";
}
/**
* Which driver the DSN names — for the handful of assertions that differ.
*/
public static function driver_code(): string
{
$dsn = self::env(self::DSN_VAR);
$pos = $dsn === null ? false : strpos($dsn, ':');
return $pos === false ? '' : substr((string) $dsn, 0, $pos);
}
private static ?string $last_error_c = null;
private static function env(string $name): ?string
{
$value = $_ENV[$name] ?? getenv($name);
return is_string($value) && trim($value) !== '' ? $value : null;
}
}