RuleSetTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\Package\BasePackage;
  17. use Composer\Repository\ArrayRepository;
  18. use Composer\Test\TestCase;
  19. class RuleSetTest extends TestCase
  20. {
  21. public function testAdd()
  22. {
  23. $rules = array(
  24. RuleSet::TYPE_PACKAGE => array(),
  25. RuleSet::TYPE_REQUEST => array(
  26. new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, null),
  27. new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, null),
  28. ),
  29. RuleSet::TYPE_LEARNED => array(
  30. new GenericRule(array(), Rule::RULE_INTERNAL_ALLOW_UPDATE, null),
  31. ),
  32. );
  33. $ruleSet = new RuleSet;
  34. $ruleSet->add($rules[RuleSet::TYPE_REQUEST][0], RuleSet::TYPE_REQUEST);
  35. $ruleSet->add($rules[RuleSet::TYPE_LEARNED][0], RuleSet::TYPE_LEARNED);
  36. $ruleSet->add($rules[RuleSet::TYPE_REQUEST][1], RuleSet::TYPE_REQUEST);
  37. $this->assertEquals($rules, $ruleSet->getRules());
  38. }
  39. public function testAddIgnoresDuplicates()
  40. {
  41. $rules = array(
  42. RuleSet::TYPE_REQUEST => array(
  43. new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, null),
  44. new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, null),
  45. new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, null),
  46. ),
  47. );
  48. $ruleSet = new RuleSet;
  49. $ruleSet->add($rules[RuleSet::TYPE_REQUEST][0], RuleSet::TYPE_REQUEST);
  50. $ruleSet->add($rules[RuleSet::TYPE_REQUEST][1], RuleSet::TYPE_REQUEST);
  51. $ruleSet->add($rules[RuleSet::TYPE_REQUEST][2], RuleSet::TYPE_REQUEST);
  52. $this->assertCount(1, $ruleSet->getIteratorFor(array(RuleSet::TYPE_REQUEST)));
  53. }
  54. /**
  55. * @expectedException \OutOfBoundsException
  56. */
  57. public function testAddWhenTypeIsNotRecognized()
  58. {
  59. $ruleSet = new RuleSet;
  60. $ruleSet->add(new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, null), 7);
  61. }
  62. public function testCount()
  63. {
  64. $ruleSet = new RuleSet;
  65. $ruleSet->add(new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, null), RuleSet::TYPE_REQUEST);
  66. $ruleSet->add(new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, null), RuleSet::TYPE_REQUEST);
  67. $this->assertEquals(2, $ruleSet->count());
  68. }
  69. public function testRuleById()
  70. {
  71. $ruleSet = new RuleSet;
  72. $rule = new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, null);
  73. $ruleSet->add($rule, RuleSet::TYPE_REQUEST);
  74. $this->assertSame($rule, $ruleSet->ruleById[0]);
  75. }
  76. public function testGetIterator()
  77. {
  78. $ruleSet = new RuleSet;
  79. $rule1 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, null);
  80. $rule2 = new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, null);
  81. $ruleSet->add($rule1, RuleSet::TYPE_REQUEST);
  82. $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
  83. $iterator = $ruleSet->getIterator();
  84. $this->assertSame($rule1, $iterator->current());
  85. $iterator->next();
  86. $this->assertSame($rule2, $iterator->current());
  87. }
  88. public function testGetIteratorFor()
  89. {
  90. $ruleSet = new RuleSet;
  91. $rule1 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, null);
  92. $rule2 = new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, null);
  93. $ruleSet->add($rule1, RuleSet::TYPE_REQUEST);
  94. $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
  95. $iterator = $ruleSet->getIteratorFor(RuleSet::TYPE_LEARNED);
  96. $this->assertSame($rule2, $iterator->current());
  97. }
  98. public function testGetIteratorWithout()
  99. {
  100. $ruleSet = new RuleSet;
  101. $rule1 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, null);
  102. $rule2 = new GenericRule(array(2), Rule::RULE_ROOT_REQUIRE, null);
  103. $ruleSet->add($rule1, RuleSet::TYPE_REQUEST);
  104. $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
  105. $iterator = $ruleSet->getIteratorWithout(RuleSet::TYPE_REQUEST);
  106. $this->assertSame($rule2, $iterator->current());
  107. }
  108. public function testPrettyString()
  109. {
  110. $pool = new Pool(array(
  111. $p = $this->getPackage('foo', '2.1'),
  112. ));
  113. $repositorySetMock = $this->getMockBuilder('Composer\Repository\RepositorySet')->disableOriginalConstructor()->getMock();
  114. $requestMock = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  115. $ruleSet = new RuleSet;
  116. $literal = $p->getId();
  117. $rule = new GenericRule(array($literal), Rule::RULE_ROOT_REQUIRE, array('packageName' => 'foo/bar', 'constraint' => null));
  118. $ruleSet->add($rule, RuleSet::TYPE_REQUEST);
  119. $this->assertContains('REQUEST : No package found to satisfy root composer.json require foo/bar', $ruleSet->getPrettyString($repositorySetMock, $requestMock, $pool));
  120. }
  121. }