RuleTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 RuleTest extends TestCase
  19. {
  20. protected $pool;
  21. public function setUp()
  22. {
  23. $this->pool = new Pool;
  24. }
  25. public function testGetHash()
  26. {
  27. $rule = new GenericRule(array(123), Rule::RULE_JOB_INSTALL, null);
  28. $hash = unpack('ihash', md5('123', true));
  29. $this->assertEquals($hash['hash'], $rule->getHash());
  30. }
  31. public function testEqualsForRulesWithDifferentHashes()
  32. {
  33. $rule = new GenericRule(array(1, 2), Rule::RULE_JOB_INSTALL, null);
  34. $rule2 = new GenericRule(array(1, 3), Rule::RULE_JOB_INSTALL, null);
  35. $this->assertFalse($rule->equals($rule2));
  36. }
  37. public function testEqualsForRulesWithDifferLiteralsQuantity()
  38. {
  39. $rule = new GenericRule(array(1, 12), Rule::RULE_JOB_INSTALL, null);
  40. $rule2 = new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null);
  41. $this->assertFalse($rule->equals($rule2));
  42. }
  43. public function testEqualsForRulesWithSameLiterals()
  44. {
  45. $rule = new GenericRule(array(1, 12), Rule::RULE_JOB_INSTALL, null);
  46. $rule2 = new GenericRule(array(1, 12), Rule::RULE_JOB_INSTALL, null);
  47. $this->assertTrue($rule->equals($rule2));
  48. }
  49. public function testSetAndGetType()
  50. {
  51. $rule = new GenericRule(array(), Rule::RULE_JOB_INSTALL, null);
  52. $rule->setType(RuleSet::TYPE_JOB);
  53. $this->assertEquals(RuleSet::TYPE_JOB, $rule->getType());
  54. }
  55. public function testEnable()
  56. {
  57. $rule = new GenericRule(array(), Rule::RULE_JOB_INSTALL, null);
  58. $rule->disable();
  59. $rule->enable();
  60. $this->assertTrue($rule->isEnabled());
  61. $this->assertFalse($rule->isDisabled());
  62. }
  63. public function testDisable()
  64. {
  65. $rule = new GenericRule(array(), Rule::RULE_JOB_INSTALL, null);
  66. $rule->enable();
  67. $rule->disable();
  68. $this->assertTrue($rule->isDisabled());
  69. $this->assertFalse($rule->isEnabled());
  70. }
  71. public function testIsAssertions()
  72. {
  73. $rule = new GenericRule(array(1, 12), Rule::RULE_JOB_INSTALL, null);
  74. $rule2 = new GenericRule(array(1), Rule::RULE_JOB_INSTALL, null);
  75. $this->assertFalse($rule->isAssertion());
  76. $this->assertTrue($rule2->isAssertion());
  77. }
  78. public function testPrettyString()
  79. {
  80. $repo = new ArrayRepository;
  81. $repo->addPackage($p1 = $this->getPackage('foo', '2.1'));
  82. $repo->addPackage($p2 = $this->getPackage('baz', '1.1'));
  83. $this->pool->addRepository($repo);
  84. $rule = new GenericRule(array($p1->getId(), -$p2->getId()), Rule::RULE_JOB_INSTALL, null);
  85. $this->assertEquals('Install command rule (don\'t install baz 1.1|install foo 2.1)', $rule->getPrettyString($this->pool));
  86. }
  87. }