Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#915)
by
unknown
03:35
created

BaseIntegrationSpec::getMysqlConnection()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 24
rs 9.6
1
<?php
2
/**
3
 * > set global general_log_file = "/var/log/mysql/queries.log";
4
 * > set global general_log = "ON";
5
 * [wait some time, hit some pages, whatever]
6
 * > set global general_log = "OFF";
7
 */
8
9
namespace SmrTest;
10
11
use mysqli;
12
use MySqlProperties;
13
use PHPUnit\Framework\TestCase;
14
use RuntimeException;
15
use Throwable;
16
17
18
class BaseIntegrationSpec extends TestCase {
19
	protected static mysqli $conn;
20
	private static $defaultPopulatedTables = array();
21
	private const MYSQL_CONNECTION_ATTEMPTS = 10;
22
	private const MYSQL_CONNECTION_RETRY_SECONDS = 5;
23
24
	public static function setUpBeforeClass(): void {
25
		$mysqlProperties = new MySqlProperties();
26
		if (self::$conn = self::getMysqlConnection($mysqlProperties)) {
27
			// Reset environment variables for flyway. Unfortunately adding -e to the docker-compose command does not take precedence.
28
			putenv("MYSQL_PORT=3306");
29
			exec("docker-compose run --rm flyway-integration-test 1>&2");
30
			$query = "SELECT table_name FROM information_schema.tables WHERE table_rows > 0 AND TABLE_SCHEMA='smr_live'";
31
			$rs = self::$conn->query($query);
32
			$all = $rs->fetch_all();
33
			array_walk_recursive($all, function ($a) {
34
				self::$defaultPopulatedTables[] = "'" . $a . "'";
35
			});
36
		}
37
	}
38
39
	private static function getMysqlConnection(MySqlProperties $mysqlProperties, $attempt = 0): mysqli {
40
		putenv("MYSQL_HOST=smr-mysql");
41
		while ($attempt < self::MYSQL_CONNECTION_ATTEMPTS) {
42
			print "#${attempt}: Attempting to connect to MySQL on " . $mysqlProperties->getHost() . ":" . $mysqlProperties->getPort() . "...\n";
43
			$conn = mysqli_connect(
44
				$mysqlProperties->getHost(),
45
				$mysqlProperties->getUser(),
46
				$mysqlProperties->getPassword(),
47
				$mysqlProperties->getDatabaseName(),
48
				$mysqlProperties->getPort());
49
			if ($conn) {
50
				print "Connection successful.\n";
51
				return $conn;
52
			}
53
			if ($attempt == 0) {
54
				print "Starting up mysql-integration-test container...\n";
55
				exec("docker-compose up -d mysql-integration-test 1>&2");
56
			}
57
			$attempt += 1;
58
			print "Attempt failed -- retrying in " . self::MYSQL_CONNECTION_RETRY_SECONDS . " seconds...\n";
59
			sleep(self::MYSQL_CONNECTION_RETRY_SECONDS);
60
			return self::getMysqlConnection($mysqlProperties, $attempt);
61
		}
62
		throw new RuntimeException("Could not reach MySQL after $attempt tries.");
63
	}
64
65
	protected function onNotSuccessfulTest(Throwable $t): void {
66
		$this->cleanUp();
67
		throw $t;
68
	}
69
70
	protected function tearDown(): void {
71
		$this->cleanUp();
72
	}
73
74
	private function cleanUp() {
75
		echo "Cleaning non-default populated tables for next test...\n";
76
		$implode = implode(",", self::$defaultPopulatedTables);
77
		$query = "SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'smr_live' and TABLE_NAME not in (${implode})";
78
		$rs = self::$conn->query($query);
79
		$all = $rs->fetch_all();
80
		foreach ($all as $truncate) {
81
			self::$conn->query($truncate[0]);
82
		}
83
	}
84
}
85