Translated using Weblate (Slovenian)
[phpmyadmin.git] / test / classes / AbstractTestCase.php
blob5de0f805c0f2c456e1637e7fa64d5e1a4a2a29e3
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Config;
8 use PhpMyAdmin\DatabaseInterface;
9 use PhpMyAdmin\Language;
10 use PhpMyAdmin\LanguageManager;
11 use PhpMyAdmin\SqlParser\Translator;
12 use PhpMyAdmin\Tests\Stubs\DbiDummy;
13 use PhpMyAdmin\Theme;
14 use PHPUnit\Framework\TestCase;
15 use ReflectionClass;
16 use const PHP_SAPI;
17 use function define;
18 use function defined;
19 use function getenv;
20 use function in_array;
21 use function is_array;
22 use function parse_url;
24 /**
25 * Abstract class to hold some usefull methods used in tests
26 * And make tests clean
28 abstract class AbstractTestCase extends TestCase
30 /**
31 * The variables to keep between tests
33 * @var string[]
35 private $globalsAllowList = [
36 '__composer_autoload_files',
37 'GLOBALS',
38 '_SERVER',
39 '__composer_autoload_files',
40 '__PHPUNIT_CONFIGURATION_FILE',
41 '__PHPUNIT_BOOTSTRAP',
44 /**
45 * Prepares environment for the test.
46 * Clean all variables
48 protected function setUp(): void
50 foreach ($GLOBALS as $key => $val) {
51 if (in_array($key, $this->globalsAllowList)) {
52 continue;
54 unset($GLOBALS[$key]);
56 $_GET = [];
57 $_POST = [];
58 $_SERVER = [
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'];
67 $_COOKIE = [];
68 $_FILES = [];
69 $_REQUEST = [];
70 // Config before DBI
71 $this->setGlobalConfig();
72 $this->setGlobalDbi();
75 protected function loadDefaultConfig(): void
77 global $cfg;
79 require ROOT_PATH . 'libraries/config.default.php';
82 protected function setGlobalDbi(): void
84 global $dbi;
85 $dbi = DatabaseInterface::load(new DbiDummy());
88 protected function setGlobalConfig(): void
90 global $PMA_Config;
91 $PMA_Config = new Config();
92 $PMA_Config->set('environment', 'development');
95 protected function setTheme(): void
97 global $PMA_Theme;
98 $PMA_Theme = Theme::load('./themes/pmahomme', ROOT_PATH . 'themes/pmahomme/');
101 protected function setLanguage(string $code = 'en'): void
103 global $lang;
105 $lang = $code;
106 /* Ensure default language is active */
107 /** @var Language $languageEn */
108 $languageEn = LanguageManager::getInstance()->getLanguage($code);
109 $languageEn->activate();
110 Translator::load();
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 ?? '');
134 // phpcs:enable
137 protected function defineVersionConstants(): void
139 global $PMA_Config;
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'));
146 // phpcs:enable
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)) {
157 continue;
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);