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 PHPUnit\Framework\TestCase; |
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class BaseIntegrationTest extends TestCase { |
17
|
|
|
protected static mysqli $conn; |
18
|
|
|
private static $defaultPopulatedTables = array(); |
19
|
|
|
|
20
|
|
|
public static function setUpBeforeClass(): void { |
21
|
|
|
exec("docker-compose run --rm flyway-integration-test 1>&2"); |
22
|
|
|
$conn = self::$conn = mysqli_connect( |
23
|
|
|
constant("OVERRIDE_MYSQL_HOST"), |
24
|
|
|
constant("OVERRIDE_MYSQL_USER"), |
25
|
|
|
constant("OVERRIDE_MYSQL_PASSWORD"), |
26
|
|
|
"smr_live", |
27
|
|
|
(int)constant("OVERRIDE_MYSQL_PORT") |
28
|
|
|
); |
29
|
|
|
$query = "SELECT table_name FROM information_schema.tables WHERE table_rows > 0 AND TABLE_SCHEMA='smr_live'"; |
30
|
|
|
$rs = $conn->query($query); |
31
|
|
|
$all = $rs->fetch_all(); |
32
|
|
|
array_walk_recursive($all, function ($a) { |
33
|
|
|
self::$defaultPopulatedTables[] = "'" . $a . "'"; |
34
|
|
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function onNotSuccessfulTest(Throwable $t): void { |
38
|
|
|
$this->cleanUp(); |
39
|
|
|
throw $t; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function tearDown(): void { |
43
|
|
|
$this->cleanUp(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function cleanUp() { |
47
|
|
|
echo "Cleaning non-default populated tables for next test..."; |
48
|
|
|
$implode = implode(",", self::$defaultPopulatedTables); |
49
|
|
|
$query = "SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'smr_live' and TABLE_NAME not in (${implode})"; |
50
|
|
|
$rs = self::$conn->query($query); |
51
|
|
|
$all = $rs->fetch_all(); |
52
|
|
|
foreach ($all as $truncate) { |
53
|
|
|
self::$conn->query($truncate[0]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|