3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
10 use PhpMyAdmin\DatabaseInterface
;
11 use PhpMyAdmin\LanguageManager
;
12 use PhpMyAdmin\SqlParser\Translator
;
13 use PhpMyAdmin\Tests\Stubs\DbiDummy
;
14 use PhpMyAdmin\Tests\Stubs\ResponseRenderer
;
16 use PhpMyAdmin\ThemeManager
;
17 use PhpMyAdmin\Utils\HttpRequest
;
18 use PHPUnit\Framework\TestCase
;
21 use function array_keys
;
22 use function in_array
;
24 use const DIRECTORY_SEPARATOR
;
27 * Abstract class to hold some usefull methods used in tests
28 * And make tests clean
30 abstract class AbstractTestCase
extends TestCase
33 * The variables to keep between tests
37 private $globalsAllowList = [
38 '__composer_autoload_files',
41 '__composer_autoload_files',
42 '__PHPUNIT_CONFIGURATION_FILE',
43 '__PHPUNIT_BOOTSTRAP',
47 * The DatabaseInterface loaded by setGlobalDbi
49 * @var DatabaseInterface
54 * The DbiDummy loaded by setGlobalDbi
61 * Prepares environment for the test.
64 protected function setUp(): void
66 foreach (array_keys($GLOBALS) as $key) {
67 if (in_array($key, $this->globalsAllowList
)) {
71 unset($GLOBALS[$key]);
77 // https://github.com/sebastianbergmann/phpunit/issues/4033
78 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'],
79 'REQUEST_TIME' => $_SERVER['REQUEST_TIME'],
80 'REQUEST_TIME_FLOAT' => $_SERVER['REQUEST_TIME_FLOAT'],
81 'PHP_SELF' => $_SERVER['PHP_SELF'],
82 'argv' => $_SERVER['argv'],
84 $_SESSION = [' PMA_token ' => 'token'];
89 $this->setGlobalConfig();
90 $this->loadContainerBuilder();
91 $this->setGlobalDbi();
95 protected function assertAllQueriesConsumed(): void
97 $unUsedQueries = $this->dummyDbi
->getUnUsedQueries();
98 $this->assertSame([], $unUsedQueries, 'Some queries where not used !');
101 protected function assertAllSelectsConsumed(): void
103 $unUsedSelects = $this->dummyDbi
->getUnUsedDatabaseSelects();
107 'Some database selects where not used !'
111 protected function assertAllErrorCodesConsumed(): void
113 if ($this->dummyDbi
->hasUnUsedErrors() === false) {
114 $this->assertTrue(true);// increment the assertion count
119 $this->fail('Some error codes where not used !');
122 protected function loadContainerBuilder(): void
124 global $containerBuilder;
126 $containerBuilder = Core
::getContainerBuilder();
129 protected function loadDbiIntoContainerBuilder(): void
131 global $containerBuilder, $dbi;
133 $containerBuilder->set(DatabaseInterface
::class, $dbi);
134 $containerBuilder->setAlias('dbi', DatabaseInterface
::class);
137 protected function loadResponseIntoContainerBuilder(): void
139 global $containerBuilder;
141 $response = new ResponseRenderer();
142 $containerBuilder->set(ResponseRenderer
::class, $response);
143 $containerBuilder->setAlias('response', ResponseRenderer
::class);
146 protected function setResponseIsAjax(): void
148 global $containerBuilder;
150 /** @var ResponseRenderer $response */
151 $response = $containerBuilder->get(ResponseRenderer
::class);
153 $response->setAjax(true);
156 protected function getResponseHtmlResult(): string
158 global $containerBuilder;
160 /** @var ResponseRenderer $response */
161 $response = $containerBuilder->get(ResponseRenderer
::class);
163 return $response->getHTMLResult();
166 protected function getResponseJsonResult(): array
168 global $containerBuilder;
170 /** @var ResponseRenderer $response */
171 $response = $containerBuilder->get(ResponseRenderer
::class);
173 return $response->getJSONResult();
176 protected function assertResponseWasNotSuccessfull(): void
178 global $containerBuilder;
179 /** @var ResponseRenderer $response */
180 $response = $containerBuilder->get(ResponseRenderer
::class);
182 $this->assertFalse($response->hasSuccessState(), 'expected the request to fail');
185 protected function assertResponseWasSuccessfull(): void
187 global $containerBuilder;
188 /** @var ResponseRenderer $response */
189 $response = $containerBuilder->get(ResponseRenderer
::class);
191 $this->assertTrue($response->hasSuccessState(), 'expected the request not to fail');
194 protected function setGlobalDbi(): void
197 $this->dummyDbi
= new DbiDummy();
198 $this->dbi
= DatabaseInterface
::load($this->dummyDbi
);
202 protected function setGlobalConfig(): void
204 global $config, $cfg;
205 $config = new Config();
206 $config->checkServers();
207 $config->set('environment', 'development');
208 $cfg = $config->settings
;
211 protected function setTheme(): void
214 $theme = Theme
::load(
215 ThemeManager
::getThemesDir() . 'pmahomme',
216 ThemeManager
::getThemesFsDir() . 'pmahomme' . DIRECTORY_SEPARATOR
,
221 protected function setLanguage(string $code = 'en'): void
226 /* Ensure default language is active */
227 $languageEn = LanguageManager
::getInstance()->getLanguage($code);
228 if ($languageEn === false) {
232 $languageEn->activate();
236 protected function setProxySettings(): void
238 HttpRequest
::setProxySettingsFromEnv();
242 * Destroys the environment built for the test.
243 * Clean all variables
245 protected function tearDown(): void
247 foreach (array_keys($GLOBALS) as $key) {
248 if (in_array($key, $this->globalsAllowList
)) {
252 unset($GLOBALS[$key]);
257 * Call protected functions by setting visibility to public.
259 * @param object|null $object The object to inspect, pass null for static objects()
260 * @param string $className The class name
261 * @param string $methodName The method name
262 * @param array $params The parameters for the invocation
263 * @phpstan-param class-string $className
265 * @return mixed the output from the protected method.
267 protected function callFunction($object, string $className, string $methodName, array $params)
269 $class = new ReflectionClass($className);
270 $method = $class->getMethod($methodName);
271 $method->setAccessible(true);
273 return $method->invokeArgs($object, $params);