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;
|
|
|
|
|
2017-02-16 22:00:57 +00:00
|
|
|
use Composer\DependencyResolver\GenericRule;
|
2012-01-22 21:06:09 +00:00
|
|
|
use Composer\DependencyResolver\Rule;
|
2015-07-09 15:21:45 +00:00
|
|
|
use Composer\DependencyResolver\RuleSet;
|
2012-05-19 18:38:56 +00:00
|
|
|
use Composer\DependencyResolver\Pool;
|
2020-01-19 22:11:36 +00:00
|
|
|
use Composer\Package\Link;
|
2020-06-05 14:41:37 +00:00
|
|
|
use Composer\Semver\Constraint\MatchAllConstraint;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
class RuleTest extends TestCase
|
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetHash(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([123], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-08-21 15:06:42 +00:00
|
|
|
$hash = unpack('ihash', (string) hash(\PHP_VERSION_ID > 80100 ? 'xxh3' : 'sha1', '123', true));
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals($hash['hash'], $rule->getHash());
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testEqualsForRulesWithDifferentHashes(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([1, 2], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
|
|
|
$rule2 = new GenericRule([1, 3], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertFalse($rule->equals($rule2));
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testEqualsForRulesWithDifferLiteralsQuantity(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([1, 12], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
|
|
|
$rule2 = new GenericRule([1], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertFalse($rule->equals($rule2));
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testEqualsForRulesWithSameLiterals(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([1, 12], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
|
|
|
$rule2 = new GenericRule([1, 12], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($rule->equals($rule2));
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSetAndGetType(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
2020-01-19 22:11:36 +00:00
|
|
|
$rule->setType(RuleSet::TYPE_REQUEST);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(RuleSet::TYPE_REQUEST, $rule->getType());
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testEnable(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
2012-01-22 21:06:09 +00:00
|
|
|
$rule->disable();
|
|
|
|
$rule->enable();
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($rule->isEnabled());
|
|
|
|
self::assertFalse($rule->isDisabled());
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDisable(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
2012-01-22 21:06:09 +00:00
|
|
|
$rule->enable();
|
|
|
|
$rule->disable();
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($rule->isDisabled());
|
|
|
|
self::assertFalse($rule->isEnabled());
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsAssertions(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([1, 12], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
|
|
|
$rule2 = new GenericRule([1], Rule::RULE_ROOT_REQUIRE, ['packageName' => '', 'constraint' => new MatchAllConstraint]);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertFalse($rule->isAssertion());
|
|
|
|
self::assertTrue($rule2->isAssertion());
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testPrettyString(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$pool = new Pool([
|
2022-11-24 13:39:08 +00:00
|
|
|
$p1 = self::getPackage('foo', '2.1'),
|
|
|
|
$p2 = self::getPackage('baz', '1.1'),
|
2022-08-17 12:20:07 +00:00
|
|
|
]);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2020-01-29 21:47:16 +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('*');
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$rule = new GenericRule([$p1->getId(), -$p2->getId()], Rule::RULE_PACKAGE_REQUIRES, new Link('baz', 'foo', $emptyConstraint));
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::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
|
|
|
}
|
|
|
|
}
|