2011-06-26 22:11:57 +00:00
|
|
|
<?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\Rule;
|
|
|
|
use Composer\DependencyResolver\RuleSet;
|
2012-05-19 18:38:56 +00:00
|
|
|
use Composer\DependencyResolver\Pool;
|
|
|
|
use Composer\Repository\ArrayRepository;
|
2013-09-25 08:14:42 +00:00
|
|
|
use Composer\TestCase;
|
2011-06-26 22:11:57 +00:00
|
|
|
|
2012-01-22 21:06:09 +00:00
|
|
|
class RuleSetTest extends TestCase
|
2011-06-26 22:11:57 +00:00
|
|
|
{
|
2012-05-19 18:38:56 +00:00
|
|
|
protected $pool;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->pool = new Pool;
|
|
|
|
}
|
|
|
|
|
2011-06-26 22:11:57 +00:00
|
|
|
public function testAdd()
|
|
|
|
{
|
|
|
|
$rules = array(
|
|
|
|
RuleSet::TYPE_PACKAGE => array(),
|
|
|
|
RuleSet::TYPE_JOB => array(
|
2012-05-19 18:38:56 +00:00
|
|
|
new Rule($this->pool, array(), 'job1', null),
|
|
|
|
new Rule($this->pool, array(), 'job2', null),
|
2011-06-26 22:11:57 +00:00
|
|
|
),
|
2012-04-27 16:28:18 +00:00
|
|
|
RuleSet::TYPE_LEARNED => array(
|
2012-05-19 18:38:56 +00:00
|
|
|
new Rule($this->pool, array(), 'update1', null),
|
2011-06-26 22:11:57 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$ruleSet = new RuleSet;
|
|
|
|
|
|
|
|
$ruleSet->add($rules[RuleSet::TYPE_JOB][0], RuleSet::TYPE_JOB);
|
2012-04-27 16:28:18 +00:00
|
|
|
$ruleSet->add($rules[RuleSet::TYPE_LEARNED][0], RuleSet::TYPE_LEARNED);
|
2011-06-26 22:11:57 +00:00
|
|
|
$ruleSet->add($rules[RuleSet::TYPE_JOB][1], RuleSet::TYPE_JOB);
|
|
|
|
|
|
|
|
$this->assertEquals($rules, $ruleSet->getRules());
|
|
|
|
}
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OutOfBoundsException
|
|
|
|
*/
|
|
|
|
public function testAddWhenTypeIsNotRecognized()
|
|
|
|
{
|
|
|
|
$ruleSet = new RuleSet;
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$ruleSet->add(new Rule($this->pool, array(), 'job1', null), 7);
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCount()
|
|
|
|
{
|
|
|
|
$ruleSet = new RuleSet;
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$ruleSet->add(new Rule($this->pool, array(), 'job1', null), RuleSet::TYPE_JOB);
|
|
|
|
$ruleSet->add(new Rule($this->pool, array(), 'job2', null), RuleSet::TYPE_JOB);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$this->assertEquals(2, $ruleSet->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRuleById()
|
|
|
|
{
|
|
|
|
$ruleSet = new RuleSet;
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$rule = new Rule($this->pool, array(), 'job1', null);
|
2012-01-22 21:06:09 +00:00
|
|
|
$ruleSet->add($rule, RuleSet::TYPE_JOB);
|
|
|
|
|
2014-12-01 17:28:45 +00:00
|
|
|
$this->assertSame($rule, $ruleSet->ruleById[0]);
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetIterator()
|
|
|
|
{
|
|
|
|
$ruleSet = new RuleSet;
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$rule1 = new Rule($this->pool, array(), 'job1', null);
|
|
|
|
$rule2 = new Rule($this->pool, array(), 'job1', null);
|
2012-01-22 21:06:09 +00:00
|
|
|
$ruleSet->add($rule1, RuleSet::TYPE_JOB);
|
2012-04-27 16:28:18 +00:00
|
|
|
$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;
|
2012-05-19 18:38:56 +00:00
|
|
|
$rule1 = new Rule($this->pool, array(), 'job1', null);
|
|
|
|
$rule2 = new Rule($this->pool, array(), 'job1', null);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$ruleSet->add($rule1, RuleSet::TYPE_JOB);
|
2012-04-27 16:28:18 +00:00
|
|
|
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2012-04-27 16:28:18 +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;
|
2012-05-19 18:38:56 +00:00
|
|
|
$rule1 = new Rule($this->pool, array(), 'job1', null);
|
|
|
|
$rule2 = new Rule($this->pool, array(), 'job1', null);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$ruleSet->add($rule1, RuleSet::TYPE_JOB);
|
2012-04-27 16:28:18 +00:00
|
|
|
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$iterator = $ruleSet->getIteratorWithout(RuleSet::TYPE_JOB);
|
|
|
|
|
|
|
|
$this->assertSame($rule2, $iterator->current());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testContainsEqual()
|
|
|
|
{
|
|
|
|
$ruleSet = new RuleSet;
|
|
|
|
|
|
|
|
$rule = $this->getRuleMock();
|
|
|
|
$rule->expects($this->any())
|
|
|
|
->method('getHash')
|
|
|
|
->will($this->returnValue('rule_1_hash'));
|
|
|
|
$rule->expects($this->any())
|
|
|
|
->method('equals')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$rule2 = $this->getRuleMock();
|
|
|
|
$rule2->expects($this->any())
|
|
|
|
->method('getHash')
|
|
|
|
->will($this->returnValue('rule_2_hash'));
|
|
|
|
|
|
|
|
$rule3 = $this->getRuleMock();
|
|
|
|
$rule3->expects($this->any())
|
|
|
|
->method('getHash')
|
|
|
|
->will($this->returnValue('rule_1_hash'));
|
|
|
|
$rule3->expects($this->any())
|
|
|
|
->method('equal')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2012-04-27 16:28:18 +00:00
|
|
|
$ruleSet->add($rule, RuleSet::TYPE_LEARNED);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$this->assertTrue($ruleSet->containsEqual($rule));
|
|
|
|
$this->assertFalse($ruleSet->containsEqual($rule2));
|
|
|
|
$this->assertFalse($ruleSet->containsEqual($rule3));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testToString()
|
|
|
|
{
|
2012-05-19 18:38:56 +00:00
|
|
|
$repo = new ArrayRepository;
|
|
|
|
$repo->addPackage($p = $this->getPackage('foo', '2.1'));
|
|
|
|
$this->pool->addRepository($repo);
|
|
|
|
|
2012-01-22 21:06:09 +00:00
|
|
|
$ruleSet = new RuleSet;
|
2012-05-19 18:38:56 +00:00
|
|
|
$literal = $p->getId();
|
|
|
|
$rule = new Rule($this->pool, array($literal), 'job1', null);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2012-04-27 16:28:18 +00:00
|
|
|
$ruleSet->add($rule, RuleSet::TYPE_JOB);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2012-04-27 16:28:18 +00:00
|
|
|
$this->assertContains('JOB : (+foo-2.1.0.0)', $ruleSet->__toString());
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getRuleMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Composer\DependencyResolver\Rule')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
2011-06-26 22:11:57 +00:00
|
|
|
}
|