RuleTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\Rule;
  13. use Composer\DependencyResolver\Literal;
  14. use Composer\Test\TestCase;
  15. class RuleTest extends TestCase
  16. {
  17. public function testGetHash()
  18. {
  19. $rule = new Rule(array(), 'job1', null);
  20. $rule->ruleHash = '123';
  21. $this->assertEquals('123', $rule->getHash());
  22. }
  23. public function testSetAndGetId()
  24. {
  25. $rule = new Rule(array(), 'job1', null);
  26. $rule->setId(666);
  27. $this->assertEquals(666, $rule->getId());
  28. }
  29. public function testEqualsForRulesWithDifferentHashes()
  30. {
  31. $rule = new Rule(array(), 'job1', null);
  32. $rule->ruleHash = '123';
  33. $rule2 = new Rule(array(), 'job1', null);
  34. $rule2->ruleHash = '321';
  35. $this->assertFalse($rule->equals($rule2));
  36. }
  37. public function testEqualsForRulesWithDifferentLiterals()
  38. {
  39. $literal = $this->getLiteralMock();
  40. $literal->expects($this->any())
  41. ->method('getId')
  42. ->will($this->returnValue(1));
  43. $rule = new Rule(array($literal), 'job1', null);
  44. $rule->ruleHash = '123';
  45. $literal = $this->getLiteralMock();
  46. $literal->expects($this->any())
  47. ->method('getId')
  48. ->will($this->returnValue(12));
  49. $rule2 = new Rule(array($literal), 'job1', null);
  50. $rule2->ruleHash = '123';
  51. $this->assertFalse($rule->equals($rule2));
  52. }
  53. public function testEqualsForRulesWithDifferLiteralsQuantity()
  54. {
  55. $literal = $this->getLiteralMock();
  56. $literal->expects($this->any())
  57. ->method('getId')
  58. ->will($this->returnValue(1));
  59. $literal2 = $this->getLiteralMock();
  60. $literal2->expects($this->any())
  61. ->method('getId')
  62. ->will($this->returnValue(12));
  63. $rule = new Rule(array($literal, $literal2), 'job1', null);
  64. $rule->ruleHash = '123';
  65. $rule2 = new Rule(array($literal), 'job1', null);
  66. $rule2->ruleHash = '123';
  67. $this->assertFalse($rule->equals($rule2));
  68. }
  69. public function testEqualsForRulesWithThisSameLiterals()
  70. {
  71. $literal = $this->getLiteralMock();
  72. $literal->expects($this->any())
  73. ->method('getId')
  74. ->will($this->returnValue(1));
  75. $literal2 = $this->getLiteralMock();
  76. $literal2->expects($this->any())
  77. ->method('getId')
  78. ->will($this->returnValue(12));
  79. $rule = new Rule(array($literal, $literal2), 'job1', null);
  80. $rule2 = new Rule(array($literal, $literal2), 'job1', null);
  81. $this->assertTrue($rule->equals($rule2));
  82. }
  83. public function testSetAndGetType()
  84. {
  85. $rule = new Rule(array(), 'job1', null);
  86. $rule->setType('someType');
  87. $this->assertEquals('someType', $rule->getType());
  88. }
  89. public function testEnable()
  90. {
  91. $rule = new Rule(array(), 'job1', null);
  92. $rule->disable();
  93. $rule->enable();
  94. $this->assertTrue($rule->isEnabled());
  95. $this->assertFalse($rule->isDisabled());
  96. }
  97. public function testDisable()
  98. {
  99. $rule = new Rule(array(), 'job1', null);
  100. $rule->enable();
  101. $rule->disable();
  102. $this->assertTrue($rule->isDisabled());
  103. $this->assertFalse($rule->isEnabled());
  104. }
  105. public function testSetWeak()
  106. {
  107. $rule = new Rule(array(), 'job1', null);
  108. $rule->setWeak(true);
  109. $rule2 = new Rule(array(), 'job1', null);
  110. $rule2->setWeak(false);
  111. $this->assertTrue($rule->isWeak());
  112. $this->assertFalse($rule2->isWeak());
  113. }
  114. public function testIsAssertions()
  115. {
  116. $literal = $this->getLiteralMock();
  117. $literal2 = $this->getLiteralMock();
  118. $rule = new Rule(array($literal, $literal2), 'job1', null);
  119. $rule2 = new Rule(array($literal), 'job1', null);
  120. $this->assertFalse($rule->isAssertion());
  121. $this->assertTrue($rule2->isAssertion());
  122. }
  123. public function testToString()
  124. {
  125. $literal = new Literal($this->getPackage('foo', '2.1'), true);
  126. $literal2 = new Literal($this->getPackage('baz', '1.1'), false);
  127. $rule = new Rule(array($literal, $literal2), 'job1', null);
  128. $this->assertEquals('(-baz-1.1.0.0|+foo-2.1.0.0)', $rule->__toString());
  129. }
  130. private function getLiteralMock()
  131. {
  132. return $this->getMockBuilder('Composer\DependencyResolver\Literal')
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. }
  136. }