From e130386612338a40a4fa0325ba65f0f2b056ebf2 Mon Sep 17 00:00:00 2001 From: Alexey Prilipko Date: Tue, 19 Jun 2012 12:08:36 +1100 Subject: [PATCH 1/2] Add '<>' operator for Version Parser --- .../Package/Version/VersionParser.php | 2 +- .../Test/DependencyResolver/SolverTest.php | 24 +++++++++++++++++++ .../Package/Version/VersionParserTest.php | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index 891f16dd0..d5f1cd135 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -228,7 +228,7 @@ class VersionParser } // match operators constraints - if (preg_match('{^(>=?|<=?|==?)?\s*(.*)}', $constraint, $matches)) { + if (preg_match('{^(<>|!=|>=?|<=?|==?)?\s*(.*)}', $constraint, $matches)) { try { $version = $this->normalize($matches[2]); diff --git a/tests/Composer/Test/DependencyResolver/SolverTest.php b/tests/Composer/Test/DependencyResolver/SolverTest.php index 7638e9509..5f51184b7 100644 --- a/tests/Composer/Test/DependencyResolver/SolverTest.php +++ b/tests/Composer/Test/DependencyResolver/SolverTest.php @@ -115,6 +115,30 @@ class SolverTest extends TestCase )); } + public function testSolverInstallHonoursNotEqualOperator() + { + $this->repo->addPackage($packageA = $this->getPackage('A', '1.0')); + $this->repo->addPackage($packageB = $this->getPackage('B', '1.0')); + $this->repo->addPackage($newPackageB11 = $this->getPackage('B', '1.1')); + $this->repo->addPackage($newPackageB12 = $this->getPackage('B', '1.2')); + $this->repo->addPackage($newPackageB13 = $this->getPackage('B', '1.3')); + + $packageA->setRequires(array( + new Link('A', 'B', $this->getVersionConstraint('<=', '1.3'), 'requires'), + new Link('A', 'B', $this->getVersionConstraint('<>', '1.3'), 'requires'), + new Link('A', 'B', $this->getVersionConstraint('!=', '1.2'), 'requires'), + )); + + $this->reposComplete(); + + $this->request->install('A'); + + $this->checkSolverResult(array( + array('job' => 'install', 'package' => $newPackageB11), + array('job' => 'install', 'package' => $packageA), + )); + } + public function testSolverInstallWithDepsInOrder() { $this->repo->addPackage($packageA = $this->getPackage('A', '1.0')); diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index 0e436d48c..ec99b3b27 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -143,6 +143,8 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase 'match any/2' => array('*.*', new MultiConstraint(array())), 'match any/3' => array('*.x.*', new MultiConstraint(array())), 'match any/4' => array('x.x.x.*', new MultiConstraint(array())), + 'not equal' => array('<>1.0.0', new VersionConstraint('<>', '1.0.0.0')), + 'not equal/2' => array('!=1.0.0', new VersionConstraint('!=', '1.0.0.0')), 'greater than' => array('>1.0.0', new VersionConstraint('>', '1.0.0.0')), 'lesser than' => array('<1.2.3.4', new VersionConstraint('<', '1.2.3.4')), 'less/eq than' => array('<=1.2.3', new VersionConstraint('<=', '1.2.3.0')), From 9f08764e9a2d8e070639eb7a3e5895b9fb07ff7a Mon Sep 17 00:00:00 2001 From: Alexey Prilipko Date: Tue, 19 Jun 2012 23:37:29 +1100 Subject: [PATCH 2/2] Add '!=' handling to VersionConstraint::matchSpecific --- .../Package/LinkConstraint/VersionConstraint.php | 16 ++++++++++++++++ .../LinkConstraint/VersionConstraintTest.php | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Composer/Package/LinkConstraint/VersionConstraint.php b/src/Composer/Package/LinkConstraint/VersionConstraint.php index 2590aa3df..e88c418b3 100644 --- a/src/Composer/Package/LinkConstraint/VersionConstraint.php +++ b/src/Composer/Package/LinkConstraint/VersionConstraint.php @@ -36,6 +36,10 @@ class VersionConstraint extends SpecificConstraint $operator = '=='; } + if ('<>' === $operator) { + $operator = '!='; + } + $this->operator = $operator; $this->version = $version; } @@ -49,6 +53,18 @@ class VersionConstraint extends SpecificConstraint $noEqualOp = str_replace('=', '', $this->operator); $providerNoEqualOp = str_replace('=', '', $provider->operator); + $isEqualOp = '==' === $this->operator; + $isNonEqualOp = '!=' === $this->operator; + $isProviderEqualOp = '==' === $provider->operator; + $isProviderNonEqualOp = '!=' === $provider->operator; + + // '!=' operator is match when other operator is not '==' operator or version is not match + // these kinds of comparisons always have a solution + if ($isNonEqualOp || $isProviderNonEqualOp) { + return !$isEqualOp && !$isProviderEqualOp + || version_compare($provider->version, $this->version, '!='); + } + // an example for the condition is <= 2.0 & < 1.0 // these kinds of comparisons always have a solution if ($this->operator != '==' && $noEqualOp == $providerNoEqualOp) { diff --git a/tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php b/tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php index 15c66c8e7..cdd6e67f5 100644 --- a/tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php +++ b/tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php @@ -27,6 +27,12 @@ class VersionConstraintTest extends \PHPUnit_Framework_TestCase array('<=', '2', '>=', '1'), array('>=', '1', '<=', '2'), array('==', '2', '>=', '2'), + array('!=', '1', '!=', '1'), + array('!=', '1', '==', '2'), + array('!=', '1', '<', '1'), + array('!=', '1', '<=', '1'), + array('!=', '1', '>', '1'), + array('!=', '1', '>=', '1'), ); } @@ -53,6 +59,8 @@ class VersionConstraintTest extends \PHPUnit_Framework_TestCase array('<=', '1', '>=', '2'), array('>=', '2', '<=', '1'), array('==', '2', '<', '2'), + array('!=', '1', '==', '1'), + array('==', '1', '!=', '1'), ); }