Translated using Weblate (Lithuanian)
[phpmyadmin.git] / test / classes / AbstractTestCase.php
blob8e44c67c96a017338bde4cc37f3249b38feffec7
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Cache;
8 use PhpMyAdmin\Config;
9 use PhpMyAdmin\Core;
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;
15 use PhpMyAdmin\Theme;
16 use PhpMyAdmin\ThemeManager;
17 use PhpMyAdmin\Utils\HttpRequest;
18 use PHPUnit\Framework\TestCase;
19 use ReflectionClass;
21 use function array_keys;
22 use function in_array;
24 use const DIRECTORY_SEPARATOR;
26 /**
27 * Abstract class to hold some usefull methods used in tests
28 * And make tests clean
30 abstract class AbstractTestCase extends TestCase
32 /**
33 * The variables to keep between tests
35 * @var string[]
37 private $globalsAllowList = [
38 '__composer_autoload_files',
39 'GLOBALS',
40 '_SERVER',
41 '__composer_autoload_files',
42 '__PHPUNIT_CONFIGURATION_FILE',
43 '__PHPUNIT_BOOTSTRAP',
46 /**
47 * The DatabaseInterface loaded by setGlobalDbi
49 * @var DatabaseInterface
51 protected $dbi;
53 /**
54 * The DbiDummy loaded by setGlobalDbi
56 * @var DbiDummy
58 protected $dummyDbi;
60 /**
61 * Prepares environment for the test.
62 * Clean all variables
64 protected function setUp(): void
66 foreach (array_keys($GLOBALS) as $key) {
67 if (in_array($key, $this->globalsAllowList)) {
68 continue;
71 unset($GLOBALS[$key]);
74 $_GET = [];
75 $_POST = [];
76 $_SERVER = [
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'];
85 $_COOKIE = [];
86 $_FILES = [];
87 $_REQUEST = [];
88 // Config before DBI
89 $this->setGlobalConfig();
90 $this->loadContainerBuilder();
91 $this->setGlobalDbi();
92 Cache::purge();
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();
104 $this->assertSame(
106 $unUsedSelects,
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
116 return;
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
196 global $dbi;
197 $this->dummyDbi = new DbiDummy();
198 $this->dbi = DatabaseInterface::load($this->dummyDbi);
199 $dbi = $this->dbi;
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
213 global $theme;
214 $theme = Theme::load(
215 ThemeManager::getThemesDir() . 'pmahomme',
216 ThemeManager::getThemesFsDir() . 'pmahomme' . DIRECTORY_SEPARATOR,
217 'pmahomme'
221 protected function setLanguage(string $code = 'en'): void
223 global $lang;
225 $lang = $code;
226 /* Ensure default language is active */
227 $languageEn = LanguageManager::getInstance()->getLanguage($code);
228 if ($languageEn === false) {
229 return;
232 $languageEn->activate();
233 Translator::load();
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)) {
249 continue;
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);