Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enhancement: Allow bumping Version #13

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,78 @@ echo $version->preRelease()->toString(); // alpha
echo $version->buildMetaData()->toString(); // build.9001
```

### Bump a `Version`

```php
<?php

declare(strict_types=1);

use Ergebnis\Version;

$version = Version\Version::fromString('1.2.3');

$one = $version->bumpMajor();

echo $one->toString(); // 2.0.0

$two = $version->bumpMinor();

echo $two->toString(); // 1.3.0

$three = $version->bumpPatch();

echo $three->toString(); // 1.2.4
```

### Bump a `Version` with `PreRelease`

```php
<?php

declare(strict_types=1);

use Ergebnis\Version;

$version = Version\Version::fromString('1.2.3-alpha');

$one = $version->bumpMajor();

echo $one->toString(); // 2.0.0

$two = $version->bumpMinor();

echo $two->toString(); // 1.3.0

$three = $version->bumpPatch();

echo $three->toString(); // 1.2.3
```

### Bump a `Version` with `BuildMetaData`

```php
<?php

declare(strict_types=1);

use Ergebnis\Version;

$version = Version\Version::fromString('1.2.3+build.9001');

$one = $version->bumpMajor();

echo $one->toString(); // 2.0.0

$two = $version->bumpMinor();

echo $two->toString(); // 1.3.0

$three = $version->bumpPatch();

echo $three->toString(); // 1.2.4
```

### Compare a `Version` with another `Version`

```php
Expand Down
3 changes: 3 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<file src="test/Unit/VersionTest.php">
<PossiblyUnusedMethod>
<code>provideInvalidValue</code>
<code>provideValidValueAndValueWithBumpedMajor</code>
<code>provideValidValueAndValueWithBumpedMinor</code>
<code>provideValidValueAndValueWithBumpedPatch</code>
<code>provideValidValueMajorMinorPatchPreReleaseAndBuildMetaData</code>
</PossiblyUnusedMethod>
</file>
Expand Down
33 changes: 33 additions & 0 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ public function toString(): string
return $value;
}

public function bumpMajor(): self
{
return new self(
$this->major->bump(),
Minor::fromInt(0),
Patch::fromInt(0),
PreRelease::empty(),
BuildMetaData::empty(),
);
}

public function bumpMinor(): self
{
return new self(
$this->major,
$this->minor->bump(),
Patch::fromInt(0),
PreRelease::empty(),
BuildMetaData::empty(),
);
}

public function bumpPatch(): self
{
return new self(
$this->major,
$this->minor,
$this->patch->bump(),
PreRelease::empty(),
BuildMetaData::empty(),
);
}

public function equals(self $other): bool
{
return $this->toString() === $other->toString();
Expand Down
114 changes: 114 additions & 0 deletions test/Unit/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,120 @@ public static function provideValidValueMajorMinorPatchPreReleaseAndBuildMetaDat
}
}

#[Framework\Attributes\DataProvider('provideValidValueAndValueWithBumpedMajor')]
public function testBumpMajorReturnsVersionWithBumpedMajor(
string $value,
string $valueWithBumpedMajor,
): void {
$one = Version::fromString($value);

$two = $one->bumpMajor();

self::assertNotSame($one, $two);
self::assertSame($valueWithBumpedMajor, $two->toString());
}

/**
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
* @see https://regex101.com/r/Ly7O1x/3/
*
* @return \Generator<string, array{0: string, 1: string}>
*/
public static function provideValidValueAndValueWithBumpedMajor(): \Generator
{
$values = [
'0.0.4' => '1.0.0',
'1.2.3' => '2.0.0',
'10.20.30' => '11.0.0',
'1.1.2-prerelease+meta' => '2.0.0',
'1.1.2+meta' => '2.0.0',
'1.0.0-alpha' => '2.0.0',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure whether this makes sense, see https://semver.org/#spec-item-11.

];

foreach ($values as $value => $valueWithBumpedMajor) {
yield $value => [
$value,
$valueWithBumpedMajor,
];
}
}

#[Framework\Attributes\DataProvider('provideValidValueAndValueWithBumpedMinor')]
public function testBumpMinorReturnsVersionWithBumpedMinor(
string $value,
string $valueWithBumpedMinor,
): void {
$one = Version::fromString($value);

$two = $one->bumpMinor();

self::assertNotSame($one, $two);
self::assertSame($valueWithBumpedMinor, $two->toString());
}

/**
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
* @see https://regex101.com/r/Ly7O1x/3/
*
* @return \Generator<string, array{0: string, 1: string}>
*/
public static function provideValidValueAndValueWithBumpedMinor(): \Generator
{
$values = [
'0.0.4' => '0.1.0',
'1.2.3' => '1.3.0',
'10.20.30' => '10.21.0',
'1.1.2-prerelease+meta' => '1.2.0',
'1.1.2+meta' => '1.2.0',
'1.0.0-alpha' => '1.1.0',
];

foreach ($values as $value => $valueWithBumpedMinor) {
yield $value => [
$value,
$valueWithBumpedMinor,
];
}
}

#[Framework\Attributes\DataProvider('provideValidValueAndValueWithBumpedPatch')]
public function testBumpPatchReturnsVersionWithBumpedPatch(
string $value,
string $valueWithBumpedPatch,
): void {
$one = Version::fromString($value);

$two = $one->bumpPatch();

self::assertNotSame($one, $two);
self::assertSame($valueWithBumpedPatch, $two->toString());
}

/**
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
* @see https://regex101.com/r/Ly7O1x/3/
*
* @return \Generator<string, array{0: string, 1: string}>
*/
public static function provideValidValueAndValueWithBumpedPatch(): \Generator
{
$values = [
'0.0.4' => '0.0.5',
'1.2.3' => '1.2.4',
'10.20.30' => '10.20.31',
'1.1.2-prerelease+meta' => '1.1.3',
'1.1.2+meta' => '1.1.3',
'1.0.0-alpha' => '1.0.1',
];

foreach ($values as $value => $valueWithBumpedPatch) {
yield $value => [
$value,
$valueWithBumpedPatch,
];
}
}

public function testEqualsReturnsFalseWhenValuesAreDifferent(): void
{
$faker = self::faker()->unique();
Expand Down