Loading package_manager/src/Event/RequireEventTrait.php +12 −4 Original line number Diff line number Diff line Loading @@ -50,7 +50,8 @@ trait RequireEventTrait { * * @return string[] * 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 { return $this->getKeyedPackages($this->runtimePackages); Loading @@ -61,7 +62,8 @@ trait RequireEventTrait { * * @return string[] * 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 { return $this->getKeyedPackages($this->devPackages); Loading @@ -75,12 +77,18 @@ trait RequireEventTrait { * * @return string[] * 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 { $keyed_packages = []; foreach ($packages as $package) { if (strpos($package, ':') > 0) { [$name, $constraint] = explode(':', $package); } else { [$name, $constraint] = [$package, '*']; } $keyed_packages[$name] = $constraint; } return $keyed_packages; Loading package_manager/tests/src/Unit/RequireEventTraitTest.php 0 → 100644 +67 −0 Original line number Diff line number Diff line <?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' => '*'], ], ]; } } Loading
package_manager/src/Event/RequireEventTrait.php +12 −4 Original line number Diff line number Diff line Loading @@ -50,7 +50,8 @@ trait RequireEventTrait { * * @return string[] * 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 { return $this->getKeyedPackages($this->runtimePackages); Loading @@ -61,7 +62,8 @@ trait RequireEventTrait { * * @return string[] * 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 { return $this->getKeyedPackages($this->devPackages); Loading @@ -75,12 +77,18 @@ trait RequireEventTrait { * * @return string[] * 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 { $keyed_packages = []; foreach ($packages as $package) { if (strpos($package, ':') > 0) { [$name, $constraint] = explode(':', $package); } else { [$name, $constraint] = [$package, '*']; } $keyed_packages[$name] = $constraint; } return $keyed_packages; Loading
package_manager/tests/src/Unit/RequireEventTraitTest.php 0 → 100644 +67 −0 Original line number Diff line number Diff line <?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' => '*'], ], ]; } }