Skip to content

Commit 27bbfab

Browse files
authored
Merge pull request #16 from ergebnis/feature/compare
Enhancement: Allow comparing `Major`s
2 parents 86be609 + f8a44a5 commit 27bbfab

4 files changed

Lines changed: 44 additions & 21 deletions

File tree

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,11 @@ declare(strict_types=1);
187187
use Ergebnis\Version;
188188

189189
$one = Version\Major::fromString('1');
190-
$two = Version\Major::fromString('1');
191-
$three = Version\Major::fromString('2');
190+
$two = Version\Major::fromString('2');
192191

193-
$one->equals($two); // true
194-
$one->equals($three); // false
195-
196-
$one->equals($two); // true
192+
$one->compare($two); // -1
193+
$one->compare($one); // 0
194+
$two->compare($one); // 1
197195
```
198196

199197
### Create a `Minor` from an `int`

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<code>provideInvalidStringValue</code>
2727
<code>provideValidStringValue</code>
2828
<code>provideValueAndBumpedValue</code>
29+
<code>provideValueOtherValueAndResult</code>
2930
</PossiblyUnusedMethod>
3031
</file>
3132
<file src="test/Unit/MinorTest.php">

src/Major.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public function bump(): self
7575
return new self((string) ($valueCastedToInt + 1));
7676
}
7777

78-
public function equals(self $other): bool
78+
public function compare(self $other): int
7979
{
80-
return $this->value === $other->value;
80+
return $this->value <=> $other->value;
8181
}
8282
}

test/Unit/MajorTest.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,47 @@ public static function provideValueAndBumpedValue(): \Generator
148148
}
149149
}
150150

151-
public function testEqualsReturnsFalseWhenValuesAreDifferent(): void
152-
{
153-
$faker = self::faker()->unique();
154-
155-
$one = Major::fromString((string) $faker->numberBetween(0));
156-
$two = Major::fromString((string) $faker->numberBetween(0));
151+
#[Framework\Attributes\DataProvider('provideValueOtherValueAndResult')]
152+
public function testCompareReturnsResultOfComparingValues(
153+
string $value,
154+
string $otherValue,
155+
int $result,
156+
): void {
157+
$one = Major::fromString($value);
158+
$two = Major::fromString($otherValue);
157159

158-
self::assertFalse($one->equals($two));
160+
self::assertSame($result, $one->compare($two));
159161
}
160162

161-
public function testEqualsReturnsTrueWhenValueIsSame(): void
163+
/**
164+
* @return \Generator<string, array{0: string, 1: string, 2: int}>
165+
*/
166+
public static function provideValueOtherValueAndResult(): \Generator
162167
{
163-
$value = (string) self::faker()->numberBetween(0);
164-
165-
$one = Major::fromString($value);
166-
$two = Major::fromString($value);
168+
$values = [
169+
'less' => [
170+
'0',
171+
'1',
172+
-1,
173+
],
174+
'same' => [
175+
'1',
176+
'1',
177+
0,
178+
],
179+
'greater' => [
180+
'2',
181+
'1',
182+
1,
183+
],
184+
];
167185

168-
self::assertTrue($one->equals($two));
186+
foreach ($values as $key => [$value, $otherValue, $result]) {
187+
yield $key => [
188+
$value,
189+
$otherValue,
190+
$result,
191+
];
192+
}
169193
}
170194
}

0 commit comments

Comments
 (0)