Skip to content

Commit

Permalink
refactor: semantic versionist to use only 2 parts
Browse files Browse the repository at this point in the history
  • Loading branch information
EriBloo committed Aug 20, 2023
1 parent afc4d2d commit 7597441
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Most can be later overridden during snapshotting using those methods:

Versionist is a class responsible for determining next snapshot version.
By default `IncrementingVersionist` is used, which simply increments versions.
There is also simple `SemanticVersionist` available if you want to keep versions in `major.minor.patch` format.
There is also simple `SemanticVersionist` available if you want to keep versions in `major.minor` format.

If you would like to create your own versionist class it must implement
`EriBloo\LaravelModelSnapshots\Contracts\Versionist`. There are two methods you must create:
Expand Down
33 changes: 8 additions & 25 deletions src/Support/Versionists/SemanticVersionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,32 @@

class SemanticVersionist implements VersionistContract
{
/**
* @var int<0, 2>
*/
protected int $level = 1;
protected bool $major = false;

public function getFirstVersion(): string
{
return match ($this->level) {
0 => '1.0.0',
1 => '0.1.0',
2 => '0.0.1'
};
return $this->major ? '1.0' : '0.1';
}

public function getNextVersion(string $version): string
{
$versionParts = explode('.', $version);
$versionParts[$this->level] = (string) ((int) $versionParts[$this->level] + 1);
[$major, $minor] = explode('.', $version);

for ($i = $this->level + 1; $i < 3; $i++) {
$versionParts[$i] = '0';
}

return implode('.', $versionParts);
return $this->major
? ((int) $major + 1) . '.' . '0'
: $major . '.' . ((int) $minor + 1);
}

public function incrementMajor(): static
{
$this->level = 0;
$this->major = true;

return $this;
}

public function incrementMinor(): static
{
$this->level = 1;

return $this;
}

public function incrementPatch(): static
{
$this->level = 2;
$this->major = false;

return $this;
}
Expand Down
19 changes: 5 additions & 14 deletions tests/SnapshotOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
it('uses custom versionist', function () {
snapshot($this->model)->commit();

expect($this->model->getLatestSnapshot()->getAttribute('version'))->toBe('0.1.0');
expect($this->model->getLatestSnapshot()->getAttribute('version'))->toBe('0.1');
});

it('excludes attributes', function () {
Expand Down Expand Up @@ -53,14 +53,14 @@
it('properly versions with versionist set at runtime', function () {
snapshot($this->model)->commit();
expect($this->model->getLatestSnapshot())
->getAttribute('version')->toBe('0.1.0');
->getAttribute('version')->toBe('0.1');

Carbon::setTestNow($this->now->addSeconds(1));
$this->model->update(['name' => Str::random()]);

snapshot($this->model)->commit();
expect($this->model->getLatestSnapshot())
->getAttribute('version')->toBe('0.2.0');
->getAttribute('version')->toBe('0.2');
$this->model->update(['name' => Str::random()]);

Carbon::setTestNow($this->now->addSeconds(2));
Expand All @@ -70,23 +70,14 @@
->version(fn (SemanticVersionist $versionist) => $versionist->incrementMajor())
->commit();
expect($this->model->getLatestSnapshot())
->getAttribute('version')->toBe('1.0.0');
->getAttribute('version')->toBe('1.0');

Carbon::setTestNow($this->now->addSeconds(3));
$this->model->update(['name' => Str::random()]);

snapshot($this->model)
->version(fn (SemanticVersionist $versionist) => $versionist->incrementPatch())
->commit();
expect($this->model->getLatestSnapshot())
->getAttribute('version')->toBe('1.0.1');

Carbon::setTestNow($this->now->addSeconds(4));
$this->model->update(['name' => Str::random()]);

snapshot($this->model)
->version(fn (SemanticVersionist $versionist) => $versionist->incrementMinor())
->commit();
expect($this->model->getLatestSnapshot())
->getAttribute('version')->toBe('1.1.0');
->getAttribute('version')->toBe('1.1');
});
18 changes: 6 additions & 12 deletions tests/VersionistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@
});

it('returns proper semantic versionist first version', function () {
expect((new SemanticVersionist())->getFirstVersion())->toBe('0.1.0')
->and((new SemanticVersionist())->incrementMajor()->getFirstVersion())->toBe('1.0.0')
->and((new SemanticVersionist())->incrementPatch()->getFirstVersion())->toBe('0.0.1');
expect((new SemanticVersionist())->getFirstVersion())->toBe('0.1')
->and((new SemanticVersionist())->incrementMajor()->getFirstVersion())->toBe('1.0');
});

it('returns proper semantic versionist next version', function () {
expect((new SemanticVersionist())->getNextVersion('0.1.0'))->toBe('0.2.0')
->and((new SemanticVersionist())->incrementMajor()->getNextVersion('0.1.0'))->toBe('1.0.0')
->and((new SemanticVersionist())->incrementPatch()->getNextVersion('0.1.0'))->toBe('0.1.1')
->and((new SemanticVersionist())->getNextVersion('1.0.0'))->toBe('1.1.0')
->and((new SemanticVersionist())->incrementMajor()->getNextVersion('1.0.0'))->toBe('2.0.0')
->and((new SemanticVersionist())->incrementPatch()->getNextVersion('1.0.0'))->toBe('1.0.1')
->and((new SemanticVersionist())->getNextVersion('0.0.1'))->toBe('0.1.0')
->and((new SemanticVersionist())->incrementMajor()->getNextVersion('0.0.1'))->toBe('1.0.0')
->and((new SemanticVersionist())->incrementPatch()->getNextVersion('0.0.1'))->toBe('0.0.2');
expect((new SemanticVersionist())->getNextVersion('0.1'))->toBe('0.2')
->and((new SemanticVersionist())->incrementMajor()->getNextVersion('0.1'))->toBe('1.0')
->and((new SemanticVersionist())->getNextVersion('1.0'))->toBe('1.1')
->and((new SemanticVersionist())->incrementMajor()->getNextVersion('1.0'))->toBe('2.0');
});

0 comments on commit 7597441

Please sign in to comment.