Skip to content

Commit 0e0dd66

Browse files
committed
Minimum PHP 8.2 refactoring, updated db versions for multi-db testing
1 parent ad6e7d5 commit 0e0dd66

File tree

1 file changed

+327
-0
lines changed

1 file changed

+327
-0
lines changed
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
#!/usr/bin/env php
2+
<?php
3+
function readFromLine( $prompt = '' ) {
4+
5+
echo $prompt;
6+
return trim(rtrim( fgets( STDIN ), PHP_EOL ));
7+
}
8+
9+
function sleepWithEcho(int $seconds) {
10+
11+
echo 'Sleeping....' . PHP_EOL;
12+
sleep($seconds);
13+
echo 'Waking....' . PHP_EOL;
14+
}
15+
16+
function echoWithLineBreaks(string $str): void {
17+
18+
echo PHP_EOL . $str . PHP_EOL;
19+
}
20+
21+
function readableElapsedTime($microtime, $format = null, $round = 3) {
22+
23+
if (is_null($format)) {
24+
$format = '%.3f%s';
25+
}
26+
27+
if ($microtime >= 3600) {
28+
29+
$unit = ' hour(s)';
30+
$time = round(($microtime / 3600), $round);
31+
32+
} elseif ($microtime >= 60) {
33+
34+
$unit = ' minute(s)';
35+
$time = round(($microtime / 60), $round);
36+
37+
} elseif ($microtime >= 1) {
38+
39+
$unit = ' second(s)';
40+
$time = round($microtime, $round);
41+
42+
} else {
43+
44+
$unit = 'ms';
45+
$time = round($microtime*1000);
46+
47+
$format = preg_replace('/(%.[\d]+f)/', '%d', $format);
48+
}
49+
50+
return sprintf($format, $time, $unit);
51+
}
52+
53+
$new_line = PHP_EOL;
54+
echoWithLineBreaks(
55+
'WARNING: This test suite could take up to 4 hours to run depending on the'
56+
. ' performance capability of this computer.' . $new_line
57+
. "\t Podman also needs to be installed in order for the tests to run." . $new_line
58+
);
59+
60+
$console_prompt = "If you have an instance of (MySql / Mariadb) and / or Postgresql"
61+
. " running, please stop them and then press Enter.{$new_line}This"
62+
. " script will be spawning new container instances of MySql & Postgresql"
63+
. " that will be forwarding to ports 3306 and 5432 on this machine:";
64+
$console_response = readFromLine($console_prompt); // hitting enter returns an empty string,
65+
// maybe later other options could be read
66+
// into this variable
67+
68+
$console_prompt2 = "Please enter a password that would be used for the Mysql & Mariadb root accounts:";
69+
$mysql_root_psw = readFromLine(PHP_EOL . $console_prompt2);
70+
71+
$test_results = [];
72+
73+
$mysql_maria_db_sql_dsn = 'mysql:host=127.0.0.1;port=3306';
74+
$mysql_user = 'root';
75+
76+
$pgsql_dsn = 'pgsql:host=127.0.0.1;port=5432;user=postgres;password=doesntmatter;dbname=blog';
77+
$pgsql_user = 'postgres';
78+
$pgsql_pass = 'doesntmatter';
79+
80+
$runtime_name_placeholder= "{{runtime_name}}";
81+
82+
////////////////////////////////////////////////////////////////////////////////
83+
// This array should be updated periodically with new db image versions added
84+
// and old versions that are no longer supported removed.
85+
////////////////////////////////////////////////////////////////////////////////
86+
$container_creation_commands = [
87+
[
88+
'mysql:5.6.51' => [
89+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:5.6.51",
90+
'dsn' => $mysql_maria_db_sql_dsn,
91+
'username' => $mysql_user,
92+
'password' => $mysql_root_psw,
93+
],
94+
],
95+
[
96+
'postgres:12.22' => [
97+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:12.22",
98+
'dsn' => $pgsql_dsn,
99+
'username' => $pgsql_user,
100+
'password' => $pgsql_pass,
101+
],
102+
],
103+
[
104+
'mysql:5.7.44' => [
105+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:5.7.44",
106+
'dsn' => $mysql_maria_db_sql_dsn,
107+
'username' => $mysql_user,
108+
'password' => $mysql_root_psw,
109+
],
110+
],
111+
[
112+
'postgres:13.23' => [
113+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:13.23",
114+
'dsn' => $pgsql_dsn,
115+
'username' => $pgsql_user,
116+
'password' => $pgsql_pass,
117+
],
118+
],
119+
[
120+
'mysql:8.0.45' => [
121+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:8.0.45",
122+
'dsn' => $mysql_maria_db_sql_dsn,
123+
'username' => $mysql_user,
124+
'password' => $mysql_root_psw,
125+
],
126+
],
127+
[
128+
'postgres:14.20' => [
129+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:14.20",
130+
'dsn' => $pgsql_dsn,
131+
'username' => $pgsql_user,
132+
'password' => $pgsql_pass,
133+
],
134+
],
135+
[
136+
'mysql:8.4.8' => [
137+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:8.4.8",
138+
'dsn' => $mysql_maria_db_sql_dsn,
139+
'username' => $mysql_user,
140+
'password' => $mysql_root_psw,
141+
],
142+
],
143+
[
144+
'postgres:15.15' => [
145+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:15.15",
146+
'dsn' => $pgsql_dsn,
147+
'username' => $pgsql_user,
148+
'password' => $pgsql_pass,
149+
],
150+
],
151+
[
152+
'postgres:16.11' => [
153+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:16.11",
154+
'dsn' => $pgsql_dsn,
155+
'username' => $pgsql_user,
156+
'password' => $pgsql_pass,
157+
],
158+
],
159+
[
160+
'postgres:17.7' => [
161+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:17.7",
162+
'dsn' => $pgsql_dsn,
163+
'username' => $pgsql_user,
164+
'password' => $pgsql_pass,
165+
],
166+
],
167+
[
168+
'postgres:18.1' => [
169+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:18.1",
170+
'dsn' => $pgsql_dsn,
171+
'username' => $pgsql_user,
172+
'password' => $pgsql_pass,
173+
],
174+
],
175+
[
176+
'mariadb:10.4.34' => [
177+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:10.4.34",
178+
'dsn' => $mysql_maria_db_sql_dsn,
179+
'username' => $mysql_user,
180+
'password' => $mysql_root_psw,
181+
],
182+
],
183+
[
184+
'mariadb:10.5.29' => [
185+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:10.5.29",
186+
'dsn' => $mysql_maria_db_sql_dsn,
187+
'username' => $mysql_user,
188+
'password' => $mysql_root_psw,
189+
],
190+
],
191+
[
192+
'mariadb:10.6.25' => [
193+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:10.6.25",
194+
'dsn' => $mysql_maria_db_sql_dsn,
195+
'username' => $mysql_user,
196+
'password' => $mysql_root_psw,
197+
],
198+
],
199+
[
200+
'mariadb:10.11.16' => [
201+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:10.11.16",
202+
'dsn' => $mysql_maria_db_sql_dsn,
203+
'username' => $mysql_user,
204+
'password' => $mysql_root_psw,
205+
],
206+
],
207+
[
208+
'mariadb:11.4.10' => [
209+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:11.4.10",
210+
'dsn' => $mysql_maria_db_sql_dsn,
211+
'username' => $mysql_user,
212+
'password' => $mysql_root_psw,
213+
],
214+
],
215+
[
216+
'mariadb:11.8.6' => [
217+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:11.8.6",
218+
'dsn' => $mysql_maria_db_sql_dsn,
219+
'username' => $mysql_user,
220+
'password' => $mysql_root_psw,
221+
],
222+
],
223+
[
224+
'mariadb:12.2.2' => [
225+
'run_container' => "docker run --rm--name {$runtime_name_placeholder} -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:12.2.2",
226+
'dsn' => $mysql_maria_db_sql_dsn,
227+
'username' => $mysql_user,
228+
'password' => $mysql_root_psw,
229+
],
230+
],
231+
];
232+
233+
echo PHP_EOL . PHP_EOL . 'Starting to create containers and run tests....' . PHP_EOL . PHP_EOL;
234+
235+
$phpunit = __DIR__ . '/vendor/bin/phpunit --coverage-text --stop-on-error';
236+
$start_time = microtime(true);
237+
238+
foreach($container_creation_commands as $current_container_creation_command) {
239+
240+
$db_versions = '';
241+
$container_names_to_stop = [];
242+
243+
foreach($current_container_creation_command as $db_version => $command ) {
244+
245+
$db_versions .= $db_version . PHP_EOL;
246+
247+
$retval=null;
248+
$output=null;
249+
$run_container_command = \str_replace(
250+
$runtime_name_placeholder,
251+
$db_version,
252+
$command['run_container']
253+
);
254+
$container_names_to_stop[] = $db_versions;
255+
echoWithLineBreaks($run_container_command);
256+
exec($run_container_command, $output, $retval);
257+
258+
echo PHP_EOL . PHP_EOL;
259+
}
260+
261+
sleepWithEcho(90); // allow databases to load properly
262+
263+
echo PHP_EOL . PHP_EOL;
264+
265+
$output = null;
266+
$phpunit_retval = null;
267+
268+
// Print out current db versions being tested with
269+
echo "Testing against the following databases:" .PHP_EOL . $db_versions . PHP_EOL;
270+
271+
$phpunit_with_env =
272+
"LEANORM_PDO_DSN=\"{$command['dsn']}\" LEANORM_PDO_USERNAME={$command['username']} LEANORM_PDO_PASSWORD={$command['password']} {$phpunit}";
273+
274+
echoWithLineBreaks($phpunit_with_env);
275+
system($phpunit_with_env, $phpunit_retval);
276+
277+
echo "Stopping container instances & deleting their images" .PHP_EOL . PHP_EOL;
278+
279+
foreach($container_names_to_stop as $container_to_stop) {
280+
281+
echoWithLineBreaks("docker stop {$container_to_stop}");
282+
system("docker stop {$container_to_stop}"); // stop the containers
283+
284+
// remove their images from the machine to save disk space
285+
echoWithLineBreaks("docker image rm --force {$container_to_stop}");
286+
system("docker image rm --force {$container_to_stop}");
287+
}
288+
289+
if($phpunit_retval !== 0) {
290+
291+
////////////////////////////////////////////////////////////////////////
292+
// A PHPUnit Test failed.
293+
// if $phpunit_retval !== 0, test failed, stop containers and exit
294+
// see https://github.com/sebastianbergmann/phpunit/blob/10.5/src/TextUI/ShellExitCodeCalculator.php
295+
// for phpunit shell exit codes
296+
////////////////////////////////////////////////////////////////////////
297+
echo PHP_EOL . "Test failed for the following databases:" . PHP_EOL . $db_versions;
298+
echo PHP_EOL . 'Goodbye!'. PHP_EOL;
299+
exit(1);
300+
301+
} eLse {
302+
303+
$test_results[] = $db_versions;
304+
echo PHP_EOL;
305+
306+
} // if($phpunit_retval !== 0)
307+
} // foreach($postgres_and_mysql_container_creation_commands as $postgres_and_mysql_container_creation_command)
308+
309+
$end_time = microtime(true);
310+
311+
$elapsed = $end_time - $start_time;
312+
313+
if (count($test_results) > 0) {
314+
315+
echo PHP_EOL . PHP_EOL
316+
. "Test passed for the following databases:"
317+
. PHP_EOL . PHP_EOL;
318+
319+
foreach ($test_results as $test_result) {
320+
321+
echo $test_result . PHP_EOL;
322+
323+
} // foreach ($test_results as $test_result)
324+
} // if (count($test_results) > 0)
325+
326+
echo PHP_EOL . 'Time taken: ' . readableElapsedTime($elapsed). PHP_EOL. PHP_EOL;
327+
echo PHP_EOL . 'Goodbye!'. PHP_EOL;

0 commit comments

Comments
 (0)