From 759744194df9ed59a60cf5be5f53b2d4fed7735d Mon Sep 17 00:00:00 2001 From: EriBloo <19932449+EriBloo@users.noreply.github.com> Date: Sun, 20 Aug 2023 15:54:13 +0200 Subject: [PATCH] refactor: semantic versionist to use only 2 parts --- README.md | 2 +- .../Versionists/SemanticVersionist.php | 33 +++++-------------- tests/SnapshotOptionsTest.php | 19 +++-------- tests/VersionistTest.php | 18 ++++------ 4 files changed, 20 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 5bba4cf..0ecf128 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/Support/Versionists/SemanticVersionist.php b/src/Support/Versionists/SemanticVersionist.php index cc2af5f..fe28443 100644 --- a/src/Support/Versionists/SemanticVersionist.php +++ b/src/Support/Versionists/SemanticVersionist.php @@ -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; } diff --git a/tests/SnapshotOptionsTest.php b/tests/SnapshotOptionsTest.php index 3dda0bb..949290c 100644 --- a/tests/SnapshotOptionsTest.php +++ b/tests/SnapshotOptionsTest.php @@ -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 () { @@ -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)); @@ -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'); }); diff --git a/tests/VersionistTest.php b/tests/VersionistTest.php index b05436e..497ac62 100644 --- a/tests/VersionistTest.php +++ b/tests/VersionistTest.php @@ -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'); });