3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
7 use PhpMyAdmin\VersionInformation
;
13 * @covers \PhpMyAdmin\VersionInformation
15 class VersionInformationTest
extends AbstractTestCase
17 /** @var stdClass[] */
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
27 parent
::setProxySettings();
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;
55 * Test version checking
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
);
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();
84 $versionInformation->versionToInt($version)
89 * Data provider for version parsing
91 public function dataVersions(): array
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'])
186 $mockVersionInfo->expects($this->exactly(9))
187 ->method('evaluateVersionCondition')
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'])
220 $mockVersionInfo->expects($this->atLeast(4))
221 ->method('evaluateVersionCondition')
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'])
247 $mockVersionInfo->expects($this->atLeast(2))
248 ->method('evaluateVersionCondition')
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(
274 ?
string $matchedLastVersion
276 $GLOBALS['cfg']['Servers'] = [];
278 $mockVersionInfo = $this->getMockBuilder(VersionInformation
::class)
279 ->onlyMethods(['evaluateVersionCondition'])
282 $conditionsCalls = [];
284 foreach ($conditions as $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
308 public function dataProviderVersionConditions(): array
314 'date' => '2019-12-26',
315 'php_versions' => '>=5.5,<8.0',
316 'version' => '4.9.3',
317 'mysql_versions' => '>=5.5',
320 'date' => '2019-12-26',
321 'php_versions' => '>=7.1,<8.0',
322 'version' => '5.0.0',
323 'mysql_versions' => '>=5.5',
349 'date' => '2019-12-26',
350 'php_versions' => '>=5.5,<7.0',
351 'version' => '6.0.0',
352 'mysql_versions' => '>=5.5',
355 'date' => '2019-12-26',
356 'php_versions' => '>=7.1,<8.0',
357 'version' => '5.0.0',
358 'mysql_versions' => '>=5.5',
380 'date' => '2019-12-26',
381 'php_versions' => '>=5.5,<7.0',
382 'version' => '6.0.0-rc1',
383 'mysql_versions' => '>=5.5',
386 'date' => '2019-12-26',
387 'php_versions' => '>=7.1,<8.0',
388 'version' => '6.0.0-rc2',
389 'mysql_versions' => '>=5.5',
411 'date' => '2019-12-26',
412 'php_versions' => '>=5.5,<7.0',
413 'version' => '6.0.0',
414 'mysql_versions' => '>=5.5',
417 'date' => '2019-12-26',
418 'php_versions' => '>=7.1,<8.0',
419 'version' => '5.0.0',
420 'mysql_versions' => '>=5.5',
442 'date' => '2019-12-26',
443 'php_versions' => '>=5.5,<7.0',
444 'version' => '6.0.0',
445 'mysql_versions' => '>=5.5',
448 'date' => '2019-12-26',
449 'php_versions' => '>=7.1,<8.0',
450 'version' => '5.0.0',
451 'mysql_versions' => '>=5.5',
473 'date' => '2019-12-26',
474 'php_versions' => '>=5.5,<8.0',
475 'version' => '4.9.3',
476 'mysql_versions' => '>=5.5',
479 'date' => '2019-12-26',
480 'php_versions' => '>=7.1,<8.0',
481 'version' => '5.0.0',
482 'mysql_versions' => '>=5.5',
508 'date' => '2019-12-26',
509 'php_versions' => '>=7.1,<8.0',
510 'version' => '5.0.0',
511 'mysql_versions' => '>=5.5',
514 'date' => '2019-12-26',
515 'php_versions' => '>=5.5,<8.0',
516 'version' => '4.9.3',
517 'mysql_versions' => '>=5.5',
544 * Tests evaluateVersionCondition() method
546 public function testEvaluateVersionCondition(): void
548 $mockVersionInfo = $this->getMockBuilder(VersionInformation
::class)
549 ->onlyMethods(['getPHPVersion'])
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'));