1
0
Fork 0
composer/tests/Composer/Test/DependencyResolver/RuleTest.php

112 lines
4.4 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
2012-01-22 21:06:09 +00:00
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\DependencyResolver;
use Composer\DependencyResolver\GenericRule;
2012-01-22 21:06:09 +00:00
use Composer\DependencyResolver\Rule;
use Composer\DependencyResolver\RuleSet;
use Composer\DependencyResolver\Pool;
use Composer\Package\Link;
2020-06-05 14:41:37 +00:00
use Composer\Semver\Constraint\MatchAllConstraint;
use Composer\Test\TestCase;
2012-01-22 21:06:09 +00:00
class RuleTest extends TestCase
{
public function testGetHash(): void
2012-01-22 21:06:09 +00:00
{
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(123), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
2015-07-08 17:36:13 +00:00
$hash = unpack('ihash', md5('123', true));
$this->assertEquals($hash['hash'], $rule->getHash());
2012-01-22 21:06:09 +00:00
}
public function testEqualsForRulesWithDifferentHashes(): void
2012-01-22 21:06:09 +00:00
{
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(1, 2), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$rule2 = new GenericRule(array(1, 3), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
$this->assertFalse($rule->equals($rule2));
}
public function testEqualsForRulesWithDifferLiteralsQuantity(): void
2012-01-22 21:06:09 +00:00
{
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(1, 12), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$rule2 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
$this->assertFalse($rule->equals($rule2));
}
public function testEqualsForRulesWithSameLiterals(): void
2012-01-22 21:06:09 +00:00
{
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(1, 12), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$rule2 = new GenericRule(array(1, 12), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
$this->assertTrue($rule->equals($rule2));
}
public function testSetAndGetType(): void
2012-01-22 21:06:09 +00:00
{
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$rule->setType(RuleSet::TYPE_REQUEST);
2012-01-22 21:06:09 +00:00
$this->assertEquals(RuleSet::TYPE_REQUEST, $rule->getType());
2012-01-22 21:06:09 +00:00
}
public function testEnable(): void
2012-01-22 21:06:09 +00:00
{
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
$rule->disable();
$rule->enable();
$this->assertTrue($rule->isEnabled());
$this->assertFalse($rule->isDisabled());
}
public function testDisable(): void
2012-01-22 21:06:09 +00:00
{
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
$rule->enable();
$rule->disable();
$this->assertTrue($rule->isDisabled());
$this->assertFalse($rule->isEnabled());
}
public function testIsAssertions(): void
2012-01-22 21:06:09 +00:00
{
2021-09-05 14:02:10 +00:00
$rule = new GenericRule(array(1, 12), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
$rule2 = new GenericRule(array(1), Rule::RULE_ROOT_REQUIRE, array('packageName' => '', 'constraint' => new MatchAllConstraint));
2012-01-22 21:06:09 +00:00
$this->assertFalse($rule->isAssertion());
$this->assertTrue($rule2->isAssertion());
}
public function testPrettyString(): void
2012-01-22 21:06:09 +00:00
{
$pool = new Pool(array(
$p1 = $this->getPackage('foo', '2.1'),
$p2 = $this->getPackage('baz', '1.1'),
));
2012-01-22 21:06:09 +00:00
$repositorySetMock = $this->getMockBuilder('Composer\Repository\RepositorySet')->disableOriginalConstructor()->getMock();
$requestMock = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
2020-06-05 14:41:37 +00:00
$emptyConstraint = new MatchAllConstraint();
2020-05-06 15:30:44 +00:00
$emptyConstraint->setPrettyString('*');
$rule = new GenericRule(array($p1->getId(), -$p2->getId()), Rule::RULE_PACKAGE_REQUIRES, new Link('baz', 'foo', $emptyConstraint));
2012-01-22 21:06:09 +00:00
2020-05-01 18:14:04 +00:00
$this->assertEquals('baz 1.1 relates to foo * -> satisfiable by foo[2.1].', $rule->getPrettyString($repositorySetMock, $requestMock, $pool, false));
2012-01-22 21:06:09 +00:00
}
}