Merge branch 'QA_5_2'
[phpmyadmin.git] / tests / unit / VersionInformationTest.php
bloba78348c3cc0b7fd93c1747b3578e872f828f5991
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Config;
8 use PhpMyAdmin\Release;
9 use PhpMyAdmin\VersionInformation;
10 use PHPUnit\Framework\Attributes\CoversClass;
11 use PHPUnit\Framework\Attributes\DataProvider;
12 use PHPUnit\Framework\Attributes\Group;
13 use PHPUnit\Framework\Attributes\Large;
15 #[CoversClass(VersionInformation::class)]
16 #[CoversClass(Release::class)]
17 #[Large]
18 class VersionInformationTest extends AbstractTestCase
20 /** @var Release[] */
21 private array $releases;
23 /**
24 * Sets up the fixture, for example, opens a network connection.
25 * This method is called before a test is executed.
27 protected function setUp(): void
29 parent::setUp();
31 $this->setProxySettings();
33 $this->releases = [];
35 $release = new Release('4.4.14.1', '2015-09-08', '>=5.3,<7.1', '>=5.5');
36 $this->releases[] = $release;
38 $release = new Release('4.4.13.3', '2015-09-09', '>=5.3,<7.0', '>=5.5');
39 $this->releases[] = $release;
41 $release = new Release('4.0.10.10', '2015-05-13', '>=5.2,<5.3', '>=5.0');
42 $this->releases[] = $release;
45 /**
46 * Test version checking
48 #[Group('network')]
49 public function testGetLatestVersion(): void
51 $this->setProxySettings();
52 Config::getInstance()->settings['VersionCheck'] = true;
53 unset($_SESSION['cache']['version_check']);
54 $versionInformation = new VersionInformation();
55 $version = $versionInformation->getLatestVersions();
56 self::assertIsArray($version);
57 self::assertNotEmpty($version);
60 /**
61 * Test version to int conversion.
63 * @param string $version Version string
64 * @param int $numeric Integer matching version
66 #[DataProvider('dataVersions')]
67 public function testVersionToInt(string $version, int $numeric): void
69 $versionInformation = new VersionInformation();
70 self::assertSame(
71 $numeric,
72 $versionInformation->versionToInt($version),
76 /**
77 * Data provider for version parsing
79 * @return mixed[]
81 public static function dataVersions(): array
83 return [
84 ['1.0.0', 1000050],
85 ['2.0.0.2-dev', 2000002],
86 ['3.4.2.1', 3040251],
87 ['3.4.2-dev3', 3040203],
88 ['3.4.2-dev', 3040200],
89 ['3.4.2-pl', 3040260],
90 ['3.4.2-pl3', 3040263],
91 ['4.4.2-rc22', 4040252],
92 ['4.4.2-rc', 4040230],
93 ['4.4.22-beta22', 4042242],
94 ['4.4.22-beta', 4042220],
95 ['4.4.21-alpha22', 4042132],
96 ['4.4.20-alpha', 4042010],
97 ['4.40.20-alpha-dev', 4402010],
98 ['4.4a', 4000050],
99 ['4.4.4-test', 4040400],
100 ['4.1.0', 4010050],
101 ['4.0.1.3', 4000153],
102 ['4.1-dev', 4010000],
107 * Tests getLatestCompatibleVersion() when there is only one server configured
109 public function testGetLatestCompatibleVersionWithSingleServer(): void
111 Config::getInstance()->settings['Servers'] = [[]];
113 $mockVersionInfo = $this->createPartialMock(VersionInformation::class, ['getPHPVersion', 'getMySQLVersion']);
114 $mockVersionInfo->expects(self::exactly(6))->method('getPHPVersion')->willReturn('5.6.0');
115 $mockVersionInfo->expects(self::exactly(2))->method('getMySQLVersion')->willReturn('5.5.0');
117 $compatible = $mockVersionInfo->getLatestCompatibleVersion($this->releases);
118 self::assertInstanceOf(Release::class, $compatible);
119 self::assertSame('4.4.14.1', $compatible->version);
123 * Tests getLatestCompatibleVersion() when there are multiple servers configured
125 public function testGetLatestCompatibleVersionWithMultipleServers(): void
127 Config::getInstance()->settings['Servers'] = [[], []];
129 $mockVersionInfo = $this->createPartialMock(VersionInformation::class, ['getPHPVersion', 'getMySQLVersion']);
130 $mockVersionInfo->expects(self::exactly(6))->method('getPHPVersion')->willReturn('5.6.0');
131 $mockVersionInfo->expects(self::never())->method('getMySQLVersion');
133 $compatible = $mockVersionInfo->getLatestCompatibleVersion($this->releases);
134 self::assertInstanceOf(Release::class, $compatible);
135 self::assertSame('4.4.14.1', $compatible->version);
139 * Tests getLatestCompatibleVersion() with an old PHP version
141 public function testGetLatestCompatibleVersionWithOldPHPVersion(): void
143 Config::getInstance()->settings['Servers'] = [[], []];
145 $mockVersionInfo = $this->createPartialMock(VersionInformation::class, ['getPHPVersion', 'getMySQLVersion']);
146 $mockVersionInfo->expects(self::exactly(4))->method('getPHPVersion')->willReturn('5.2.1');
147 $mockVersionInfo->expects(self::never())->method('getMySQLVersion');
149 $compatible = $mockVersionInfo->getLatestCompatibleVersion($this->releases);
150 self::assertInstanceOf(Release::class, $compatible);
151 self::assertSame('4.0.10.10', $compatible->version);
155 * Tests getLatestCompatibleVersion() with an new PHP version
157 * @param list<Release> $versions The versions to use
158 * @param array{int, string} $conditions The conditions that will be executed
159 * @param string|null $matchedLastVersion The version that will be matched
161 #[DataProvider('dataProviderVersionConditions')]
162 public function testGetLatestCompatibleVersionWithNewPHPVersion(
163 array $versions,
164 array $conditions,
165 string|null $matchedLastVersion,
166 ): void {
167 Config::getInstance()->settings['Servers'] = [];
169 $mockVersionInfo = $this->createPartialMock(VersionInformation::class, ['getPHPVersion', 'getMySQLVersion']);
170 $mockVersionInfo->expects(self::exactly($conditions[0]))->method('getPHPVersion')->willReturn($conditions[1]);
171 $mockVersionInfo->expects(self::never())->method('getMySQLVersion');
173 $compatible = $mockVersionInfo->getLatestCompatibleVersion($versions);
174 self::assertSame($matchedLastVersion, $compatible->version ?? null);
178 * Provider for testGetLatestCompatibleVersionWithNewPHPVersion
179 * Returns the conditions to be used for mocks
181 * @return list<array{list<Release>, array{int, string}, string|null}>
183 public static function dataProviderVersionConditions(): array
185 return [
188 new Release('4.9.3', '2019-12-26', '>=5.5,<8.0', '>=5.5'),
189 new Release('5.0.0', '2019-12-26', '>=7.1,<8.0', '>=5.5'),
191 [3, '7.0.0'],
192 '4.9.3',
196 new Release('6.0.0', '2019-12-26', '>=5.5,<7.0', '>=5.5'),
197 new Release('5.0.0', '2019-12-26', '>=7.1,<8.0', '>=5.5'),
199 [3, '5.6.0'],
200 '6.0.0',
204 new Release('6.0.0-rc1', '2019-12-26', '>=5.5,<7.0', '>=5.5'),
205 new Release('6.0.0-rc2', '2019-12-26', '>=7.1,<8.0', '>=5.5'),
207 [3, '5.6.0'],
208 '6.0.0-rc1',
212 new Release('6.0.0', '2019-12-26', '>=5.5,<7.0', '>=5.5'),
213 new Release('5.0.0', '2019-12-26', '>=7.1,<8.0', '>=5.5'),
215 [3, '7.0.0'],
216 null,
220 new Release('6.0.0', '2019-12-26', '>=5.5,<7.0', '>=5.5'),
221 new Release('5.0.0', '2019-12-26', '>=7.1,<8.0', '>=5.5'),
223 [4, '7.1.0'],
224 '5.0.0',
228 new Release('4.9.3', '2019-12-26', '>=5.5,<8.0', '>=5.5'),
229 new Release('5.0.0', '2019-12-26', '>=7.1,<8.0', '>=5.5'),
231 [4, '7.1.0'],
232 '5.0.0',
236 new Release('5.0.0', '2019-12-26', '>=7.1,<8.0', '>=5.5'),
237 new Release('4.9.3', '2019-12-26', '>=5.5,<8.0', '>=5.5'),
239 [4, '7.2.0'],
240 '5.0.0',
246 * Tests evaluateVersionCondition() method
248 public function testEvaluateVersionCondition(): void
250 $mockVersionInfo = $this->getMockBuilder(VersionInformation::class)
251 ->onlyMethods(['getPHPVersion'])
252 ->getMock();
254 $mockVersionInfo->expects(self::any())
255 ->method('getPHPVersion')
256 ->willReturn('5.2.4');
258 self::assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '<=5.3'));
259 self::assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '<5.3'));
260 self::assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '>=5.2'));
261 self::assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '>5.2'));
262 self::assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '!=5.3'));
264 self::assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '<=5.2'));
265 self::assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '<5.2'));
266 self::assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '>=7.0'));
267 self::assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '>7.0'));
268 self::assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '!=5.2'));