From a58fbdbc22a42836be5440360635602ad791fbde Mon Sep 17 00:00:00 2001 From: Julien Dufresne Date: Thu, 26 May 2016 21:28:35 +0200 Subject: [PATCH] Moves Sort to dedicated class --- README.md | 2 +- Tests/VersionSorterTest.php | 94 +++++++++++++++++++++++++++++++++++++ Tests/VersionTest.php | 55 +++++----------------- Version.php | 17 ------- VersionSorter.php | 37 +++++++++++++++ 5 files changed, 145 insertions(+), 60 deletions(-) create mode 100644 Tests/VersionSorterTest.php create mode 100644 VersionSorter.php diff --git a/README.md b/README.md index c5c8d4a..8b8f1aa 100644 --- a/README.md +++ b/README.md @@ -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'), diff --git a/Tests/VersionSorterTest.php b/Tests/VersionSorterTest.php new file mode 100644 index 0000000..7cf501c --- /dev/null +++ b/Tests/VersionSorterTest.php @@ -0,0 +1,94 @@ + + * + * 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'), + ], + ], + ]; + } +} diff --git a/Tests/VersionTest.php b/Tests/VersionTest.php index c745034..5a74ed3 100644 --- a/Tests/VersionTest.php +++ b/Tests/VersionTest.php @@ -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() //////////////////////////////////////////////////////////////////////////// @@ -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() //////////////////////////////////////////////////////////////////////////// diff --git a/Version.php b/Version.php index de8c432..63b4eb3 100644 --- a/Version.php +++ b/Version.php @@ -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 */ diff --git a/VersionSorter.php b/VersionSorter.php new file mode 100644 index 0000000..f25e51f --- /dev/null +++ b/VersionSorter.php @@ -0,0 +1,37 @@ + + * + * 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; + } +}