Skip to content

Commit

Permalink
Moves Sort to dedicated class (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliendufresne committed May 26, 2016
1 parent 520f00c commit 657555f
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
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
@@ -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
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
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
@@ -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;
}
}

0 comments on commit 657555f

Please sign in to comment.