1
0
Fork 0
composer/tests/Composer/Test/DependencyResolver/RuleSetTest.php

157 lines
6.2 KiB
PHP
Raw Permalink Normal View History

<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\DependencyResolver;
use Composer\DependencyResolver\GenericRule;
use Composer\DependencyResolver\Rule;
use Composer\DependencyResolver\RuleSet;
use Composer\DependencyResolver\Pool;
2021-09-05 14:02:10 +00:00
use Composer\Semver\Constraint\MatchAllConstraint;
use Composer\Semver\Constraint\MatchNoneConstraint;
use Composer\Test\TestCase;
2012-01-22 21:06:09 +00:00
class RuleSetTest extends TestCase
{
public function testAdd()
{
$rules = array(
RuleSet::TYPE_PACKAGE => array(),
RuleSet::TYPE_REQUEST => array(
2021-09-05 14:02:10 +00:00
new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint)),
new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint)),
),
RuleSet::TYPE_LEARNED => array(
2021-09-05 14:02:10 +00:00
new GenericRule(array(), Rule::RULE_LEARNED, 1),
),
);
$ruleSet = new RuleSet;
$ruleSet->add($rules[RuleSet::TYPE_REQUEST][0], RuleSet::TYPE_REQUEST);
$ruleSet->add($rules[RuleSet::TYPE_LEARNED][0], RuleSet::TYPE_LEARNED);
$ruleSet->add($rules[RuleSet::TYPE_REQUEST][1], RuleSet::TYPE_REQUEST);
$this->assertEquals($rules, $ruleSet->getRules());
}
2012-01-22 21:06:09 +00:00
2016-09-30 13:06:22 +00:00
public function testAddIgnoresDuplicates()
{
$rules = array(
RuleSet::TYPE_REQUEST => array(
2021-09-05 14:02:10 +00:00
new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint)),
new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint)),
new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint)),
2017-03-08 14:07:29 +00:00
),
2016-09-30 13:06:22 +00:00
);
$ruleSet = new RuleSet;
$ruleSet->add($rules[RuleSet::TYPE_REQUEST][0], RuleSet::TYPE_REQUEST);
$ruleSet->add($rules[RuleSet::TYPE_REQUEST][1], RuleSet::TYPE_REQUEST);
$ruleSet->add($rules[RuleSet::TYPE_REQUEST][2], RuleSet::TYPE_REQUEST);
2016-09-30 13:06:22 +00:00
$this->assertCount(1, $ruleSet->getIteratorFor(array(RuleSet::TYPE_REQUEST)));
2016-09-30 13:06:22 +00:00
}
2012-01-22 21:06:09 +00:00
public function testAddWhenTypeIsNotRecognized()
{
$ruleSet = new RuleSet;
$this->setExpectedException('OutOfBoundsException');
// @phpstan-ignore-next-line
2021-09-05 14:02:10 +00:00
$ruleSet->add(new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint)), 7);
2012-01-22 21:06:09 +00:00
}
public function testCount()
{
$ruleSet = new RuleSet;
2021-09-05 14:02:10 +00:00
$ruleSet->add(new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint)), RuleSet::TYPE_REQUEST);
$ruleSet->add(new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint)), RuleSet::TYPE_REQUEST);
2012-01-22 21:06:09 +00:00
$this->assertEquals(2, $ruleSet->count());
}
public function testRuleById()
{
$ruleSet = new RuleSet;
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$ruleSet->add($rule, RuleSet::TYPE_REQUEST);
2012-01-22 21:06:09 +00:00
$this->assertSame($rule, $ruleSet->ruleById[0]);
2012-01-22 21:06:09 +00:00
}
public function testGetIterator()
{
$ruleSet = new RuleSet;
2021-09-05 14:02:10 +00:00
$rule1 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$rule2 = new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$ruleSet->add($rule1, RuleSet::TYPE_REQUEST);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
2012-01-22 21:06:09 +00:00
$iterator = $ruleSet->getIterator();
$this->assertSame($rule1, $iterator->current());
$iterator->next();
$this->assertSame($rule2, $iterator->current());
}
public function testGetIteratorFor()
{
$ruleSet = new RuleSet;
2021-09-05 14:02:10 +00:00
$rule1 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$rule2 = new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
$ruleSet->add($rule1, RuleSet::TYPE_REQUEST);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
2012-01-22 21:06:09 +00:00
$iterator = $ruleSet->getIteratorFor(RuleSet::TYPE_LEARNED);
2012-01-22 21:06:09 +00:00
$this->assertSame($rule2, $iterator->current());
}
public function testGetIteratorWithout()
{
$ruleSet = new RuleSet;
2021-09-05 14:02:10 +00:00
$rule1 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$rule2 = new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
$ruleSet->add($rule1, RuleSet::TYPE_REQUEST);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
2012-01-22 21:06:09 +00:00
$iterator = $ruleSet->getIteratorWithout(RuleSet::TYPE_REQUEST);
2012-01-22 21:06:09 +00:00
$this->assertSame($rule2, $iterator->current());
}
public function testPrettyString()
2012-01-22 21:06:09 +00:00
{
$pool = new Pool(array(
$p = $this->getPackage('foo', '2.1'),
));
$repositorySetMock = $this->getMockBuilder('Composer\Repository\RepositorySet')->disableOriginalConstructor()->getMock();
$requestMock = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
2012-01-22 21:06:09 +00:00
$ruleSet = new RuleSet;
$literal = $p->getId();
$rule = new GenericRule(array($literal), Rule::RULE_ROOT_REQUIRE, array('packageName' => 'foo/bar', 'constraint' => new MatchNoneConstraint));
2012-01-22 21:06:09 +00:00
$ruleSet->add($rule, RuleSet::TYPE_REQUEST);
2012-01-22 21:06:09 +00:00
$this->assertStringContainsString('REQUEST : No package found to satisfy root composer.json require foo/bar', $ruleSet->getPrettyString($repositorySetMock, $requestMock, $pool));
2012-01-22 21:06:09 +00:00
}
}