PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/classes/AdvisorTest.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 332 lines | 302 code | 10 blank | 20 comment | 9 complexity | a149b2c7746bf1522d373705b48e83f6 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Tests;
  4. use PhpMyAdmin\Advisor;
  5. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  6. /**
  7. * @covers \PhpMyAdmin\Advisor
  8. */
  9. class AdvisorTest extends AbstractTestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. parent::setGlobalConfig();
  15. $GLOBALS['server'] = 1;
  16. }
  17. /**
  18. * test for Advisor::byTime
  19. *
  20. * @param float $time time
  21. * @param string $expected expected result
  22. *
  23. * @dataProvider advisorTimes
  24. */
  25. public function testAdvisorBytime(float $time, string $expected): void
  26. {
  27. $result = Advisor::byTime($time, 2);
  28. $this->assertEquals($expected, $result);
  29. }
  30. public function advisorTimes(): array
  31. {
  32. return [
  33. [
  34. 10,
  35. '10 per second',
  36. ],
  37. [
  38. 0.02,
  39. '1.2 per minute',
  40. ],
  41. [
  42. 0.003,
  43. '10.8 per hour',
  44. ],
  45. [
  46. 0.00003,
  47. '2.59 per day',
  48. ],
  49. [
  50. 0.0000000003,
  51. '<0.01 per day',
  52. ],
  53. ];
  54. }
  55. /**
  56. * Test for adding rule
  57. *
  58. * @param array $rule Rule to test
  59. * @param array $expected Expected rendered rule in fired/errors list
  60. * @param string|null $error Expected error string (null if none error expected)
  61. *
  62. * @dataProvider rulesProvider
  63. */
  64. public function testAddRule(array $rule, array $expected, ?string $error): void
  65. {
  66. parent::setLanguage();
  67. $advisor = new Advisor($GLOBALS['dbi'], new ExpressionLanguage());
  68. $parseResult = include ROOT_PATH . 'libraries/advisory_rules_generic.php';
  69. $this->assertIsArray($parseResult);
  70. $this->assertArrayHasKey(0, $parseResult);
  71. $this->assertIsArray($parseResult[0]);
  72. $advisor->setVariable('value', 0);
  73. $advisor->addRule('fired', $rule);
  74. $runResult = $advisor->getRunResult();
  75. if (isset($runResult['errors']) || $error !== null) {
  76. $this->assertEquals([$error], $runResult['errors']);
  77. }
  78. if (! isset($runResult['fired']) && $expected == []) {
  79. return;
  80. }
  81. $this->assertEquals([$expected], $runResult['fired']);
  82. }
  83. public function rulesProvider(): array
  84. {
  85. return [
  86. [
  87. [
  88. 'id' => 'Basic',
  89. 'justification' => 'foo',
  90. 'name' => 'Basic',
  91. 'issue' => 'issue',
  92. 'recommendation' => 'Recommend',
  93. ],
  94. [
  95. 'justification' => 'foo',
  96. 'id' => 'Basic',
  97. 'name' => 'Basic',
  98. 'issue' => 'issue',
  99. 'recommendation' => 'Recommend',
  100. ],
  101. null,
  102. ],
  103. [
  104. [
  105. 'id' => 'Variable',
  106. 'justification' => 'foo',
  107. 'name' => 'Variable',
  108. 'issue' => 'issue',
  109. 'recommendation' => 'Recommend {status_var}',
  110. ],
  111. [
  112. 'justification' => 'foo',
  113. 'id' => 'Variable',
  114. 'name' => 'Variable',
  115. 'issue' => 'issue',
  116. 'recommendation' => 'Recommend <a href="index.php?route=/server/variables&' .
  117. 'filter=status_var&lang=en">status_var</a>',
  118. ],
  119. null,
  120. ],
  121. [
  122. [
  123. 'id' => 'Format',
  124. 'justification' => '%s foo',
  125. 'justification_formula' => 'value',
  126. 'name' => 'Format',
  127. 'issue' => 'issue',
  128. 'recommendation' => 'Recommend',
  129. ],
  130. [
  131. 'justification' => '0 foo',
  132. 'justification_formula' => 'value',
  133. 'id' => 'Format',
  134. 'name' => 'Format',
  135. 'issue' => 'issue',
  136. 'recommendation' => 'Recommend',
  137. ],
  138. null,
  139. ],
  140. [
  141. [
  142. 'id' => 'Percent',
  143. 'justification' => '%s%% foo',
  144. 'justification_formula' => 'value',
  145. 'name' => 'Percent',
  146. 'issue' => 'issue',
  147. 'recommendation' => 'Recommend',
  148. ],
  149. [
  150. 'justification' => '0% foo',
  151. 'justification_formula' => 'value',
  152. 'id' => 'Percent',
  153. 'name' => 'Percent',
  154. 'issue' => 'issue',
  155. 'recommendation' => 'Recommend',
  156. ],
  157. null,
  158. ],
  159. [
  160. [
  161. 'id' => 'Double',
  162. 'justification' => '%s%% %d foo',
  163. 'justification_formula' => 'value, value',
  164. 'name' => 'Double',
  165. 'issue' => 'issue',
  166. 'recommendation' => 'Recommend',
  167. ],
  168. [
  169. 'justification' => '0% 0 foo',
  170. 'justification_formula' => 'value, value',
  171. 'id' => 'Double',
  172. 'name' => 'Double',
  173. 'issue' => 'issue',
  174. 'recommendation' => 'Recommend',
  175. ],
  176. null,
  177. ],
  178. [
  179. [
  180. 'id' => 'Quotes',
  181. 'justification' => '"\'foo',
  182. 'name' => 'Quotes',
  183. 'issue' => 'issue',
  184. 'recommendation' => 'Recommend"\'',
  185. ],
  186. [
  187. 'justification' => '"\'foo',
  188. 'id' => 'Quotes',
  189. 'name' => 'Quotes',
  190. 'issue' => 'issue',
  191. 'recommendation' => 'Recommend"\'',
  192. ],
  193. null,
  194. ],
  195. [
  196. [
  197. 'justification' => 'foo',
  198. 'justification_formula' => 'fsafdsa',
  199. 'name' => 'Failure',
  200. 'issue' => 'issue',
  201. 'recommendation' => 'Recommend',
  202. ],
  203. [],
  204. 'Failed formatting string for rule \'Failure\'. ' .
  205. 'Error when evaluating: Variable "fsafdsa" is not ' .
  206. 'valid around position 2 for expression `[fsafdsa]`.',
  207. ],
  208. [
  209. [
  210. 'id' => 'Distribution',
  211. 'justification' => 'Version string (%s)',
  212. 'justification_formula' => 'value',
  213. 'name' => 'Distribution',
  214. 'issue' => 'official MySQL binaries.',
  215. 'recommendation' => 'See <a href="https://example.com/">web</a>',
  216. ],
  217. [
  218. 'justification' => 'Version string (0)',
  219. 'justification_formula' => 'value',
  220. 'name' => 'Distribution',
  221. 'issue' => 'official MySQL binaries.',
  222. 'recommendation' => 'See <a href="./url.php?url=https%3A%2F%2F' .
  223. 'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>',
  224. 'id' => 'Distribution',
  225. ],
  226. null,
  227. ],
  228. [
  229. [
  230. 'id' => 'Distribution',
  231. 'justification' => 'Timestamp (%s)',
  232. 'justification_formula' => 'ADVISOR_timespanFormat(1377027)',
  233. 'name' => 'Distribution',
  234. 'issue' => 'official MySQL binaries.',
  235. 'recommendation' => 'See <a href="https://example.com/">web</a>',
  236. ],
  237. [
  238. 'justification' => 'Timestamp (15 days, 22 hours, 30 minutes and 27 seconds)',
  239. 'justification_formula' => 'ADVISOR_timespanFormat(1377027)',
  240. 'name' => 'Distribution',
  241. 'issue' => 'official MySQL binaries.',
  242. 'recommendation' => 'See <a href="./url.php?url=https%3A%2F%2F' .
  243. 'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>',
  244. 'id' => 'Distribution',
  245. ],
  246. null,
  247. ],
  248. [
  249. [
  250. 'id' => 'Distribution',
  251. 'justification' => 'Memory: %s',
  252. 'justification_formula' => 'ADVISOR_formatByteDown(1000000, 2, 2)',
  253. 'name' => 'Distribution',
  254. 'issue' => 'official MySQL binaries.',
  255. 'recommendation' => 'See <a href="https://example.com/">web</a> and'
  256. . ' <a href="https://example.com/">web2</a>',
  257. ],
  258. [
  259. 'justification' => 'Memory: 0.95 MiB',
  260. 'justification_formula' => 'ADVISOR_formatByteDown(1000000, 2, 2)',
  261. 'name' => 'Distribution',
  262. 'issue' => 'official MySQL binaries.',
  263. 'recommendation' => 'See <a href="./url.php?url=https%3A%2F%2F'
  264. . 'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>'
  265. . ' and <a href="./url.php?url=https%3A%2F%2Fexample.com%2F" target="_blank"'
  266. . ' rel="noopener noreferrer">web2</a>',
  267. 'id' => 'Distribution',
  268. ],
  269. null,
  270. ],
  271. [
  272. [
  273. 'id' => 'Distribution',
  274. 'justification' => 'Time: %s',
  275. 'justification_formula' => 'ADVISOR_bytime(0.02, 2)',
  276. 'name' => 'Distribution',
  277. 'issue' => '{long_query_time} is set to 10 seconds or more',
  278. 'recommendation' => 'See <a href=\'https://example.com/\'>web</a> and'
  279. . ' <a href=\'https://example.com/\'>web2</a>',
  280. ],
  281. [
  282. 'justification' => 'Time: 1.2 per minute',
  283. 'justification_formula' => 'ADVISOR_bytime(0.02, 2)',
  284. 'name' => 'Distribution',
  285. 'issue' => '<a href="index.php?route=/server/variables&filter=long_query_time&lang=en">'
  286. . 'long_query_time</a> is set to 10 seconds or more',
  287. 'recommendation' => 'See <a href="./url.php?url=https%3A%2F%2F'
  288. . 'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>'
  289. . ' and <a href="./url.php?url=https%3A%2F%2Fexample.com%2F" target="_blank"'
  290. . ' rel="noopener noreferrer">web2</a>',
  291. 'id' => 'Distribution',
  292. ],
  293. null,
  294. ],
  295. [
  296. [
  297. 'id' => 'Minor Version',
  298. 'justification' => 'Current version: %s',
  299. 'justification_formula' => 'value',
  300. 'name' => 'Minor Version',
  301. 'precondition' => '! fired(\'Release Series\')',
  302. 'issue' => 'Version less than 5.1.30',
  303. 'recommendation' => 'You should upgrade',
  304. 'formula' => 'version',
  305. 'test' => "substr(value,0,2) <= '5.' && substr(value,2,1) <= 1 && substr(value,4,2) < 30",
  306. ],
  307. [
  308. 'justification' => 'Current version: 0',
  309. 'justification_formula' => 'value',
  310. 'name' => 'Minor Version',
  311. 'issue' => 'Version less than 5.1.30',
  312. 'recommendation' => 'You should upgrade',
  313. 'id' => 'Minor Version',
  314. 'precondition' => '! fired(\'Release Series\')',
  315. 'formula' => 'version',
  316. 'test' => "substr(value,0,2) <= '5.' && substr(value,2,1) <= 1 && substr(value,4,2) < 30",
  317. ],
  318. null,
  319. ],
  320. ];
  321. }
  322. }