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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ $versions = [
Version::fromString('1.3.3-alpha.2'),
Version::fromString('1.2.3-rc.1+exp.sha.5114f85'),
];
var_dump(Version::sort($versions));
var_dump(VersionSorter::sort($versions));
// Result:
// [
// Version::fromString('1.2.3-rc.1+exp.sha.5114f85'),
Expand Down
94 changes: 94 additions & 0 deletions Tests/VersionSorterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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\VersionSorter;

/**
* Test VersionSorter class.
*/
final class VersionSorterTest extends PHPUnit_Framework_TestCase
{
////////////////////////////////////////////////////////////////////////////
// ::sort()
////////////////////////////////////////////////////////////////////////////

/**
* @dataProvider provideVersions
*
* @param array $versions
* @param array $expectedResult
*/
public function testSort(array $versions, array $expectedResult)
{
$result = VersionSorter::sort($versions);

static::assertCount(count($versions), $result, 'sort result should contain every items.');
static::assertCount(count($expectedResult), $result);

do {
$version = array_shift($result);
$expectedVersion = array_shift($expectedResult);
static::assertEquals(
$expectedVersion->getMajor(),
$version->getMajor(),
'::sort() versions must be ordered by major version'
);
static::assertEquals(
$expectedVersion->getMinor(),
$version->getMinor(),
'::sort() versions must be ordered by major version'
);
static::assertEquals(
$expectedVersion->getPatch(),
$version->getPatch(),
'::sort() versions must be ordered by major version'
);
static::assertEquals(
$expectedVersion->getPreRelease(),
$version->getPreRelease(),
'::sort() versions must be ordered by major version'
);
} while (count($result));
}

/**
* @return array
*/
public function provideVersions() : array
{
return [
[
[
Version::fromString('2.0.0'),
Version::fromString('1.2.3'),
Version::fromString('1.3.3'),
Version::fromString('1.3.3-alpha.10'),
Version::fromString('1.3.3-alpha.2'),
Version::fromString('1.2.3-rc.1+exp.sha.5114f85'),
],
[
Version::fromString('1.2.3-rc.1+exp.sha.5114f85'),
Version::fromString('1.2.3'),
Version::fromString('1.3.3-alpha.2'),
Version::fromString('1.3.3-alpha.10'),
Version::fromString('1.3.3'),
Version::fromString('2.0.0'),
],
],
];
}
}
55 changes: 13 additions & 42 deletions Tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,48 +199,6 @@ public function provideWrongStringVersion() : array
];
}

////////////////////////////////////////////////////////////////////////////
// sort()
////////////////////////////////////////////////////////////////////////////
public function testSort()
{
$result = Version::sort(
[
Version::fromString('2.0.0'),
Version::fromString('1.10.10'),
]
);
$expectedResult =
[
Version::fromString('1.10.10'),
Version::fromString('2.0.0'),
];
do {
$shiftResult = array_shift($result);
$shiftExpected = array_shift($expectedResult);
static::assertEquals(
$shiftExpected->getMajor(),
$shiftResult->getMajor(),
'::sort() versions must be ordered by major version'
);
static::assertEquals(
$shiftExpected->getMinor(),
$shiftResult->getMinor(),
'::sort() versions must be ordered by major version'
);
static::assertEquals(
$shiftExpected->getPatch(),
$shiftResult->getPatch(),
'::sort() versions must be ordered by major version'
);
static::assertEquals(
$shiftExpected->getPreRelease(),
$shiftResult->getPreRelease(),
'::sort() versions must be ordered by major version'
);
} while (count($result) || count($expectedResult));
}

////////////////////////////////////////////////////////////////////////////
// __toString()
////////////////////////////////////////////////////////////////////////////
Expand All @@ -258,6 +216,19 @@ public function testToString()
}
}

////////////////////////////////////////////////////////////////////////////
// equals()
////////////////////////////////////////////////////////////////////////////
public function testEquals()
{
$current = new Version(1, 0, 0);
$other = new Version(1, 0, 0);
static::assertEquals(true, $other->equals($current));
$current = new Version(1, 1, 0);
$other = new Version(1, 0, 0);
static::assertEquals(false, $other->equals($current));
}

////////////////////////////////////////////////////////////////////////////
// greaterThan()
////////////////////////////////////////////////////////////////////////////
Expand Down
17 changes: 0 additions & 17 deletions Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,6 @@ public static function fromString(string $version) : Version
);
}

/**
* @param array $versions
*
* @return array
*/
public static function sort(array $versions) : array
{
usort(
$versions,
function (Version $a, Version $b) {
return VersionComparator::compare($a, $b);
}
);

return $versions;
}

/**
* @return int
*/
Expand Down
37 changes: 37 additions & 0 deletions VersionSorter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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;

/**
* Sort multiple Version objects.
*/
final class VersionSorter
{
/**
* @param array $versions
*
* @return array
*/
public static function sort(array $versions) : array
{
usort(
$versions,
function (Version $a, Version $b) {
return VersionComparator::compare($a, $b);
}
);

return $versions;
}
}