Skip to content

Commit 2933498

Browse files
committed
Improve VersionCheckControllerTest tests
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent acba6ed commit 2933498

File tree

1 file changed

+61
-20
lines changed

1 file changed

+61
-20
lines changed

test/classes/Controllers/VersionCheckControllerTest.php

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,93 @@
1010
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
1111
use PhpMyAdmin\VersionInformation;
1212

13-
use function json_encode;
14-
use function time;
15-
16-
use const PHP_VERSION_ID;
17-
1813
/**
1914
* @covers \PhpMyAdmin\Controllers\VersionCheckController
2015
*/
2116
class VersionCheckControllerTest extends AbstractTestCase
2217
{
23-
public function testInvoke(): void
18+
public function testWithLatestCompatibleVersion(): void
2419
{
2520
$_GET = [];
26-
$GLOBALS['cfg']['VersionCheck'] = true;
27-
$versionInfo = [
21+
$versionInfo = (object) [
2822
'date' => '2022-02-11',
2923
'version' => '5.1.3',
3024
'releases' => [
31-
[
25+
(object) [
3226
'date' => '2022-02-11',
3327
'php_versions' => '>=7.1,<8.1',
3428
'version' => '5.1.3',
3529
'mysql_versions' => '>=5.5',
3630
],
37-
[
31+
(object) [
3832
'date' => '2022-02-11',
3933
'php_versions' => '>=5.5,<8.0',
4034
'version' => '4.9.10',
4135
'mysql_versions' => '>=5.5',
4236
],
4337
],
4438
];
45-
$_SESSION['cache'] = [];
46-
$_SESSION['cache']['version_check'] = [
47-
'response' => json_encode($versionInfo),
48-
'timestamp' => time(),
39+
40+
$versionInformation = $this->createMock(VersionInformation::class);
41+
$versionInformation->expects($this->once())->method('getLatestVersion')->willReturn($versionInfo);
42+
$versionInformation->expects($this->once())->method('getLatestCompatibleVersion')
43+
->with($this->equalTo($versionInfo->releases))
44+
->willReturn(['version' => '5.1.3', 'date' => '2022-02-11']);
45+
46+
(new VersionCheckController(new ResponseRenderer(), new Template(), $versionInformation))();
47+
48+
$output = $this->getActualOutputForAssertion();
49+
$this->assertTrue(isset($_GET['ajax_request']));
50+
$this->assertSame('{"version":"5.1.3","date":"2022-02-11"}', $output);
51+
}
52+
53+
public function testWithoutLatestCompatibleVersion(): void
54+
{
55+
$_GET = [];
56+
$versionInfo = (object) [
57+
'date' => '2022-02-11',
58+
'version' => '5.1.3',
59+
'releases' => [
60+
(object) [
61+
'date' => '2022-02-11',
62+
'php_versions' => '>=7.1,<8.1',
63+
'version' => '5.1.3',
64+
'mysql_versions' => '>=5.5',
65+
],
66+
(object) [
67+
'date' => '2022-02-11',
68+
'php_versions' => '>=5.5,<8.0',
69+
'version' => '4.9.10',
70+
'mysql_versions' => '>=5.5',
71+
],
72+
],
4973
];
5074

51-
(new VersionCheckController(new ResponseRenderer(), new Template(), new VersionInformation()))();
75+
$versionInformation = $this->createMock(VersionInformation::class);
76+
$versionInformation->expects($this->once())->method('getLatestVersion')->willReturn($versionInfo);
77+
$versionInformation->expects($this->once())->method('getLatestCompatibleVersion')
78+
->with($this->equalTo($versionInfo->releases))
79+
->willReturn(null);
80+
81+
(new VersionCheckController(new ResponseRenderer(), new Template(), $versionInformation))();
82+
83+
$output = $this->getActualOutputForAssertion();
84+
$this->assertTrue(isset($_GET['ajax_request']));
85+
$this->assertSame('{"version":"","date":""}', $output);
86+
}
87+
88+
public function testWithoutLatestVersion(): void
89+
{
90+
$_GET = [];
91+
92+
$versionInformation = $this->createMock(VersionInformation::class);
93+
$versionInformation->expects($this->once())->method('getLatestVersion')->willReturn(null);
94+
$versionInformation->expects($this->never())->method('getLatestCompatibleVersion');
95+
96+
(new VersionCheckController(new ResponseRenderer(), new Template(), $versionInformation))();
5297

5398
$output = $this->getActualOutputForAssertion();
5499
$this->assertTrue(isset($_GET['ajax_request']));
55-
if (PHP_VERSION_ID < 80100) {
56-
$this->assertSame('{"version":"5.1.3","date":"2022-02-11"}', $output);
57-
} else {
58-
$this->assertSame('{"version":"","date":""}', $output);
59-
}
100+
$this->assertSame('[]', $output);
60101
}
61102
}

0 commit comments

Comments
 (0)