2022-02-23 15:58:18 +00:00
< ? php declare ( strict_types = 1 );
2011-06-26 22:11:57 +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 ;
2011-06-26 22:11:57 +00:00
use Composer\DependencyResolver\Rule ;
use Composer\DependencyResolver\RuleSet ;
2012-05-19 18:38:56 +00:00
use Composer\DependencyResolver\Pool ;
2021-09-05 14:02:10 +00:00
use Composer\Semver\Constraint\MatchAllConstraint ;
2021-08-21 15:41:52 +00:00
use Composer\Semver\Constraint\MatchNoneConstraint ;
2018-11-12 14:23:32 +00:00
use Composer\Test\TestCase ;
2011-06-26 22:11:57 +00:00
2012-01-22 21:06:09 +00:00
class RuleSetTest extends TestCase
2011-06-26 22:11:57 +00:00
{
2022-02-18 09:38:54 +00:00
public function testAdd () : void
2011-06-26 22:11:57 +00:00
{
2022-08-17 12:20:07 +00:00
$rules = [
RuleSet :: TYPE_PACKAGE => [],
RuleSet :: TYPE_REQUEST => [
new GenericRule ([ 1 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]),
new GenericRule ([ 2 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]),
],
RuleSet :: TYPE_LEARNED => [
new GenericRule ([], Rule :: RULE_LEARNED , 1 ),
],
];
2011-06-26 22:11:57 +00:00
$ruleSet = new RuleSet ;
2020-01-19 22:11:36 +00:00
$ruleSet -> add ( $rules [ RuleSet :: TYPE_REQUEST ][ 0 ], RuleSet :: TYPE_REQUEST );
2012-04-27 16:28:18 +00:00
$ruleSet -> add ( $rules [ RuleSet :: TYPE_LEARNED ][ 0 ], RuleSet :: TYPE_LEARNED );
2020-01-19 22:11:36 +00:00
$ruleSet -> add ( $rules [ RuleSet :: TYPE_REQUEST ][ 1 ], RuleSet :: TYPE_REQUEST );
2011-06-26 22:11:57 +00:00
2024-05-29 21:12:06 +00:00
self :: assertEquals ( $rules , $ruleSet -> getRules ());
2011-06-26 22:11:57 +00:00
}
2012-01-22 21:06:09 +00:00
2022-02-18 09:38:54 +00:00
public function testAddIgnoresDuplicates () : void
2016-09-30 13:06:22 +00:00
{
2022-08-17 12:20:07 +00:00
$rules = [
RuleSet :: TYPE_REQUEST => [
new GenericRule ([], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]),
new GenericRule ([], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]),
new GenericRule ([], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]),
],
];
2016-09-30 13:06:22 +00:00
$ruleSet = new RuleSet ;
2020-01-19 22:11:36 +00:00
$ruleSet -> add ( $rules [ RuleSet :: TYPE_REQUEST ][ 0 ], RuleSet :: TYPE_REQUEST );
$ruleSet -> add ( $rules [ RuleSet :: TYPE_REQUEST ][ 1 ], RuleSet :: TYPE_REQUEST );
$ruleSet -> add ( $rules [ RuleSet :: TYPE_REQUEST ][ 2 ], RuleSet :: TYPE_REQUEST );
2016-09-30 13:06:22 +00:00
2024-05-29 21:12:06 +00:00
self :: assertCount ( 1 , $ruleSet -> getIteratorFor ([ RuleSet :: TYPE_REQUEST ]));
2016-09-30 13:06:22 +00:00
}
2022-02-18 09:38:54 +00:00
public function testAddWhenTypeIsNotRecognized () : void
2012-01-22 21:06:09 +00:00
{
$ruleSet = new RuleSet ;
2021-12-09 19:55:26 +00:00
self :: expectException ( 'OutOfBoundsException' );
2024-05-22 07:09:04 +00:00
// @phpstan-ignore argument.type
2022-08-17 12:20:07 +00:00
$ruleSet -> add ( new GenericRule ([], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]), 7 );
2012-01-22 21:06:09 +00:00
}
2022-02-18 09:38:54 +00:00
public function testCount () : void
2012-01-22 21:06:09 +00:00
{
$ruleSet = new RuleSet ;
2022-08-17 12:20:07 +00:00
$ruleSet -> add ( new GenericRule ([ 1 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]), RuleSet :: TYPE_REQUEST );
$ruleSet -> add ( new GenericRule ([ 2 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]), RuleSet :: TYPE_REQUEST );
2012-01-22 21:06:09 +00:00
2024-05-29 21:12:06 +00:00
self :: assertEquals ( 2 , $ruleSet -> count ());
2012-01-22 21:06:09 +00:00
}
2022-02-18 09:38:54 +00:00
public function testRuleById () : void
2012-01-22 21:06:09 +00:00
{
$ruleSet = new RuleSet ;
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
$ruleSet -> add ( $rule , RuleSet :: TYPE_REQUEST );
2012-01-22 21:06:09 +00:00
2024-05-29 21:12:06 +00:00
self :: assertSame ( $rule , $ruleSet -> ruleById [ 0 ]);
2012-01-22 21:06:09 +00:00
}
2022-02-18 09:38:54 +00:00
public function testGetIterator () : void
2012-01-22 21:06:09 +00:00
{
$ruleSet = new RuleSet ;
2022-08-17 12:20:07 +00:00
$rule1 = new GenericRule ([ 1 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]);
$rule2 = new GenericRule ([ 2 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]);
2020-01-19 22:11:36 +00:00
$ruleSet -> add ( $rule1 , RuleSet :: TYPE_REQUEST );
2012-04-27 16:28:18 +00:00
$ruleSet -> add ( $rule2 , RuleSet :: TYPE_LEARNED );
2012-01-22 21:06:09 +00:00
$iterator = $ruleSet -> getIterator ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( $rule1 , $iterator -> current ());
2012-01-22 21:06:09 +00:00
$iterator -> next ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( $rule2 , $iterator -> current ());
2012-01-22 21:06:09 +00:00
}
2022-02-18 09:38:54 +00:00
public function testGetIteratorFor () : void
2012-01-22 21:06:09 +00:00
{
$ruleSet = new RuleSet ;
2022-08-17 12:20:07 +00:00
$rule1 = new GenericRule ([ 1 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]);
$rule2 = new GenericRule ([ 2 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]);
2012-01-22 21:06:09 +00:00
2020-01-19 22:11:36 +00:00
$ruleSet -> add ( $rule1 , RuleSet :: TYPE_REQUEST );
2012-04-27 16:28:18 +00:00
$ruleSet -> add ( $rule2 , RuleSet :: TYPE_LEARNED );
2012-01-22 21:06:09 +00:00
2012-04-27 16:28:18 +00:00
$iterator = $ruleSet -> getIteratorFor ( RuleSet :: TYPE_LEARNED );
2012-01-22 21:06:09 +00:00
2024-05-29 21:12:06 +00:00
self :: assertSame ( $rule2 , $iterator -> current ());
2012-01-22 21:06:09 +00:00
}
2022-02-18 09:38:54 +00:00
public function testGetIteratorWithout () : void
2012-01-22 21:06:09 +00:00
{
$ruleSet = new RuleSet ;
2022-08-17 12:20:07 +00:00
$rule1 = new GenericRule ([ 1 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]);
$rule2 = new GenericRule ([ 2 ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => '' , 'constraint' => new MatchAllConstraint ]);
2012-01-22 21:06:09 +00:00
2020-01-19 22:11:36 +00:00
$ruleSet -> add ( $rule1 , RuleSet :: TYPE_REQUEST );
2012-04-27 16:28:18 +00:00
$ruleSet -> add ( $rule2 , RuleSet :: TYPE_LEARNED );
2012-01-22 21:06:09 +00:00
2020-01-19 22:11:36 +00:00
$iterator = $ruleSet -> getIteratorWithout ( RuleSet :: TYPE_REQUEST );
2012-01-22 21:06:09 +00:00
2024-05-29 21:12:06 +00:00
self :: assertSame ( $rule2 , $iterator -> current ());
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
$p = self :: getPackage ( 'foo' , '2.1' ),
2022-08-17 12:20:07 +00:00
]);
2012-05-19 18:38:56 +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 ();
2012-01-22 21:06:09 +00:00
$ruleSet = new RuleSet ;
2012-05-19 18:38:56 +00:00
$literal = $p -> getId ();
2022-08-17 12:20:07 +00:00
$rule = new GenericRule ([ $literal ], Rule :: RULE_ROOT_REQUIRE , [ 'packageName' => 'foo/bar' , 'constraint' => new MatchNoneConstraint ]);
2012-01-22 21:06:09 +00:00
2020-01-19 22:11:36 +00:00
$ruleSet -> add ( $rule , RuleSet :: TYPE_REQUEST );
2012-01-22 21:06:09 +00:00
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( 'REQUEST : No package found to satisfy root composer.json require foo/bar' , $ruleSet -> getPrettyString ( $repositorySetMock , $requestMock , $pool ));
2012-01-22 21:06:09 +00:00
}
2011-06-26 22:11:57 +00:00
}