Skip to content
Snippets Groups Projects
Commit 4846b0f2 authored by omkar podey's avatar omkar podey Committed by Adam G-H
Browse files

Issue #3310000 by omkar.podey, phenaproxima: RequireEventTrait should default...

Issue #3310000 by omkar.podey, phenaproxima: RequireEventTrait should default unspecified version constraints to *
parent 843ea353
No related branches found
No related tags found
1 merge request!477Issue #3310000: Update \Drupal\package_manager\Event\RequireEventTrait to support Project Browser.
...@@ -50,7 +50,8 @@ trait RequireEventTrait { ...@@ -50,7 +50,8 @@ trait RequireEventTrait {
* *
* @return string[] * @return string[]
* An array of packages where the values are version constraints and keys * An array of packages where the values are version constraints and keys
* are package names in the form 'vendor/name'. * are package names in the form `vendor/name`. Packages without a version
* constraint will default to `*`.
*/ */
public function getRuntimePackages(): array { public function getRuntimePackages(): array {
return $this->getKeyedPackages($this->runtimePackages); return $this->getKeyedPackages($this->runtimePackages);
...@@ -61,7 +62,8 @@ trait RequireEventTrait { ...@@ -61,7 +62,8 @@ trait RequireEventTrait {
* *
* @return string[] * @return string[]
* An array of packages where the values are version constraints and keys * An array of packages where the values are version constraints and keys
* are package names in the form 'vendor/name'. * are package names in the form `vendor/name`. Packages without a version
* constraint will default to `*`.
*/ */
public function getDevPackages(): array { public function getDevPackages(): array {
return $this->getKeyedPackages($this->devPackages); return $this->getKeyedPackages($this->devPackages);
...@@ -75,12 +77,18 @@ trait RequireEventTrait { ...@@ -75,12 +77,18 @@ trait RequireEventTrait {
* *
* @return string[] * @return string[]
* An array of packages where the values are version constraints and keys * An array of packages where the values are version constraints and keys
* are package names in the form 'vendor/name'. * are package names in the form `vendor/name`. Packages without a version
* constraint will default to `*`.
*/ */
private function getKeyedPackages(array $packages): array { private function getKeyedPackages(array $packages): array {
$keyed_packages = []; $keyed_packages = [];
foreach ($packages as $package) { foreach ($packages as $package) {
[$name, $constraint] = explode(':', $package); if (strpos($package, ':') > 0) {
[$name, $constraint] = explode(':', $package);
}
else {
[$name, $constraint] = [$package, '*'];
}
$keyed_packages[$name] = $constraint; $keyed_packages[$name] = $constraint;
} }
return $keyed_packages; return $keyed_packages;
......
<?php
namespace Drupal\Tests\package_manager\Unit;
use Drupal\Tests\UnitTestCase;
/**
* @covers \Drupal\package_manager\Event\RequireEventTrait
*
* @group package_manager
*/
class RequireEventTraitTest extends UnitTestCase {
/**
* Tests that runtime and dev packages are keyed correctly.
*
* @param string[] $runtime_packages
* The runtime package constraints passed to the event constructor.
* @param string[] $dev_packages
* The dev package constraints passed to the event constructor.
* @param string[] $expected_runtime_packages
* The keyed runtime packages that should be returned by
* ::getRuntimePackages().
* @param string[] $expected_dev_packages
* The keyed dev packages that should be returned by ::getDevPackages().
*
* @dataProvider providerGetPackages
*/
public function testGetPackages(array $runtime_packages, array $dev_packages, array $expected_runtime_packages, array $expected_dev_packages): void {
$stage = $this->createMock('\Drupal\package_manager\Stage');
$events = [
'\Drupal\package_manager\Event\PostRequireEvent',
'\Drupal\package_manager\Event\PreRequireEvent',
];
foreach ($events as $event) {
/** @var \Drupal\package_manager\Event\RequireEventTrait $event */
$event = new $event($stage, $runtime_packages, $dev_packages);
$this->assertSame($expected_runtime_packages, $event->getRuntimePackages());
$this->assertSame($expected_dev_packages, $event->getDevPackages());
}
}
/**
* Data provider for testGetPackages().
*
* @return mixed[]
* The test cases.
*/
public function providerGetPackages(): array {
return [
'Package with constraint' => [
['drupal/new_package:^8.1'],
['drupal/dev_package:^9'],
['drupal/new_package' => '^8.1'],
['drupal/dev_package' => '^9'],
],
'Package without constraint' => [
['drupal/new_package'],
['drupal/dev_package'],
['drupal/new_package' => '*'],
['drupal/dev_package' => '*'],
],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment