Skip to content

Commit c41f2cd

Browse files
committed
Tests: replace config with test config for database tests
Database is prevented from auto-connecting in all cases, as the test tables may not exist yet.
1 parent d524336 commit c41f2cd

1 file changed

Lines changed: 62 additions & 10 deletions

File tree

qa-tests/Q2A_TestsSetup.php

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,71 @@
22

33
class Q2A_TestsSetup
44
{
5-
private $useDatabase = true;
5+
private $useDatabase = false;
6+
private $qaConfig = __DIR__ . '/../qa-config.php';
7+
private $qaConfigBackup = __DIR__ . '/../qa-config.php.bak';
8+
private $phpunitConfig = __DIR__ . '/phpunit-qa-config.php';
69

710
public function run()
811
{
12+
if (!$this->databaseIsNeeded()) {
13+
$this->initialConfiguration();
14+
return;
15+
}
16+
17+
// If we're using the database, replace config with a stand-in before loading Q2A
18+
$this->useDatabase = true;
19+
$this->replaceConfig();
20+
// This ensures it's restored after PHPUnit finishes
21+
register_shutdown_function(function() {
22+
$this->restoreConfig();
23+
});
24+
925
$this->initialConfiguration();
1026
$this->recreateTables();
1127
$this->createContent();
1228
}
1329

30+
/**
31+
* Backup config file, overwrite with phpunit stand-in.
32+
*/
33+
private function replaceConfig()
34+
{
35+
if (!is_file($this->qaConfig)) {
36+
throw new Exception("Q2A config file {$this->qaConfig} could not be found.");
37+
}
38+
if (!is_file($this->phpunitConfig)) {
39+
throw new Exception("PHPUnit stand-in config file {$this->phpunitConfig} could not be found.");
40+
}
41+
42+
if (!copy($this->qaConfig, $this->qaConfigBackup)) {
43+
throw new Exception("Could not backup Q2A config file; tests will not be run.");
44+
}
45+
if (!copy($this->phpunitConfig, $this->qaConfig)) {
46+
throw new Exception("Could not replace Q2A config file with PHPUnit stand-in; tests will not be run.");
47+
}
48+
}
49+
50+
/**
51+
* Remove phpunit stand-in, restore original config file.
52+
*/
53+
private function restoreConfig()
54+
{
55+
if (!is_file($this->qaConfigBackup)) {
56+
throw new Exception("Q2A config backup file {$this->qaConfigBackup} could not be found.");
57+
}
58+
59+
if (copy($this->qaConfigBackup, $this->qaConfig)) {
60+
unlink($this->qaConfigBackup);
61+
}
62+
}
63+
1464
private function initialConfiguration()
1565
{
16-
$this->disableDatabaseIfNeeded();
66+
// Prevent accessing database before we're ready (required in the case of the test database not set up yet)
67+
global $qa_options_cache, $qa_autoconnect;
68+
$qa_autoconnect = false;
69+
$qa_options_cache['enabled_plugins'] = '';
1770

1871
// currently, all Q2A code depends on qa-base
1972
require_once __DIR__ . '/../qa-include/qa-base.php';
@@ -29,23 +82,22 @@ private function initialConfiguration()
2982

3083
/**
3184
* Detect whether Q2A should access the database while testing or not. This reads the command line
32-
* used to run PHPUnit and, if the database tests were explicitly excluded there, then the database
33-
* access is disabled. These are two example command lines with and without the exclusion:
85+
* used to run PHPUnit, and if the database tests were explicitly excluded there then the test
86+
* database is not generated. These are two example command lines with and without the exclusion:
3487
* `phpunit --bootstrap qa-tests/autoload.php qa-tests`
3588
* `phpunit --bootstrap qa-tests/autoload.php --exclude-group database qa-tests`
3689
*/
37-
private function disableDatabaseIfNeeded()
90+
private function databaseIsNeeded()
3891
{
39-
global $qa_options_cache, $qa_autoconnect, $argv;
92+
global $argv;
4093

4194
foreach ($argv as $index => $arg) {
4295
if ($arg === '--exclude-group' && isset($argv[$index + 1]) && $argv[$index + 1] === 'database') {
43-
$qa_autoconnect = false;
44-
$qa_options_cache['enabled_plugins'] = '';
45-
$this->useDatabase = false;
46-
break;
96+
return false;
4797
}
4898
}
99+
100+
return true;
49101
}
50102

51103
private function recreateTables()

0 commit comments

Comments
 (0)