Skip to content

Commit e9b5e4d

Browse files
committed
Extract MatrixInterface::lowestAndHighest into static class Versions
1 parent e8b84da commit e9b5e4d

File tree

8 files changed

+130
-86
lines changed

8 files changed

+130
-86
lines changed

src/Console/Command.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Console\Input\InputInterface;
1313
use Symfony\Component\Console\Input\InputOption;
1414
use Symfony\Component\Console\Output\OutputInterface;
15+
use TypistTech\PhpMatrix\Versions;
1516

1617
#[AsCommand(
1718
name: 'php-matrix',
@@ -118,14 +119,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
118119
);
119120
}
120121

121-
[$lowest, $highest] = $matrix->lowestAndHighest(...$versions);
122-
123122
$result = json_encode(
124123
(object) [
125124
self::CONSTRAINT_ARGUMENT_NAME => $constraint,
126-
'versions' => $versions,
127-
'lowest' => $lowest,
128-
'highest' => $highest,
125+
'versions' => Versions::sort(...$versions),
126+
'lowest' => Versions::lowest(...$versions),
127+
'highest' => Versions::highest(...$versions),
129128
],
130129
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT
131130
);

src/Matrix.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,4 @@ public function satisfiedBy(string $constraint): array
2222
$constraint
2323
);
2424
}
25-
26-
public function lowestAndHighest(string $version, string ...$versions): array
27-
{
28-
if (empty($versions)) {
29-
return [$version, $version];
30-
}
31-
32-
$sorted = Semver::sort([$version, ...$versions]);
33-
$count = count($sorted);
34-
35-
return [$sorted[0], $sorted[$count - 1]];
36-
}
3725
}

src/MatrixInterface.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@ interface MatrixInterface
1010
* @return string[]
1111
*/
1212
public function satisfiedBy(string $constraint): array;
13-
14-
/**
15-
* @return string[]
16-
*/
17-
public function lowestAndHighest(string $version, string ...$versions): array;
1813
}

src/Versions.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypistTech\PhpMatrix;
6+
7+
use Composer\Semver\Semver;
8+
use UnexpectedValueException;
9+
10+
readonly class Versions
11+
{
12+
public static function sort(string ...$versions): array
13+
{
14+
if (empty($versions)) {
15+
throw new UnexpectedValueException('Argument #1 ($versions) must not be empty');
16+
}
17+
18+
return Semver::sort($versions);
19+
}
20+
21+
public static function lowest(string ...$versions): string
22+
{
23+
$sorted = self::sort(...$versions);
24+
25+
return $sorted[0];
26+
}
27+
28+
public static function highest(string ...$versions): string
29+
{
30+
$sorted = self::sort(...$versions);
31+
32+
return $sorted[array_key_last($sorted)];
33+
}
34+
}

tests/Feature/TestCase.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public function mockMatrix(): array
5353
->satisfiedBy($constraint)
5454
->andReturn($expectedObject->versions);
5555

56-
$matrix->expects()
57-
->lowestAndHighest(...$expectedObject->versions)
58-
->andReturn([$expectedObject->lowest, $expectedObject->highest]);
59-
6056
return [
6157
'matrix' => $matrix,
6258
'constraint' => $constraint,

tests/Unit/MatrixTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,4 @@
6969
expect($actual)->toBe($expected);
7070
})->with('satisfied_by');
7171
});
72-
73-
describe('::lowestAndHighest()', static function (): void {
74-
dataset('lowest_and_highest', [
75-
[['1.2.3'], '1.2.3', '1.2.3'],
76-
[['1.2.3', '1.2.3'], '1.2.3', '1.2.3'],
77-
[['1.2.3', '2.2.3'], '1.2.3', '2.2.3'],
78-
[['1.2.3', '2.2.3', '3.2.3'], '1.2.3', '3.2.3'],
79-
80-
[['1.2'], '1.2', '1.2'],
81-
[['1.2', '1.2'], '1.2', '1.2'],
82-
[['1.2', '2.2'], '1.2', '2.2'],
83-
[['1.2', '2.2', '3.2'], '1.2', '3.2'],
84-
]);
85-
86-
it(
87-
'returns lowest and highest versions',
88-
function (array $versions, string $expectedLowest, string $expectedHighest): void {
89-
$releases = Mockery::mock(ReleasesInterface::class);
90-
91-
$matrix = new Matrix($releases);
92-
93-
shuffle($versions);
94-
95-
[$actualLowest, $actualHighest] = $matrix->lowestAndHighest(...$versions);
96-
97-
expect($actualLowest)->toBe($expectedLowest);
98-
expect($actualHighest)->toBe($expectedHighest);
99-
}
100-
)->with('lowest_and_highest');
101-
});
10272
});

