Skip to content

Commit 4b56fdf

Browse files
authored
Only stable version in exception (#4)
1 parent 05ce39a commit 4b56fdf

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/VersionChecker.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function greaterThan(?string $version = null): void
5353
$installedVersion = $this->installedVersion->getInstalledVersion();
5454

5555
if (!$this->comparator->greaterThan($version, $installedVersion)) {
56-
throw new UnsupportedVersionException(\sprintf(
56+
throw new UnsupportedVersionException($this->getFormattedMessage(
5757
'Installed RoadRunner version `%s` not supported. Requires version `%s` or higher.',
5858
$installedVersion,
5959
$version
@@ -72,7 +72,7 @@ public function lessThan(string $version): void
7272
$installedVersion = $this->installedVersion->getInstalledVersion();
7373

7474
if (!$this->comparator->lessThan($version, $installedVersion)) {
75-
throw new UnsupportedVersionException(\sprintf(
75+
throw new UnsupportedVersionException($this->getFormattedMessage(
7676
'Installed RoadRunner version `%s` not supported. Requires version `%s` or lower.',
7777
$installedVersion,
7878
$version
@@ -91,11 +91,32 @@ public function equal(string $version): void
9191
$installedVersion = $this->installedVersion->getInstalledVersion();
9292

9393
if (!$this->comparator->equal($version, $installedVersion)) {
94-
throw new UnsupportedVersionException(\sprintf(
94+
throw new UnsupportedVersionException($this->getFormattedMessage(
9595
'Installed RoadRunner version `%s` not supported. Requires version `%s`.',
9696
$installedVersion,
9797
$version
9898
), $installedVersion, $version);
9999
}
100100
}
101+
102+
/**
103+
* @param non-empty-string $message
104+
* @param non-empty-string $installedVersion
105+
* @param non-empty-string $version
106+
*
107+
* @return non-empty-string
108+
*/
109+
private function getFormattedMessage(string $message, string $installedVersion, string $version): string
110+
{
111+
\preg_match('/\bv?(\d+)\.(\d+)\.(\d+)\b/', $version, $matches);
112+
113+
if (!empty($matches[0])) {
114+
$version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
115+
}
116+
117+
/** @var non-empty-string $msg */
118+
$msg = \sprintf($message, $installedVersion, $version);
119+
120+
return $msg;
121+
}
101122
}

tests/src/Unit/VersionCheckerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,34 @@ public function testFailEqual(): void
256256
);
257257
}
258258

259+
/**
260+
* @dataProvider getFormattedMessageDataProvider
261+
*/
262+
public function testGetFormattedMessage(string $version, string $expected): void
263+
{
264+
$checker = new VersionChecker();
265+
$ref = new \ReflectionMethod($checker, 'getFormattedMessage');
266+
$ref->setAccessible(true);
267+
268+
$this->assertSame(
269+
\sprintf('installed 1 required %s', $expected),
270+
$ref->invoke($checker, 'installed %s required %s', '1', $version)
271+
);
272+
}
273+
259274
public static function invalidVersionsDataProvider(): \Traversable
260275
{
261276
yield [''];
262277
yield [null];
263278
}
279+
280+
public static function getFormattedMessageDataProvider(): \Traversable
281+
{
282+
yield ['1', '1'];
283+
yield ['1.1', '1.1'];
284+
yield ['1.2.3', '1.2.3'];
285+
yield ['1.2.3.4', '1.2.3'];
286+
yield ['v1.2.3.4', '1.2.3'];
287+
yield ['2023.1.0.0-dev', '2023.1.0'];
288+
}
264289
}

0 commit comments

Comments
 (0)