diff --git a/src/Version.php b/src/Version.php index eff3897..d2e1bfc 100644 --- a/src/Version.php +++ b/src/Version.php @@ -14,7 +14,7 @@ class Version /** @var string */ private $commitHash; - + /** @var bool */ private $versionIsTagged; @@ -48,7 +48,7 @@ public function getFullVersion(): string public function getVersionWithShortCommit(): string { - return $this->getShortVersion() . '@' . substr($this->getCommitHash(), 0, self::SHORT_COMMIT_LENGTH); + return $this->getShortVersion() . '@' . $this->getShortCommitHash(); } public function getPackageName(): string @@ -65,7 +65,12 @@ public function getCommitHash(): string { return $this->commitHash; } - + + public function getShortCommitHash(): string + { + return substr($this->commitHash, 0, self::SHORT_COMMIT_LENGTH); + } + public function __toString(): string { return $this->getPrettyVersion(); diff --git a/tests/Unit/PrettyVersionsTest.php b/tests/Unit/PrettyVersionsTest.php index 6839c42..3536640 100644 --- a/tests/Unit/PrettyVersionsTest.php +++ b/tests/Unit/PrettyVersionsTest.php @@ -128,4 +128,24 @@ public function commitHashProvider(): array [self::REPLACE_VERSION, 'aaabbbcccddd'], ]; } + + /** + * @dataProvider shortCommitHashProvider + */ + public function testGetShortCommitHash(string $originalVersion, string $expectedHash) + { + $version = new Version('vendor/package', $originalVersion); + + $this->assertSame($expectedHash, $version->getShortCommitHash()); + } + + public function shortCommitHashProvider(): array + { + return [ + [self::STABLE_VERSION, '51e867c'], + [self::STABLE_VERSION_WITH_V, '93d39f1'], + [self::DEV_VERSION, 'f6e77da'], + [self::REPLACE_VERSION, 'aaabbbc'], + ]; + } }