3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
8 use PhpMyAdmin\DatabaseInterface
;
9 use PhpMyAdmin\Language
;
10 use PhpMyAdmin\LanguageManager
;
11 use PhpMyAdmin\SqlParser\Translator
;
12 use PhpMyAdmin\Tests\Stubs\DbiDummy
;
14 use PHPUnit\Framework\TestCase
;
20 use function in_array
;
21 use function is_array
;
22 use function parse_url
;
25 * Abstract class to hold some usefull methods used in tests
26 * And make tests clean
28 abstract class AbstractTestCase
extends TestCase
31 * The variables to keep between tests
35 private $globalsAllowList = [
36 '__composer_autoload_files',
39 '__composer_autoload_files',
40 '__PHPUNIT_CONFIGURATION_FILE',
41 '__PHPUNIT_BOOTSTRAP',
45 * Prepares environment for the test.
48 protected function setUp(): void
50 foreach ($GLOBALS as $key => $val) {
51 if (in_array($key, $this->globalsAllowList
)) {
54 unset($GLOBALS[$key]);
59 // https://github.com/sebastianbergmann/phpunit/issues/4033
60 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'],
61 'REQUEST_TIME' => $_SERVER['REQUEST_TIME'],
62 'REQUEST_TIME_FLOAT' => $_SERVER['REQUEST_TIME_FLOAT'],
63 'PHP_SELF' => $_SERVER['PHP_SELF'],
64 'argv' => $_SERVER['argv'],
66 $_SESSION = [' PMA_token ' => 'token'];
71 $this->setGlobalConfig();
72 $this->setGlobalDbi();
75 protected function loadDefaultConfig(): void
79 require ROOT_PATH
. 'libraries/config.default.php';
82 protected function setGlobalDbi(): void
85 $dbi = DatabaseInterface
::load(new DbiDummy());
88 protected function setGlobalConfig(): void
91 $PMA_Config = new Config();
92 $PMA_Config->set('environment', 'development');
95 protected function setTheme(): void
98 $PMA_Theme = Theme
::load('./themes/pmahomme', ROOT_PATH
. 'themes/pmahomme/');
101 protected function setLanguage(string $code = 'en'): void
106 /* Ensure default language is active */
107 /** @var Language $languageEn */
108 $languageEn = LanguageManager
::getInstance()->getLanguage($code);
109 $languageEn->activate();
113 protected function setProxySettings(): void
115 $httpProxy = getenv('http_proxy');
116 $urlInfo = parse_url((string) $httpProxy);
117 if (PHP_SAPI
=== 'cli' && is_array($urlInfo)) {
118 $proxyUrl = ($urlInfo['host'] ??
'')
119 . (isset($urlInfo['port']) ?
':' . $urlInfo['port'] : '');
120 $proxyUser = $urlInfo['user'] ??
'';
121 $proxyPass = $urlInfo['pass'] ??
'';
123 $GLOBALS['cfg']['ProxyUrl'] = $proxyUrl;
124 $GLOBALS['cfg']['ProxyUser'] = $proxyUser;
125 $GLOBALS['cfg']['ProxyPass'] = $proxyPass;
128 // phpcs:disable PSR1.Files.SideEffects
129 if (! defined('PROXY_URL')) {
130 define('PROXY_URL', $proxyUrl ??
'');
131 define('PROXY_USER', $proxyUser ??
'');
132 define('PROXY_PASS', $proxyPass ??
'');
137 protected function defineVersionConstants(): void
140 // Initialize PMA_VERSION variable
141 // phpcs:disable PSR1.Files.SideEffects
142 if (! defined('PMA_VERSION')) {
143 define('PMA_VERSION', $PMA_Config->get('PMA_VERSION'));
144 define('PMA_MAJOR_VERSION', $PMA_Config->get('PMA_MAJOR_VERSION'));
150 * Desctroys the environment built for the test.
151 * Clean all variables
153 protected function tearDown(): void
155 foreach ($GLOBALS as $key => $val) {
156 if (in_array($key, $this->globalsAllowList
)) {
159 unset($GLOBALS[$key]);
164 * Call protected functions by setting visibility to public.
166 * @param object|null $object The object to inspect, pass null for static objects()
167 * @param string $className The class name
168 * @param string $methodName The method name
169 * @param array $params The parameters for the invocation
171 * @return mixed the output from the protected method.
173 * @phpstan-param class-string $className
175 protected function callFunction($object, string $className, string $methodName, array $params)
177 $class = new ReflectionClass($className);
178 $method = $class->getMethod($methodName);
179 $method->setAccessible(true);
181 return $method->invokeArgs($object, $params);