Skip to content

Commit 9e1a5d8

Browse files
Merge branch '11.4' into 11.5
2 parents 98f5580 + c3dc5e6 commit 9e1a5d8

File tree

5 files changed

+164
-10
lines changed

5 files changed

+164
-10
lines changed

src/TextUI/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
use PHPUnit\TextUI\XmlConfiguration\Configuration as XmlConfiguration;
8585
use PHPUnit\TextUI\XmlConfiguration\DefaultConfiguration;
8686
use PHPUnit\TextUI\XmlConfiguration\Loader;
87+
use PHPUnit\Util\Http\PhpDownloader;
8788
use SebastianBergmann\Timer\Timer;
8889
use Throwable;
8990

@@ -465,7 +466,7 @@ private function executeCommandsThatOnlyRequireCliConfiguration(CliConfiguration
465466
}
466467

467468
if ($cliConfiguration->checkVersion()) {
468-
$this->execute(new VersionCheckCommand);
469+
$this->execute(new VersionCheckCommand(new PhpDownloader, Version::majorVersionNumber(), Version::id()));
469470
}
470471

471472
if ($cliConfiguration->help()) {

src/TextUI/Command/Commands/VersionCheckCommand.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,41 @@
1111

1212
use const PHP_EOL;
1313
use function assert;
14-
use function file_get_contents;
1514
use function sprintf;
1615
use function version_compare;
17-
use PHPUnit\Runner\Version;
16+
use PHPUnit\Util\Http\Downloader;
1817

1918
/**
2019
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
2120
*
2221
* @internal This class is not covered by the backward compatibility promise for PHPUnit
23-
*
24-
* @codeCoverageIgnore
2522
*/
2623
final readonly class VersionCheckCommand implements Command
2724
{
25+
private Downloader $downloader;
26+
private int $majorVersionNumber;
27+
private string $versionId;
28+
29+
public function __construct(Downloader $downloader, int $majorVersionNumber, string $versionId)
30+
{
31+
$this->downloader = $downloader;
32+
$this->majorVersionNumber = $majorVersionNumber;
33+
$this->versionId = $versionId;
34+
}
35+
2836
public function execute(): Result
2937
{
30-
$latestVersion = file_get_contents('https://phar.phpunit.de/latest-version-of/phpunit');
38+
$latestVersion = $this->downloader->download('https://phar.phpunit.de/latest-version-of/phpunit');
3139

3240
assert($latestVersion !== false);
3341

34-
$latestCompatibleVersion = @file_get_contents('https://phar.phpunit.de/latest-version-of/phpunit-' . Version::majorVersionNumber());
42+
$latestCompatibleVersion = $this->downloader->download('https://phar.phpunit.de/latest-version-of/phpunit-' . $this->majorVersionNumber);
3543

36-
$notLatest = version_compare($latestVersion, Version::id(), '>');
44+
$notLatest = version_compare($latestVersion, $this->versionId, '>');
3745
$notLatestCompatible = false;
3846

3947
if ($latestCompatibleVersion !== false) {
40-
$notLatestCompatible = version_compare($latestCompatibleVersion, Version::id(), '>');
48+
$notLatestCompatible = version_compare($latestCompatibleVersion, $this->versionId, '>');
4149
}
4250

4351
if (!$notLatest && !$notLatestCompatible) {
@@ -51,7 +59,7 @@ public function execute(): Result
5159
if ($notLatestCompatible) {
5260
$buffer .= sprintf(
5361
'The latest version compatible with PHPUnit %s is PHPUnit %s.' . PHP_EOL,
54-
Version::id(),
62+
$this->versionId,
5563
$latestCompatibleVersion,
5664
);
5765
}

src/Util/Http/Downloader.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\Http;
11+
12+
/**
13+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
14+
*
15+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
16+
*/
17+
interface Downloader
18+
{
19+
/**
20+
* @param non-empty-string $url
21+
*/
22+
public function download(string $url): false|string;
23+
}

src/Util/Http/PhpDownloader.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\Http;
11+
12+
use function file_get_contents;
13+
14+
/**
15+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
16+
*
17+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
18+
*/
19+
final class PhpDownloader implements Downloader
20+
{
21+
/**
22+
* @param non-empty-string $url
23+
*/
24+
public function download(string $url): false|string
25+
{
26+
return file_get_contents($url);
27+
}
28+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TextUI\Command;
11+
12+
use function trim;
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\MockObject\Stub;
17+
use PHPUnit\Framework\TestCase;
18+
use PHPUnit\Util\Http\Downloader;
19+
20+
#[CoversClass(VersionCheckCommand::class)]
21+
#[Small]
22+
final class VersionCheckCommandTest extends TestCase
23+
{
24+
/**
25+
* @return non-empty-list<array{0: non-empty-string, 1: non-negative-int, 2: positive-int, 3: non-empty-string, 4: non-empty-string, 5: non-empty-string}>
26+
*/
27+
public static function provider(): array
28+
{
29+
return [
30+
[
31+
'You are using the latest version of PHPUnit.',
32+
Result::SUCCESS,
33+
10,
34+
'10.5.0',
35+
'10.5.0',
36+
'10.5.0',
37+
],
38+
[
39+
'You are not using the latest version of PHPUnit.
40+
The latest version compatible with PHPUnit 10.5.0 is PHPUnit 10.5.1.
41+
The latest version is PHPUnit 10.5.1.',
42+
Result::FAILURE,
43+
10,
44+
'10.5.0',
45+
'10.5.1',
46+
'10.5.1',
47+
],
48+
[
49+
'You are not using the latest version of PHPUnit.
50+
The latest version compatible with PHPUnit 10.5.0 is PHPUnit 10.5.1.
51+
The latest version is PHPUnit 11.0.0.',
52+
Result::FAILURE,
53+
10,
54+
'10.5.0',
55+
'11.0.0',
56+
'10.5.1',
57+
],
58+
];
59+
}
60+
61+
/**
62+
* @param non-empty-string $expectedMessage
63+
* @param non-negative-int $expectedShellExitCode
64+
* @param positive-int $majorVersionNumber
65+
* @param non-empty-string $versionId
66+
* @param non-empty-string $latestVersion
67+
* @param non-empty-string $latestCompatibleVersion
68+
*/
69+
#[DataProvider('provider')]
70+
public function testChecksVersion(string $expectedMessage, int $expectedShellExitCode, int $majorVersionNumber, string $versionId, string $latestVersion, string $latestCompatibleVersion): void
71+
{
72+
$command = new VersionCheckCommand(
73+
$this->downloader($latestVersion, $latestCompatibleVersion),
74+
$majorVersionNumber,
75+
$versionId,
76+
);
77+
78+
$result = $command->execute();
79+
80+
$this->assertSame($expectedMessage, trim($result->output()));
81+
$this->assertSame($expectedShellExitCode, $result->shellExitCode());
82+
}
83+
84+
private function downloader(string $latestVersion, string $latestCompatibleVersion): Downloader&Stub
85+
{
86+
$downloader = $this->createStub(Downloader::class);
87+
88+
$downloader
89+
->method('download')
90+
->willReturn($latestVersion, $latestCompatibleVersion);
91+
92+
return $downloader;
93+
}
94+
}

0 commit comments

Comments
 (0)