RuleTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Package\Link;
  18. use Composer\Repository\ArrayRepository;
  19. use Composer\Test\TestCase;
  20. class RuleTest extends TestCase
  21. {
  22. public function testGetHash()
  23. {
  24. $rule = new GenericRule(array(123), Rule::RULE_ROOT_REQUIRE, null);
  25. $hash = unpack('ihash', md5('123', true));
  26. $this->assertEquals($hash['hash'], $rule->getHash());
  27. }
  28. public function testEqualsForRulesWithDifferentHashes()
  29. {
  30. $rule = new GenericRule(array(1, 2), Rule::RULE_ROOT_REQUIRE, null);
  31. $rule2 = new GenericRule(array(1, 3), Rule::RULE_ROOT_REQUIRE, null);
  32. $this->assertFalse($rule->equals($rule2));
  33. }
  34. public function testEqualsForRulesWithDifferLiteralsQuantity()
  35. {
  36. $rule = new GenericRule(array(1, 12), Rule::RULE_ROOT_REQUIRE, null);
  37. $rule2 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, null);
  38. $this->assertFalse($rule->equals($rule2));
  39. }
  40. public function testEqualsForRulesWithSameLiterals()
  41. {
  42. $rule = new GenericRule(array(1, 12), Rule::RULE_ROOT_REQUIRE, null);
  43. $rule2 = new GenericRule(array(1, 12), Rule::RULE_ROOT_REQUIRE, null);
  44. $this->assertTrue($rule->equals($rule2));
  45. }
  46. public function testSetAndGetType()
  47. {
  48. $rule = new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, null);
  49. $rule->setType(RuleSet::TYPE_REQUEST);
  50. $this->assertEquals(RuleSet::TYPE_REQUEST, $rule->getType());
  51. }
  52. public function testEnable()
  53. {
  54. $rule = new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, null);
  55. $rule->disable();
  56. $rule->enable();
  57. $this->assertTrue($rule->isEnabled());
  58. $this->assertFalse($rule->isDisabled());
  59. }
  60. public function testDisable()
  61. {
  62. $rule = new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, null);
  63. $rule->enable();
  64. $rule->disable();
  65. $this->assertTrue($rule->isDisabled());
  66. $this->assertFalse($rule->isEnabled());
  67. }
  68. public function testIsAssertions()
  69. {
  70. $rule = new GenericRule(array(1, 12), Rule::RULE_ROOT_REQUIRE, null);
  71. $rule2 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, null);
  72. $this->assertFalse($rule->isAssertion());
  73. $this->assertTrue($rule2->isAssertion());
  74. }
  75. public function testPrettyString()
  76. {
  77. $pool = new Pool(array(
  78. $p1 = $this->getPackage('foo', '2.1'),
  79. $p2 = $this->getPackage('baz', '1.1'),
  80. ));
  81. $repositorySetMock = $this->getMockBuilder('Composer\Repository\RepositorySet')->disableOriginalConstructor()->getMock();
  82. $requestMock = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  83. $rule = new GenericRule(array($p1->getId(), -$p2->getId()), Rule::RULE_PACKAGE_REQUIRES, new Link('baz', 'foo'));
  84. $this->assertEquals('baz 1.1 relates to foo -> satisfiable by foo[2.1].', $rule->getPrettyString($repositorySetMock, $requestMock, $pool, false));
  85. }
  86. }