From 9b514beb038703e4260f97da15b84091597f7011 Mon Sep 17 00:00:00 2001 From: Julien Dufresne Date: Sat, 28 May 2016 14:56:47 +0200 Subject: [PATCH] Add methods to update version --- .php_cs | 2 - README.md | 73 ++++++++++++++++++++++++++++++++++++ Tests/VersionTest.php | 86 ++++++++++++++++++++++++++++++++++++++----- Version.php | 42 +++++++++++++++++++++ composer.lock | 8 ++-- 5 files changed, 195 insertions(+), 16 deletions(-) diff --git a/.php_cs b/.php_cs index db2574f..f685915 100644 --- a/.php_cs +++ b/.php_cs @@ -59,9 +59,7 @@ return PhpCsFixer\Config::create() 'no_blank_lines_after_class_opening' => true, // @Symfony 'no_blank_lines_after_phpdoc' => true, // @Symfony // 'no_blank_lines_before_namespace' => false, - 'no_blank_lines_between_uses' => true, // @Symfony // 'no_closing_tag' => true, // @PSR2, @Symfony - 'no_duplicate_semicolons' => true, // @Symfony 'no_empty_comment' => true, 'no_empty_phpdoc' => true, // @Symfony 'no_empty_statement' => true, // @Symfony diff --git a/README.md b/README.md index 9a16026..46af03d 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,79 @@ $version = Version::fromString('1.2.3+exp.sha.5114f85'); $version = Version::fromString('1.2.3-rc.1+exp.sha.5114f85'); ``` +### Update to next version + +It will always create a new Version, leaving the current as it is. + +**update the major version** + +```php +use SemVer\SemVer\Version; + +$version = Version::fromString('1.2.3'); +$nextVersion = $version->major(); + +var_dump($nextVersion->isEquals(new Version('2.0.0')); // true +``` + +Special case: when there is a pre-release and both minor and patch version are equals to 0, the next major version is the +actual version without the pre-release + +```php +use SemVer\SemVer\Version; + +$version = Version::fromString('1.0.0-rc.1'); +$nextVersion = $version->major(); + +var_dump($nextVersion->isEquals(new Version('1.0.0')); // true +``` + +**update the minor version** + +```php +use SemVer\SemVer\Version; + +$version = Version::fromString('1.2.3'); +$nextVersion = $version->minor(); + +var_dump($nextVersion->isEquals(new Version('1.3.0')); // true +``` + +Special case: when there is a pre-release and the patch version is equal to 0, the next minor version is the +actual version without the pre-release + +```php +use SemVer\SemVer\Version; + +$version = Version::fromString('1.1.0-rc.1'); +$nextVersion = $version->minor(); + +var_dump($nextVersion->isEquals(new Version('1.1.0')); // true +``` + + +**update the patch version** + +```php +use SemVer\SemVer\Version; + +$version = Version::fromString('1.2.3-rc.1'); +$nextVersion = $version->patch(); + +var_dump($nextVersion->isEquals(new Version('1.2.3')); // true +``` + +Special case: when there is a pre-release, the next major version is the actual version without the pre-release + +```php +use SemVer\SemVer\Version; + +$version = Version::fromString('1.0.0-rc.1'); +$nextVersion = $version->patch(); + +var_dump($nextVersion->isEquals(new Version('1.0.0')); // true +``` + ### Compare two versions ```php diff --git a/Tests/VersionTest.php b/Tests/VersionTest.php index 9b5253b..da6e5fb 100644 --- a/Tests/VersionTest.php +++ b/Tests/VersionTest.php @@ -217,9 +217,75 @@ public function testToString() } //////////////////////////////////////////////////////////////////////////// - // equals() + // major() //////////////////////////////////////////////////////////////////////////// - public function testEquals() + public function testMajor() + { + $data = [ + '1.10.10' => '2.0.0', + '1.10.10-alpha.test' => '2.0.0', + '1.10.10-alpha.test+build' => '2.0.0', + '1.10.10+build' => '2.0.0', + '1.0.0+build' => '2.0.0', + '1.0.0-alpha' => '1.0.0', + '1.0.0-alpha+build' => '1.0.0', + ]; + + foreach ($data as $currentVersion => $expectedVersion) { + $current = Version::fromString($currentVersion); + $result = $current->major(); + static::assertEquals(Version::fromString($expectedVersion), $result); + static::assertNotSame($current, $result, '::major() should create a new version.'); + } + } + + //////////////////////////////////////////////////////////////////////////// + // minor() + //////////////////////////////////////////////////////////////////////////// + public function testMinor() + { + $data = [ + '1.10.10' => '1.11.0', + '1.10.10-alpha.test' => '1.11.0', + '1.10.10-alpha.test+build' => '1.11.0', + '1.10.10+build' => '1.11.0', + '1.0.0+build' => '1.1.0', + '1.0.0-alpha' => '1.0.0', + '1.0.0-alpha+build' => '1.0.0', + ]; + + foreach ($data as $currentVersion => $expectedVersion) { + $current = Version::fromString($currentVersion); + $result = $current->minor(); + static::assertEquals(Version::fromString($expectedVersion), $result); + static::assertNotSame($current, $result, '::minor() should create a new version.'); + } + } + + //////////////////////////////////////////////////////////////////////////// + // patch() + //////////////////////////////////////////////////////////////////////////// + public function testPatch() + { + $data = [ + '1.10.10' => '1.10.11', + '1.10.10+build' => '1.10.11', + '1.10.10-alpha.test' => '1.10.10', + '1.10.10-alpha.test+build' => '1.10.10', + ]; + + foreach ($data as $currentVersion => $expectedVersion) { + $current = Version::fromString($currentVersion); + $result = $current->patch(); + static::assertEquals(Version::fromString($expectedVersion), $result); + static::assertNotSame($current, $result, '::patch() should create a new version.'); + } + } + + //////////////////////////////////////////////////////////////////////////// + // isEquals() + //////////////////////////////////////////////////////////////////////////// + public function testIsEquals() { $current = new Version(1, 0, 0); $other = new Version(1, 0, 0); @@ -230,9 +296,9 @@ public function testEquals() } //////////////////////////////////////////////////////////////////////////// - // greaterThan() + // isGreaterThan() //////////////////////////////////////////////////////////////////////////// - public function testGreaterThan() + public function testIsGreaterThan() { $current = new Version(1, 0, 0); $other = new Version(1, 1, 0); @@ -241,9 +307,9 @@ public function testGreaterThan() } //////////////////////////////////////////////////////////////////////////// - // greaterThanOrEqual() + // isGreaterThanOrEqual() //////////////////////////////////////////////////////////////////////////// - public function testGreaterThanOrEqual() + public function testIsGreaterThanOrEqual() { $current = new Version(1, 0, 0); $other = new Version(1, 1, 0); @@ -252,9 +318,9 @@ public function testGreaterThanOrEqual() } //////////////////////////////////////////////////////////////////////////// - // lessThan() + // isLessThan() //////////////////////////////////////////////////////////////////////////// - public function testLessThan() + public function testIsLessThan() { $current = new Version(1, 0, 0); $other = new Version(1, 1, 0); @@ -263,9 +329,9 @@ public function testLessThan() } //////////////////////////////////////////////////////////////////////////// - // lessThanOrEqual() + // isLessThanOrEqual() //////////////////////////////////////////////////////////////////////////// - public function testLessThanOrEqual() + public function testIsLessThanOrEqual() { $current = new Version(1, 0, 0); $other = new Version(1, 1, 0); diff --git a/Version.php b/Version.php index 4b0f462..c193277 100644 --- a/Version.php +++ b/Version.php @@ -146,6 +146,48 @@ public function getBuild() : string return $this->build; } + /** + * @throws \SemVer\SemVer\Exception\InvalidArgumentException + * + * @return Version + */ + public function major() : Version + { + if ('' !== $this->preRelease && 0 === $this->patch && 0 === $this->minor) { + return new self($this->major, 0, 0); + } + + return new self($this->major + 1, 0, 0); + } + + /** + * @throws \SemVer\SemVer\Exception\InvalidArgumentException + * + * @return Version + */ + public function minor() : Version + { + if ('' !== $this->preRelease && 0 === $this->patch) { + return new self($this->major, $this->minor, 0); + } + + return new self($this->major, $this->minor + 1, 0); + } + + /** + * @throws \SemVer\SemVer\Exception\InvalidArgumentException + * + * @return Version + */ + public function patch() : Version + { + if ('' !== $this->preRelease) { + return new self($this->major, $this->minor, $this->patch); + } + + return new self($this->major, $this->minor, $this->patch + 1); + } + /** * @param Version $other * diff --git a/composer.lock b/composer.lock index 0865f86..f6e9167 100644 --- a/composer.lock +++ b/composer.lock @@ -68,12 +68,12 @@ "source": { "type": "git", "url": "https://github.com/FriendsOfPHP/PHP-CS-Fixer.git", - "reference": "d0d76b434728fcf522270b67b454ed7e84e850ed" + "reference": "2b185644f973adeaa99f9270cdaa37d74d32f6a5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/FriendsOfPHP/PHP-CS-Fixer/zipball/d0d76b434728fcf522270b67b454ed7e84e850ed", - "reference": "d0d76b434728fcf522270b67b454ed7e84e850ed", + "url": "https://api.github.com/repos/FriendsOfPHP/PHP-CS-Fixer/zipball/2b185644f973adeaa99f9270cdaa37d74d32f6a5", + "reference": "2b185644f973adeaa99f9270cdaa37d74d32f6a5", "shasum": "" }, "require": { @@ -120,7 +120,7 @@ } ], "description": "A tool to automatically fix PHP code style", - "time": "2016-05-26 23:59:22" + "time": "2016-05-28 01:25:40" }, { "name": "guzzle/guzzle",