Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ $version = Version::fromString('1.2.3-rc.1+exp.sha.5114f85');

```php
use SemVer\SemVer\Version;
use SemVer\SemVer\VersionComparator;

$version1 = Version::fromString('1.2.3');
$version2 = Version::fromString('1.2.3-rc.1+exp.sha.5114f85');
var_dump($version1->compare($version2)); // 1
var_dump(VersionComparator::compare($version1, $version2)); // 1
var_dump($version1->equals($version2)); // false
var_dump($version1->greaterThan($version2)); // true
var_dump($version1->greaterThanOrEqual($version2)); // true
Expand Down
168 changes: 168 additions & 0 deletions Tests/VersionComparatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

/*
* This file is part of semver/semver.
*
* (c) SemVer <https://github.com/git-pull-request>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace SemVer\SemVer\Tests;

use PHPUnit_Framework_TestCase;
use SemVer\SemVer\Version;
use SemVer\SemVer\VersionComparator;

/**
* Test of VersionComparator object.
*/
final class VersionComparatorTest extends PHPUnit_Framework_TestCase
{
////////////////////////////////////////////////////////////////////////////
// ::compare()
////////////////////////////////////////////////////////////////////////////

/**
* @dataProvider provideCompareVersions
*
* @param Version $version1
* @param Version $version2
* @param int $expectedResult
* @param string $message
*/
public function testCompare(Version $version1, Version $version2, int $expectedResult, string $message)
{
static::assertEquals($expectedResult, VersionComparator::compare($version1, $version2), $message);
}

/**
* @return array
*/
public function provideCompareVersions() : array
{
return [
// major
[
Version::fromString('1.0.0'),
Version::fromString('2.0.0'),
-1,
'::compare() major version. 1.0.0 is lower than 2.0.0',
],
[
Version::fromString('2.0.0'),
Version::fromString('1.0.0'),
1,
'::compare() major version. 2.0.0 is greater than 1.0.0',
],
[
Version::fromString('10.0.0'),
Version::fromString('2.0.0'),
1,
'::compare() major version compares numerically. 10.0.0 is greater than 2.0.0',
],
// minor
[
Version::fromString('2.0.0'),
Version::fromString('2.10.0'),
-1,
'::compare() minor version. 2.0.0 is lower than 2.10.0',
],
[
Version::fromString('2.10.0'),
Version::fromString('2.0.0'),
1,
'::compare() minor version. 2.10.0 is greater than 2.0.0',
],
[
Version::fromString('2.10.0'),
Version::fromString('2.2.0'),
1,
'::compare() minor version compares numerically. 2.10.0 is greater than 2.2.0',
],
// patch
[
Version::fromString('2.0.0'),
Version::fromString('2.0.10'),
-1,
'::compare() patch version. 2.0.0 is lower than 2.0.10',
],
[
Version::fromString('2.0.10'),
Version::fromString('2.0.0'),
1,
'::compare() patch version. 2.0.10 is greater than 2.0.0',
],
[
Version::fromString('2.0.10'),
Version::fromString('2.0.2'),
1,
'::compare() patch version. 2.0.10 is greater than 2.0.2',
],
[
Version::fromString('2.0.0'),
Version::fromString('2.0.0+build'),
0,
'::compare() build differs. versions should be equals.',
],
// pre release
[
Version::fromString('2.0.0'),
Version::fromString('2.0.0-alpha'),
1,
'::compare() second version has a pre-release, the first not',
],
[
Version::fromString('2.0.0-alpha'),
Version::fromString('2.0.0'),
-1,
'::compare() first version has a pre-release, the second not',
],
[
Version::fromString('2.0.0-alpha.1'),
Version::fromString('2.0.0-alpha'),
1,
'::compare() first has two pre-release identifiers, second only has one',
],
[
Version::fromString('2.0.0-alpha'),
Version::fromString('2.0.0-alpha.1'),
-1,
'::compare() second has two pre-release identifiers, first only has one',
],
[
Version::fromString('2.0.0-1'),
Version::fromString('2.0.0-beta'),
-1,
'::compare() a numeric identifier is lower than an alphabetical one',
],
[
Version::fromString('2.0.0-beta'),
Version::fromString('2.0.0-1'),
1,
'::compare() an alphabetical identifier is greater than a numeric one',
],
[
Version::fromString('2.0.0-alpha.1'),
Version::fromString('2.0.0-alpha.beta'),
-1,
'::compare() a numeric identifier is lower than an alphabetical one even when multiple identifiers given',
],
[
Version::fromString('2.0.0-alpha.10'),
Version::fromString('2.0.0-alpha.2'),
1,
'::compare() an alphabetical identifier is greater than a numeric one even when multiple identifiers given',
],
[
Version::fromString('2.0.0-alpha+build127'),
Version::fromString('2.0.0-alpha+build128'),
0,
'::compare() two versions that only differs with their build are equals.',
],
];
}
}
131 changes: 0 additions & 131 deletions Tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,137 +241,6 @@ public function testSort()
} while (count($result) || count($expectedResult));
}

////////////////////////////////////////////////////////////////////////////
// compare()
////////////////////////////////////////////////////////////////////////////

/**
* @dataProvider provideCompareVersions
*
* @param Version $version1
* @param Version $version2
* @param int $expectedResult
* @param string $message
*/
public function testCompare(Version $version1, Version $version2, int $expectedResult, string $message)
{
static::assertEquals($expectedResult, $version1->compare($version2), $message);
}

/**
* @return array
*/
public function provideCompareVersions() : array
{
return [
// major
[
Version::fromString('1.0.0'),
Version::fromString('2.0.0'),
-1,
'::compare() versions must be ordered by major version (current lower than other)',
],
[
Version::fromString('2.0.0'),
Version::fromString('1.0.0'),
1,
'::compare() versions must be ordered by major version (current greater than other)',
],
[
Version::fromString('2.0.0'),
Version::fromString('10.0.0'),
-1,
'::compare() versions must be ordered by major version numerically',
],
// minor
[
Version::fromString('2.10.0'),
Version::fromString('2.0.0'),
1,
'::compare() if major versions are equals, then it must be ordered by minor version (current lower than other)',
],
[
Version::fromString('2.0.0'),
Version::fromString('2.10.0'),
-1,
'::compare() if major versions are equals, then it must be ordered by minor version (current greater than other)',
],
[
Version::fromString('2.10.0'),
Version::fromString('2.2.0'),
1,
'::compare() if major versions are equals, then it must be ordered by minor version numerically',
],
// patch
[
Version::fromString('2.0.10'),
Version::fromString('2.0.0'),
1,
'::compare() if major and minor versions are equals, then it must be ordered by patch version numerically (current lower than other)',
],
[
Version::fromString('2.0.0'),
Version::fromString('2.0.10'),
-1,
'::compare() if major and minor versions are equals, then it must be ordered by patch version numerically (current greater than other)',
],
[
Version::fromString('2.0.10'),
Version::fromString('2.0.2'),
1,
'::compare() if major and minor versions are equals, then it must be ordered by patch version numerically',
],
[
Version::fromString('2.0.0'),
Version::fromString('2.0.0+build'),
0,
'::compare() if major, minor and patch versions are equals and both versions do not have pre-release, then they are equals',
],
[
Version::fromString('2.0.0'),
Version::fromString('2.0.0-alpha'),
1,
'::compare() When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version. (current without pre-release)',
],
[
Version::fromString('2.0.0-alpha'),
Version::fromString('2.0.0'),
-1,
'::compare() A larger set of pre-release fields has a higher precedence than a smaller set',
],
[
Version::fromString('2.0.0-alpha.1'),
Version::fromString('2.0.0-alpha'),
1,
'::compare() A larger set of pre-release fields has a higher precedence than a smaller set (multiple level)',
],
[
Version::fromString('2.0.0-1'),
Version::fromString('2.0.0-beta'),
-1,
'::compare() Precedence for two pre-release versions with the same major, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers',
],
[
Version::fromString('2.0.0-beta'),
Version::fromString('2.0.0-1'),
1,
'::compare() Precedence for two pre-release versions with the same major, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers',
],
[
Version::fromString('2.0.0-alpha.1'),
Version::fromString('2.0.0-alpha.beta'),
-1,
'::compare() Precedence for two pre-release versions with the same major, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers. Test with multiple identifiers level.',
],
[
Version::fromString('2.0.0-alpha.10'),
Version::fromString('2.0.0-alpha.2'),
1,
'::compare() numeric pre-release, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers. Test with multiple identifiers level.',
],
];
}

////////////////////////////////////////////////////////////////////////////
// __toString()
////////////////////////////////////////////////////////////////////////////
Expand Down
Loading