RuleSetTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\DependencyResolver;
  12. use Composer\DependencyResolver\GenericRule;
  13. use Composer\DependencyResolver\Rule;
  14. use Composer\DependencyResolver\RuleSet;
  15. use Composer\DependencyResolver\Pool;
  16. use Composer\Repository\ArrayRepository;
  17. use Composer\Test\TestCase;
  18. class RuleSetTest extends TestCase
  19. {
  20. protected $pool;
  21. public function setUp()
  22. {
  23. $this->pool = new Pool;
  24. }
  25. public function testAdd()
  26. {
  27. $rules = array(
  28. RuleSet::TYPE_PACKAGE => array(),
  29. RuleSet::TYPE_JOB => array(
  30. new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null),
  31. new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null),
  32. ),
  33. RuleSet::TYPE_LEARNED => array(
  34. new GenericRule(array(), Rule::RULE_INTERNAL_ALLOW_UPDATE, null),
  35. ),
  36. );
  37. $ruleSet = new RuleSet;
  38. $ruleSet->add($rules[RuleSet::TYPE_JOB][0], RuleSet::TYPE_JOB);
  39. $ruleSet->add($rules[RuleSet::TYPE_LEARNED][0], RuleSet::TYPE_LEARNED);
  40. $ruleSet->add($rules[RuleSet::TYPE_JOB][1], RuleSet::TYPE_JOB);
  41. $this->assertEquals($rules, $ruleSet->getRules());
  42. }
  43. public function testAddIgnoresDuplicates()
  44. {
  45. $rules = array(
  46. RuleSet::TYPE_JOB => array(
  47. new GenericRule(array(), Rule::RULE_JOB_INSTALL, null),
  48. new GenericRule(array(), Rule::RULE_JOB_INSTALL, null),
  49. new GenericRule(array(), Rule::RULE_JOB_INSTALL, null),
  50. ),
  51. );
  52. $ruleSet = new RuleSet;
  53. $ruleSet->add($rules[RuleSet::TYPE_JOB][0], RuleSet::TYPE_JOB);
  54. $ruleSet->add($rules[RuleSet::TYPE_JOB][1], RuleSet::TYPE_JOB);
  55. $ruleSet->add($rules[RuleSet::TYPE_JOB][2], RuleSet::TYPE_JOB);
  56. $this->assertCount(1, $ruleSet->getIteratorFor(array(RuleSet::TYPE_JOB)));
  57. }
  58. /**
  59. * @expectedException \OutOfBoundsException
  60. */
  61. public function testAddWhenTypeIsNotRecognized()
  62. {
  63. $ruleSet = new RuleSet;
  64. $ruleSet->add(new GenericRule(array(), Rule::RULE_JOB_INSTALL, null), 7);
  65. }
  66. public function testCount()
  67. {
  68. $ruleSet = new RuleSet;
  69. $ruleSet->add(new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null), RuleSet::TYPE_JOB);
  70. $ruleSet->add(new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null), RuleSet::TYPE_JOB);
  71. $this->assertEquals(2, $ruleSet->count());
  72. }
  73. public function testRuleById()
  74. {
  75. $ruleSet = new RuleSet;
  76. $rule = new GenericRule(array(), Rule::RULE_JOB_INSTALL, null);
  77. $ruleSet->add($rule, RuleSet::TYPE_JOB);
  78. $this->assertSame($rule, $ruleSet->ruleById[0]);
  79. }
  80. public function testGetIterator()
  81. {
  82. $ruleSet = new RuleSet;
  83. $rule1 = new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null);
  84. $rule2 = new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null);
  85. $ruleSet->add($rule1, RuleSet::TYPE_JOB);
  86. $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
  87. $iterator = $ruleSet->getIterator();
  88. $this->assertSame($rule1, $iterator->current());
  89. $iterator->next();
  90. $this->assertSame($rule2, $iterator->current());
  91. }
  92. public function testGetIteratorFor()
  93. {
  94. $ruleSet = new RuleSet;
  95. $rule1 = new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null);
  96. $rule2 = new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null);
  97. $ruleSet->add($rule1, RuleSet::TYPE_JOB);
  98. $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
  99. $iterator = $ruleSet->getIteratorFor(RuleSet::TYPE_LEARNED);
  100. $this->assertSame($rule2, $iterator->current());
  101. }
  102. public function testGetIteratorWithout()
  103. {
  104. $ruleSet = new RuleSet;
  105. $rule1 = new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null);
  106. $rule2 = new GenericRule(array(2), Rule::RULE_JOB_INSTALL, null);
  107. $ruleSet->add($rule1, RuleSet::TYPE_JOB);
  108. $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
  109. $iterator = $ruleSet->getIteratorWithout(RuleSet::TYPE_JOB);
  110. $this->assertSame($rule2, $iterator->current());
  111. }
  112. public function testPrettyString()
  113. {
  114. $repo = new ArrayRepository;
  115. $repo->addPackage($p = $this->getPackage('foo', '2.1'));
  116. $this->pool->addRepository($repo);
  117. $ruleSet = new RuleSet;
  118. $literal = $p->getId();
  119. $rule = new GenericRule(array($literal), Rule::RULE_JOB_INSTALL, null);
  120. $ruleSet->add($rule, RuleSet::TYPE_JOB);
  121. $this->assertContains('JOB : Install command rule (install foo 2.1)', $ruleSet->getPrettyString($this->pool));
  122. }
  123. }