Skip to content

Commit

Permalink
Merge 9b514be into 2c0ce91
Browse files Browse the repository at this point in the history
  • Loading branch information
juliendufresne committed May 28, 2016
2 parents 2c0ce91 + 9b514be commit 4509a06
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .php_cs
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
86 changes: 76 additions & 10 deletions Tests/VersionTest.php
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions Version.php
Expand Up @@ -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
*
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4509a06

Please sign in to comment.