Translated using Weblate (Lithuanian)
[phpmyadmin.git] / test / classes / VersionInformationTest.php
blob014ebeb90a43aeace40f73c0f84d35abaa17ca33
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\VersionInformation;
8 use stdClass;
10 use function count;
12 /**
13 * @covers \PhpMyAdmin\VersionInformation
15 class VersionInformationTest extends AbstractTestCase
17 /** @var stdClass[] */
18 private $releases;
20 /**
21 * Sets up the fixture, for example, opens a network connection.
22 * This method is called before a test is executed.
24 protected function setUp(): void
26 parent::setUp();
27 parent::setProxySettings();
28 $this->releases = [];
30 // phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
31 $release = new stdClass();
32 $release->date = '2015-09-08';
33 $release->php_versions = '>=5.3,<7.1';
34 $release->version = '4.4.14.1';
35 $release->mysql_versions = '>=5.5';
36 $this->releases[] = $release;
38 $release = new stdClass();
39 $release->date = '2015-09-09';
40 $release->php_versions = '>=5.3,<7.0';
41 $release->version = '4.4.13.3';
42 $release->mysql_versions = '>=5.5';
43 $this->releases[] = $release;
45 $release = new stdClass();
46 $release->date = '2015-05-13';
47 $release->php_versions = '>=5.2,<5.3';
48 $release->version = '4.0.10.10';
49 $release->mysql_versions = '>=5.0';
50 $this->releases[] = $release;
51 // phpcs:enable
54 /**
55 * Test version checking
57 * @group large
58 * @group network
60 public function testGetLatestVersion(): void
62 $this->setProxySettings();
63 $GLOBALS['cfg']['VersionCheck'] = true;
64 $versionInformation = new VersionInformation();
65 $version = $versionInformation->getLatestVersion();
66 $this->assertIsObject($version);
67 $this->assertNotEmpty($version->version);
68 $this->assertNotEmpty($version->date);
71 /**
72 * Test version to int conversion.
74 * @param string $version Version string
75 * @param int $numeric Integer matching version
77 * @dataProvider dataVersions
79 public function testVersionToInt(string $version, int $numeric): void
81 $versionInformation = new VersionInformation();
82 $this->assertEquals(
83 $numeric,
84 $versionInformation->versionToInt($version)
88 /**
89 * Data provider for version parsing
91 public function dataVersions(): array
93 return [
95 '1.0.0',
96 1000050,
99 '2.0.0.2-dev',
100 2000002,
103 '3.4.2.1',
104 3040251,
107 '3.4.2-dev3',
108 3040203,
111 '3.4.2-dev',
112 3040200,
115 '3.4.2-pl',
116 3040260,
119 '3.4.2-pl3',
120 3040263,
123 '4.4.2-rc22',
124 4040252,
127 '4.4.2-rc',
128 4040230,
131 '4.4.22-beta22',
132 4042242,
135 '4.4.22-beta',
136 4042220,
139 '4.4.21-alpha22',
140 4042132,
143 '4.4.20-alpha',
144 4042010,
147 '4.40.20-alpha-dev',
148 4402010,
151 '4.4a',
152 4000050,
155 '4.4.4-test',
156 4040400,
159 '4.1.0',
160 4010050,
163 '4.0.1.3',
164 4000153,
167 '4.1-dev',
168 4010000,
174 * Tests getLatestCompatibleVersion() when there is only one server configured
176 public function testGetLatestCompatibleVersionWithSingleServer(): void
178 $GLOBALS['cfg']['Servers'] = [
182 $mockVersionInfo = $this->getMockBuilder(VersionInformation::class)
183 ->onlyMethods(['evaluateVersionCondition'])
184 ->getMock();
186 $mockVersionInfo->expects($this->exactly(9))
187 ->method('evaluateVersionCondition')
188 ->withConsecutive(
189 ['PHP', '>=5.3'],
190 ['PHP', '<7.1'],
191 ['MySQL', '>=5.5'],
192 ['PHP', '>=5.3'],
193 ['PHP', '<7.0'],
194 ['MySQL', '>=5.5'],
195 ['PHP', '>=5.2'],
196 ['PHP', '<5.3'],
197 ['MySQL', '>=5.0']
199 ->willReturnOnConsecutiveCalls(true, true, true, true, true, true, true, true, true);
201 $compatible = $mockVersionInfo->getLatestCompatibleVersion($this->releases);
202 $this->assertIsArray($compatible);
203 $this->assertEquals('4.4.14.1', $compatible['version']);
207 * Tests getLatestCompatibleVersion() when there are multiple servers configured
209 public function testGetLatestCompatibleVersionWithMultipleServers(): void
211 $GLOBALS['cfg']['Servers'] = [
216 $mockVersionInfo = $this->getMockBuilder(VersionInformation::class)
217 ->onlyMethods(['evaluateVersionCondition'])
218 ->getMock();
220 $mockVersionInfo->expects($this->atLeast(4))
221 ->method('evaluateVersionCondition')
222 ->withConsecutive(
223 ['PHP', '>=5.3'],
224 ['PHP', '<7.1']
226 ->willReturnOnConsecutiveCalls(true, true);
228 $compatible = $mockVersionInfo->getLatestCompatibleVersion($this->releases);
229 $this->assertIsArray($compatible);
230 $this->assertEquals('4.4.14.1', $compatible['version']);
234 * Tests getLatestCompatibleVersion() with an old PHP version
236 public function testGetLatestCompatibleVersionWithOldPHPVersion(): void
238 $GLOBALS['cfg']['Servers'] = [
243 $mockVersionInfo = $this->getMockBuilder(VersionInformation::class)
244 ->onlyMethods(['evaluateVersionCondition'])
245 ->getMock();
247 $mockVersionInfo->expects($this->atLeast(2))
248 ->method('evaluateVersionCondition')
249 ->withConsecutive(
250 ['PHP', '>=5.3'],
251 ['PHP', '>=5.3'],
252 ['PHP', '>=5.2'],
253 ['PHP', '<5.3']
255 ->willReturnOnConsecutiveCalls(false, false, true, true);
257 $compatible = $mockVersionInfo->getLatestCompatibleVersion($this->releases);
258 $this->assertIsArray($compatible);
259 $this->assertEquals('4.0.10.10', $compatible['version']);
263 * Tests getLatestCompatibleVersion() with an new PHP version
265 * @param array[] $versions The versions to use
266 * @param array[] $conditions The conditions that will be executed
267 * @param string|null $matchedLastVersion The version that will be matched
269 * @dataProvider dataProviderVersionConditions
271 public function testGetLatestCompatibleVersionWithNewPHPVersion(
272 array $versions,
273 array $conditions,
274 ?string $matchedLastVersion
275 ): void {
276 $GLOBALS['cfg']['Servers'] = [];
278 $mockVersionInfo = $this->getMockBuilder(VersionInformation::class)
279 ->onlyMethods(['evaluateVersionCondition'])
280 ->getMock();
282 $conditionsCalls = [];
283 $returnValues = [];
284 foreach ($conditions as $conditionArray) {
286 $condition,
287 $returnValue,
288 ] = $conditionArray;
289 $conditionsCalls[] = ['PHP', $condition];
290 $returnValues[] = $returnValue;
293 $mockVersionInfo->expects($this->exactly(count($conditionsCalls)))
294 ->method('evaluateVersionCondition')
295 ->withConsecutive(...$conditionsCalls)
296 ->willReturnOnConsecutiveCalls(...$returnValues);
298 $compatible = $mockVersionInfo->getLatestCompatibleVersion($versions);
299 $this->assertEquals($matchedLastVersion, $compatible['version'] ?? null);
303 * Provider for testGetLatestCompatibleVersionWithNewPHPVersion
304 * Returns the conditions to be used for mocks
306 * @return array[]
308 public function dataProviderVersionConditions(): array
310 return [
313 ((object) [
314 'date' => '2019-12-26',
315 'php_versions' => '>=5.5,<8.0',
316 'version' => '4.9.3',
317 'mysql_versions' => '>=5.5',
319 ((object) [
320 'date' => '2019-12-26',
321 'php_versions' => '>=7.1,<8.0',
322 'version' => '5.0.0',
323 'mysql_versions' => '>=5.5',
328 '>=5.5',
329 true,
332 '<8.0',
333 true,
336 '>=7.1',
337 true,
340 '<8.0',
341 false,
344 '4.9.3',
348 ((object) [
349 'date' => '2019-12-26',
350 'php_versions' => '>=5.5,<7.0',
351 'version' => '6.0.0',
352 'mysql_versions' => '>=5.5',
354 ((object) [
355 'date' => '2019-12-26',
356 'php_versions' => '>=7.1,<8.0',
357 'version' => '5.0.0',
358 'mysql_versions' => '>=5.5',
363 '>=5.5',
364 true,
367 '<7.0',
368 true,
371 '>=7.1',
372 false,
375 '6.0.0',
379 ((object) [
380 'date' => '2019-12-26',
381 'php_versions' => '>=5.5,<7.0',
382 'version' => '6.0.0-rc1',
383 'mysql_versions' => '>=5.5',
385 ((object) [
386 'date' => '2019-12-26',
387 'php_versions' => '>=7.1,<8.0',
388 'version' => '6.0.0-rc2',
389 'mysql_versions' => '>=5.5',
394 '>=5.5',
395 true,
398 '<7.0',
399 true,
402 '>=7.1',
403 false,
406 '6.0.0-rc1',
410 ((object) [
411 'date' => '2019-12-26',
412 'php_versions' => '>=5.5,<7.0',
413 'version' => '6.0.0',
414 'mysql_versions' => '>=5.5',
416 ((object) [
417 'date' => '2019-12-26',
418 'php_versions' => '>=7.1,<8.0',
419 'version' => '5.0.0',
420 'mysql_versions' => '>=5.5',
425 '>=5.5',
426 false,
429 '>=7.1',
430 true,
433 '<8.0',
434 false,
437 null,
441 ((object) [
442 'date' => '2019-12-26',
443 'php_versions' => '>=5.5,<7.0',
444 'version' => '6.0.0',
445 'mysql_versions' => '>=5.5',
447 ((object) [
448 'date' => '2019-12-26',
449 'php_versions' => '>=7.1,<8.0',
450 'version' => '5.0.0',
451 'mysql_versions' => '>=5.5',
456 '>=5.5',
457 false,
460 '>=7.1',
461 true,
464 '<8.0',
465 true,
468 '5.0.0',
472 ((object) [
473 'date' => '2019-12-26',
474 'php_versions' => '>=5.5,<8.0',
475 'version' => '4.9.3',
476 'mysql_versions' => '>=5.5',
478 ((object) [
479 'date' => '2019-12-26',
480 'php_versions' => '>=7.1,<8.0',
481 'version' => '5.0.0',
482 'mysql_versions' => '>=5.5',
487 '>=5.5',
488 true,
491 '<8.0',
492 true,
495 '>=7.1',
496 true,
499 '<8.0',
500 true,
503 '5.0.0',
507 ((object) [
508 'date' => '2019-12-26',
509 'php_versions' => '>=7.1,<8.0',
510 'version' => '5.0.0',
511 'mysql_versions' => '>=5.5',
513 ((object) [
514 'date' => '2019-12-26',
515 'php_versions' => '>=5.5,<8.0',
516 'version' => '4.9.3',
517 'mysql_versions' => '>=5.5',
522 '>=7.1',
523 true,
526 '<8.0',
527 true,
530 '>=5.5',
531 true,
534 '<8.0',
535 true,
538 '5.0.0',
544 * Tests evaluateVersionCondition() method
546 public function testEvaluateVersionCondition(): void
548 $mockVersionInfo = $this->getMockBuilder(VersionInformation::class)
549 ->onlyMethods(['getPHPVersion'])
550 ->getMock();
552 $mockVersionInfo->expects($this->any())
553 ->method('getPHPVersion')
554 ->will($this->returnValue('5.2.4'));
556 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '<=5.3'));
557 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '<5.3'));
558 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '>=5.2'));
559 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '>5.2'));
560 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '!=5.3'));
562 $this->assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '<=5.2'));
563 $this->assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '<5.2'));
564 $this->assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '>=7.0'));
565 $this->assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '>7.0'));
566 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '!=5.2'));