From 80c273602183081a6344cbff5857816c4cc979ef Mon Sep 17 00:00:00 2001 From: Zbigniew Czapran Date: Thu, 30 Aug 2012 22:01:21 +0100 Subject: [PATCH 1/4] in VersionParser only truncate the version if hash --- .../Package/Version/VersionParser.php | 23 +- tests/Composer/Test/Mock/PackageMock.php | 239 ++++++++++++++++++ .../Package/Version/VersionParserTest.php | 35 +++ 3 files changed, 294 insertions(+), 3 deletions(-) create mode 100644 tests/Composer/Test/Mock/PackageMock.php diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index ce06891b5..7fa452ed2 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -26,6 +26,7 @@ use Composer\Package\LinkConstraint\VersionConstraint; class VersionParser { private static $modifierRegex = '[._-]?(?:(beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?'; + private static $sha1Length = 40; /** * Returns the stability of a version @@ -73,10 +74,26 @@ class VersionParser if (!$package->isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) { return $package->getPrettyVersion(); } - - return $package->getPrettyVersion() . ' ' . ($truncate ? substr($package->getSourceReference(), 0, 6) : $package->getSourceReference()); + + // if source reference is a sha1 hash -- truncate + if ($truncate && self::isHash($package->getSourceReference())) { + return $package->getPrettyVersion() . ' ' . substr($package->getSourceReference(), 0, 6); + } else { + return $package->getPrettyVersion() . ' ' . $package->getSourceReference(); + } } + /** + * Indicates whether version is in form of hash. + * + * @param string $version + * @return boolean + */ + private static function isHash($version) + { + return strlen($version) == self::$sha1Length; + } + /** * Normalizes a version string to be able to perform comparisons on it * @@ -274,5 +291,5 @@ class VersionParser } throw new \UnexpectedValueException('Could not parse version constraint '.$constraint); - } + } } diff --git a/tests/Composer/Test/Mock/PackageMock.php b/tests/Composer/Test/Mock/PackageMock.php new file mode 100644 index 000000000..2775571c1 --- /dev/null +++ b/tests/Composer/Test/Mock/PackageMock.php @@ -0,0 +1,239 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Mock; + +use Composer\Package\PackageInterface; + +/** + * Mock class for PackageInterface. + * + * More fields might be added if required in test cases. + */ +class PackageMock implements PackageInterface +{ + + private $isDev; + private $prettyVersion; + private $sourceReference; + private $sourceType; + + public function __toString() + { + return 'PackageMock'; + } + + public function getAlias() + { + + } + + public function getAutoload() + { + + } + + public function getBinaries() + { + + } + + public function getConflicts() + { + + } + + public function getDevRequires() + { + + } + + public function getDistReference() + { + + } + + public function getDistSha1Checksum() + { + + } + + public function getDistType() + { + + } + + public function getDistUrl() + { + + } + + public function getExtra() + { + + } + + public function getId() + { + + } + + public function getIncludePaths() + { + + } + + public function getInstallationSource() + { + + } + + public function getName() + { + + } + + public function getNames() + { + + } + + public function getPrettyAlias() + { + + } + + public function getPrettyName() + { + + } + + public function getPrettyString() + { + + } + + public function getPrettyVersion() + { + return $this->prettyVersion; + } + + public function getProvides() + { + + } + + public function getReleaseDate() + { + + } + + public function getReplaces() + { + + } + + public function getRepository() + { + + } + + public function getRequires() + { + + } + + public function getSourceReference() + { + return $this->sourceReference; + } + + public function getSourceType() + { + return $this->sourceType; + } + + public function getSourceUrl() + { + + } + + public function getStability() + { + + } + + public function getSuggests() + { + + } + + public function getTargetDir() + { + + } + + public function getType() + { + + } + + public function getUniqueName() + { + + } + + public function getVersion() + { + + } + + public function isDev() + { + return $this->isDev; + } + + public function setId($id) + { + + } + + public function setInstallationSource($type) + { + + } + + public function setIsDev($isDev) + { + $this->isDev = $isDev; + } + + public function setPrettyVersion($prettyVersion) + { + $this->prettyVersion = $prettyVersion; + } + + public function setSourceReference($sourceReference) + { + $this->sourceReference = $sourceReference; + } + + public function setSourceType($sourceType) + { + $this->sourceType = $sourceType; + } + + public function setRepository(\Composer\Repository\RepositoryInterface $repository) + { + + } +} diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index ff64184e3..98fac3493 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -15,9 +15,44 @@ namespace Composer\Test\Package\Version; use Composer\Package\Version\VersionParser; use Composer\Package\LinkConstraint\MultiConstraint; use Composer\Package\LinkConstraint\VersionConstraint; +use Composer\Package\PackageInterface; +use Composer\Test\Mock\PackageMock; class VersionParserTest extends \PHPUnit_Framework_TestCase { + /** + * @dataProvider formattedVersions + * + * @param \Composer\Package\PackageInterface $package + * @param string $expected + */ + public function testFormatVersionForDevPackage(PackageInterface $package, $truncate, $expected) + { + $this->assertSame($expected, VersionParser::formatVersion($package, $truncate)); + } + + public function formattedVersions() + { + $data = array( + array('sourceReference' => 'v2.1.0-RC2', 'truncate' => true, 'expected' => 'PrettyVersion v2.1.0-RC2'), + array('sourceReference' => 'bbf527a27356414bfa9bf520f018c5cb7af67c77', 'truncate' => true, 'expected' => 'PrettyVersion bbf527'), + array('sourceReference' => 'v1.0.0', 'truncate' => false, 'expected' => 'PrettyVersion v1.0.0'), + array('sourceReference' => 'bbf527a27356414bfa9bf520f018c5cb7af67c77', 'truncate' => false, 'expected' => 'PrettyVersion bbf527a27356414bfa9bf520f018c5cb7af67c77'), + ); + + $createPackage = function($arr) { + $package = new PackageMock(); + $package->setIsDev(true); + $package->setSourceType('git'); + $package->setPrettyVersion('PrettyVersion'); + $package->setSourceReference($arr['sourceReference']); + + return array($package, $arr['truncate'], $arr['expected']); + }; + + return array_map($createPackage, $data); + } + /** * @dataProvider successfulNormalizedVersions */ From 730f206ceae7b07ab8c56bb2b710dd1982d1a3f2 Mon Sep 17 00:00:00 2001 From: Zbigniew Czapran Date: Thu, 30 Aug 2012 23:44:54 +0100 Subject: [PATCH 2/4] removed unneccesary PackageMock and trailing whitespaces --- .../Package/Version/VersionParser.php | 8 +- tests/Composer/Test/Mock/PackageMock.php | 239 ------------------ .../Package/Version/VersionParserTest.php | 26 +- 3 files changed, 17 insertions(+), 256 deletions(-) delete mode 100644 tests/Composer/Test/Mock/PackageMock.php diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index 7fa452ed2..81e2612ac 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -74,7 +74,7 @@ class VersionParser if (!$package->isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) { return $package->getPrettyVersion(); } - + // if source reference is a sha1 hash -- truncate if ($truncate && self::isHash($package->getSourceReference())) { return $package->getPrettyVersion() . ' ' . substr($package->getSourceReference(), 0, 6); @@ -85,7 +85,7 @@ class VersionParser /** * Indicates whether version is in form of hash. - * + * * @param string $version * @return boolean */ @@ -93,7 +93,7 @@ class VersionParser { return strlen($version) == self::$sha1Length; } - + /** * Normalizes a version string to be able to perform comparisons on it * @@ -291,5 +291,5 @@ class VersionParser } throw new \UnexpectedValueException('Could not parse version constraint '.$constraint); - } + } } diff --git a/tests/Composer/Test/Mock/PackageMock.php b/tests/Composer/Test/Mock/PackageMock.php deleted file mode 100644 index 2775571c1..000000000 --- a/tests/Composer/Test/Mock/PackageMock.php +++ /dev/null @@ -1,239 +0,0 @@ - - * Jordi Boggiano - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Composer\Test\Mock; - -use Composer\Package\PackageInterface; - -/** - * Mock class for PackageInterface. - * - * More fields might be added if required in test cases. - */ -class PackageMock implements PackageInterface -{ - - private $isDev; - private $prettyVersion; - private $sourceReference; - private $sourceType; - - public function __toString() - { - return 'PackageMock'; - } - - public function getAlias() - { - - } - - public function getAutoload() - { - - } - - public function getBinaries() - { - - } - - public function getConflicts() - { - - } - - public function getDevRequires() - { - - } - - public function getDistReference() - { - - } - - public function getDistSha1Checksum() - { - - } - - public function getDistType() - { - - } - - public function getDistUrl() - { - - } - - public function getExtra() - { - - } - - public function getId() - { - - } - - public function getIncludePaths() - { - - } - - public function getInstallationSource() - { - - } - - public function getName() - { - - } - - public function getNames() - { - - } - - public function getPrettyAlias() - { - - } - - public function getPrettyName() - { - - } - - public function getPrettyString() - { - - } - - public function getPrettyVersion() - { - return $this->prettyVersion; - } - - public function getProvides() - { - - } - - public function getReleaseDate() - { - - } - - public function getReplaces() - { - - } - - public function getRepository() - { - - } - - public function getRequires() - { - - } - - public function getSourceReference() - { - return $this->sourceReference; - } - - public function getSourceType() - { - return $this->sourceType; - } - - public function getSourceUrl() - { - - } - - public function getStability() - { - - } - - public function getSuggests() - { - - } - - public function getTargetDir() - { - - } - - public function getType() - { - - } - - public function getUniqueName() - { - - } - - public function getVersion() - { - - } - - public function isDev() - { - return $this->isDev; - } - - public function setId($id) - { - - } - - public function setInstallationSource($type) - { - - } - - public function setIsDev($isDev) - { - $this->isDev = $isDev; - } - - public function setPrettyVersion($prettyVersion) - { - $this->prettyVersion = $prettyVersion; - } - - public function setSourceReference($sourceReference) - { - $this->sourceReference = $sourceReference; - } - - public function setSourceType($sourceType) - { - $this->sourceType = $sourceType; - } - - public function setRepository(\Composer\Repository\RepositoryInterface $repository) - { - - } -} diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index 98fac3493..c436e4813 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -16,13 +16,12 @@ use Composer\Package\Version\VersionParser; use Composer\Package\LinkConstraint\MultiConstraint; use Composer\Package\LinkConstraint\VersionConstraint; use Composer\Package\PackageInterface; -use Composer\Test\Mock\PackageMock; class VersionParserTest extends \PHPUnit_Framework_TestCase { /** * @dataProvider formattedVersions - * + * * @param \Composer\Package\PackageInterface $package * @param string $expected */ @@ -30,7 +29,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase { $this->assertSame($expected, VersionParser::formatVersion($package, $truncate)); } - + public function formattedVersions() { $data = array( @@ -39,20 +38,21 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase array('sourceReference' => 'v1.0.0', 'truncate' => false, 'expected' => 'PrettyVersion v1.0.0'), array('sourceReference' => 'bbf527a27356414bfa9bf520f018c5cb7af67c77', 'truncate' => false, 'expected' => 'PrettyVersion bbf527a27356414bfa9bf520f018c5cb7af67c77'), ); - - $createPackage = function($arr) { - $package = new PackageMock(); - $package->setIsDev(true); - $package->setSourceType('git'); - $package->setPrettyVersion('PrettyVersion'); - $package->setSourceReference($arr['sourceReference']); - + + $self = $this; + $createPackage = function($arr) use ($self) { + $package = $self->getMock('\Composer\Package\PackageInterface'); + $package->expects($self->once())->method('isDev')->will($self->returnValue(true)); + $package->expects($self->once())->method('getSourceType')->will($self->returnValue('git')); + $package->expects($self->once())->method('getPrettyVersion')->will($self->returnValue('PrettyVersion')); + $package->expects($self->any())->method('getSourceReference')->will($self->returnValue($arr['sourceReference'])); + return array($package, $arr['truncate'], $arr['expected']); }; - + return array_map($createPackage, $data); } - + /** * @dataProvider successfulNormalizedVersions */ From c0c23033d53a65cf52125f70658bef362ef79bf7 Mon Sep 17 00:00:00 2001 From: Zbigniew Czapran Date: Thu, 30 Aug 2012 23:46:33 +0100 Subject: [PATCH 3/4] simplified if statement --- src/Composer/Package/Version/VersionParser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index 81e2612ac..d5f809d18 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -78,9 +78,9 @@ class VersionParser // if source reference is a sha1 hash -- truncate if ($truncate && self::isHash($package->getSourceReference())) { return $package->getPrettyVersion() . ' ' . substr($package->getSourceReference(), 0, 6); - } else { - return $package->getPrettyVersion() . ' ' . $package->getSourceReference(); } + + return $package->getPrettyVersion() . ' ' . $package->getSourceReference(); } /** From 8d966547a28e792c4ef27446b267064a36851143 Mon Sep 17 00:00:00 2001 From: Zbigniew Czapran Date: Fri, 31 Aug 2012 16:37:03 +0100 Subject: [PATCH 4/4] removed overkill function isHash as suggested by @stloyd --- src/Composer/Package/Version/VersionParser.php | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index d5f809d18..c9a0944eb 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -26,7 +26,6 @@ use Composer\Package\LinkConstraint\VersionConstraint; class VersionParser { private static $modifierRegex = '[._-]?(?:(beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?'; - private static $sha1Length = 40; /** * Returns the stability of a version @@ -76,24 +75,13 @@ class VersionParser } // if source reference is a sha1 hash -- truncate - if ($truncate && self::isHash($package->getSourceReference())) { + if ($truncate && strlen($package->getSourceReference()) === 40) { return $package->getPrettyVersion() . ' ' . substr($package->getSourceReference(), 0, 6); } return $package->getPrettyVersion() . ' ' . $package->getSourceReference(); } - /** - * Indicates whether version is in form of hash. - * - * @param string $version - * @return boolean - */ - private static function isHash($version) - { - return strlen($version) == self::$sha1Length; - } - /** * Normalizes a version string to be able to perform comparisons on it *