tests/Unit/MinorOnlyMatrixTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,4 @@
6464
expect($actual)->toBe($expected);
6565
})->with('satisfied_by');
6666
});
67-
68-
describe('::lowestAndHighest()', static function (): void {
69-
dataset('lowest_and_highest', [
70-
[['1.2.3'], '1.2.3', '1.2.3'],
71-
[['1.2.3', '1.2.3'], '1.2.3', '1.2.3'],
72-
[['1.2.3', '2.2.3'], '1.2.3', '2.2.3'],
73-
[['1.2.3', '2.2.3', '3.2.3'], '1.2.3', '3.2.3'],
74-
75-
[['1.2'], '1.2', '1.2'],
76-
[['1.2', '1.2'], '1.2', '1.2'],
77-
[['1.2', '2.2'], '1.2', '2.2'],
78-
[['1.2', '2.2', '3.2'], '1.2', '3.2'],
79-
]);
80-
81-
it(
82-
'returns lowest and highest versions',
83-
function (array $versions, string $expectedLowest, string $expectedHighest): void {
84-
$releases = Mockery::mock(ReleasesInterface::class);
85-
86-
$matrix = new MinorOnlyMatrix($releases);
87-
88-
shuffle($versions);
89-
90-
[$actualLowest, $actualHighest] = $matrix->lowestAndHighest(...$versions);
91-
92-
expect($actualLowest)->toBe($expectedLowest);
93-
expect($actualHighest)->toBe($expectedHighest);
94-
}
95-
)->with('lowest_and_highest');
96-
});
9767
});

tests/Unit/VersionsTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature\Releases;
6+
7+
use TypistTech\PhpMatrix\Versions;
8+
use UnexpectedValueException;
9+
10+
covers(Versions::class);
11+
12+
describe(Versions::class, static function (): void {
13+
describe('::sort()', static function (): void {
14+
dataset('sort', [
15+
[['1.2.3']],
16+
[['1.2.3', '1.2.3']],
17+
[['1.2.3', '2.2.3']],
18+
[['1.2.3', '2.2.3', '3.2.3']],
19+
20+
[['1.2']],
21+
[['1.2', '1.2']],
22+
[['1.2', '2.2']],
23+
[['1.2', '2.2', '3.2']],
24+
]);
25+
26+
it('sorts the versions', function (array $versions): void {
27+
$expected = $versions;
28+
29+
shuffle($versions);
30+
31+
$actual = Versions::sort(...$versions);
32+
33+
expect($actual)->toBe($expected);
34+
})->with('sort');
35+
36+
it('throws exception when argument is empty', function (): void {
37+
Versions::sort();
38+
})->throws(UnexpectedValueException::class);
39+
});
40+
41+
describe('::lowest()', static function (): void {
42+
dataset('lowest', [
43+
[['1.2.3'], '1.2.3'],
44+
[['1.2.3', '1.2.3'], '1.2.3'],
45+
[['1.2.3', '2.2.3'], '1.2.3'],
46+
[['1.2.3', '2.2.3', '3.2.3'], '1.2.3'],
47+
48+
[['1.2'], '1.2'],
49+
[['1.2', '1.2'], '1.2'],
50+
[['1.2', '2.2'], '1.2'],
51+
[['1.2', '2.2', '3.2'], '1.2'],
52+
]);
53+
54+
it('returns the lowest version', function (array $versions, string $expected): void {
55+
shuffle($versions);
56+
57+
$actual = Versions::lowest(...$versions);
58+
59+
expect($actual)->toBe($expected);
60+
})->with('lowest');
61+
62+
it('throws exception when argument is empty', function (): void {
63+
Versions::lowest();
64+
})->throws(UnexpectedValueException::class);
65+
});
66+
67+
describe('::highest()', static function (): void {
68+
dataset('highest', [
69+
[['1.2.3'], '1.2.3'],
70+
[['1.2.3', '1.2.3'], '1.2.3'],
71+
[['1.2.3', '2.2.3'], '2.2.3'],
72+
[['1.2.3', '2.2.3', '3.2.3'], '3.2.3'],
73+
74+
[['1.2'], '1.2'],
75+
[['1.2', '1.2'], '1.2'],
76+
[['1.2', '2.2'], '2.2'],
77+
[['1.2', '2.2', '3.2'], '3.2'],
78+
]);
79+
80+
it('return the highest version', function (array $versions, string $expected): void {
81+
shuffle($versions);
82+
83+
$actual = Versions::highest(...$versions);
84+
85+
expect($actual)->toBe($expected);
86+
})->with('highest');
87+
88+
it('throws exception when argument is empty', function (): void {
89+
Versions::highest();
90+
})->throws(UnexpectedValueException::class);
91+
});
92+
});

0 commit comments

Comments
 (0)