Skip to content

Commit e1e1fd0

Browse files
Refactor
1 parent f018938 commit e1e1fd0

File tree

5 files changed

+159
-10
lines changed

5 files changed

+159
-10
lines changed

src/TextUI/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
use PHPUnit\TextUI\XmlConfiguration\Configuration as XmlConfiguration;
7575
use PHPUnit\TextUI\XmlConfiguration\DefaultConfiguration;
7676
use PHPUnit\TextUI\XmlConfiguration\Loader;
77+
use PHPUnit\Util\Http\PhpDownloader;
7778
use SebastianBergmann\Timer\Timer;
7879
use Throwable;
7980

@@ -453,7 +454,7 @@ private function executeCommandsThatOnlyRequireCliConfiguration(CliConfiguration
453454
}
454455

455456
if ($cliConfiguration->checkVersion()) {
456-
$this->execute(new VersionCheckCommand);
457+
$this->execute(new VersionCheckCommand(new PhpDownloader, Version::majorVersionNumber(), Version::id()));
457458
}
458459

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

src/TextUI/Command/Commands/VersionCheckCommand.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,35 @@
1010
namespace PHPUnit\TextUI\Command;
1111

1212
use const PHP_EOL;
13-
use function file_get_contents;
1413
use function sprintf;
1514
use function version_compare;
16-
use PHPUnit\Runner\Version;
15+
use PHPUnit\Util\Http\Downloader;
1716

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

32-
$notLatest = version_compare($latestVersion, Version::id(), '>');
33-
$notLatestCompatible = version_compare($latestCompatibleVersion, Version::id(), '>');
40+
$notLatest = version_compare($latestVersion, $this->versionId, '>');
41+
$notLatestCompatible = version_compare($latestCompatibleVersion, $this->versionId, '>');
3442

3543
if (!$notLatest && !$notLatestCompatible) {
3644
return Result::from(
@@ -43,7 +51,7 @@ public function execute(): Result
4351
if ($notLatestCompatible) {
4452
$buffer .= sprintf(
4553
'The latest version compatible with PHPUnit %s is PHPUnit %s.' . PHP_EOL,
46-
Version::id(),
54+
$this->versionId,
4755
$latestCompatibleVersion,
4856
);
4957
}

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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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: positive-int, 2: non-empty-string, 3: non-empty-string, 4: non-empty-string}>
26+
*/
27+
public static function provider(): array
28+
{
29+
return [
30+
[
31+
'You are using the latest version of PHPUnit.',
32+
10,
33+
'10.5.0',
34+
'10.5.0',
35+
'10.5.0',
36+
],
37+
[
38+
'You are not using the latest version of PHPUnit.
39+
The latest version compatible with PHPUnit 10.5.0 is PHPUnit 10.5.1.
40+
The latest version is PHPUnit 10.5.1.',
41+
10,
42+
'10.5.0',
43+
'10.5.1',
44+
'10.5.1',
45+
],
46+
[
47+
'You are not using the latest version of PHPUnit.
48+
The latest version compatible with PHPUnit 10.5.0 is PHPUnit 10.5.1.
49+
The latest version is PHPUnit 11.0.0.',
50+
10,
51+
'10.5.0',
52+
'11.0.0',
53+
'10.5.1',
54+
],
55+
];
56+
}
57+
58+
/**
59+
* @param non-empty-string $expected
60+
* @param positive-int $majorVersionNumber
61+
* @param non-empty-string $versionId
62+
* @param non-empty-string $latestVersion
63+
* @param non-empty-string $latestCompatibleVersion
64+
*/
65+
#[DataProvider('provider')]
66+
public function testChecksVersion(string $expected, int $majorVersionNumber, string $versionId, string $latestVersion, string $latestCompatibleVersion): void
67+
{
68+
$command = new VersionCheckCommand(
69+
$this->downloader($latestVersion, $latestCompatibleVersion),
70+
$majorVersionNumber,
71+
$versionId,
72+
);
73+
74+
$result = $command->execute();
75+
76+
$this->assertSame($expected, trim($result->output()));
77+
}
78+
79+
private function downloader(string $latestVersion, string $latestCompatibleVersion): Downloader&Stub
80+
{
81+
$downloader = $this->createStub(Downloader::class);
82+
83+
$downloader
84+
->method('download')
85+
->willReturn($latestVersion, $latestCompatibleVersion);
86+
87+
return $downloader;
88+
}
89+
}

0 commit comments

Comments
 (0)