Skip to content

Commit 1e0e1da

Browse files
committed
Fix DrupalGenerator
1 parent cb1c968 commit 1e0e1da

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
run: |
6565
composer config --no-plugins allow-plugins.infection/extension-installer true
6666
composer req infection/infection -W
67-
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4
67+
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4 --only-covered
6868
- name: Run phpstan
6969
if: ${{ matrix.php-versions == 8.3 && matrix.operating-system == 'ubuntu-latest' }}
7070
run: |

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ parameters:
22
level: 6
33
paths:
44
- src
5+
excludePaths:
6+
- src/Command/BaseNotTypedCommand.php
57
checkGenericClassInNonGenericObjectType: true
68
checkMissingIterableValueType: true
79
bootstrapFiles:

src/Url/DrupalGenerator.php

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
class DrupalGenerator extends GitlabGenerator
99
{
10+
const DRUPAL_CORE = 'drupal/core';
11+
1012
/**
1113
* {@inheritdoc}
1214
*/
1315
public function supportsPackage(PackageInterface $package)
1416
{
15-
return 'drupal/core' === $package->getName() || in_array($package->getType(), array('drupal-module', 'drupal-theme')) || parent::supportsPackage($package);
17+
return self::DRUPAL_CORE === $package->getName() || parent::supportsPackage($package);
1618
}
1719

1820
/**
@@ -24,13 +26,7 @@ protected function getCompareRef(PackageInterface $package)
2426
return $package->getDistReference();
2527
}
2628

27-
$reference = $package->getSourceReference();
28-
29-
if (40 === \strlen($reference)) {
30-
return \substr($reference, 0, 7);
31-
}
32-
33-
return $reference;
29+
return parent::getCompareRef($package);
3430
}
3531

3632
/**
@@ -43,31 +39,15 @@ public function getReleaseUrl(PackageInterface $package)
4339
return null;
4440
}
4541

46-
if ($package->getDistReference()) {
47-
$version = $package->getDistReference();
48-
}
49-
elseif ($package->getSourceReference()) {
50-
$version = $package->getSourceReference();
51-
}
52-
else {
53-
return null;
54-
}
55-
56-
return sprintf('%s/releases/%s', $this->getProjectUrl($package), $version);
42+
return sprintf('%s/releases/%s', $this->getProjectUrl($package), $this->getVersionReference($package));
5743
}
5844

5945
/**
6046
* {@inheritdoc}
6147
*/
6248
public function getProjectUrl(PackageInterface $package)
6349
{
64-
if ($package instanceof CompletePackageInterface) {
65-
return $package->getHomepage();
66-
}
67-
68-
$name = $this->getDrupalProjectName($package);
69-
70-
return sprintf('https://www.drupal.org/project/%s', $name);
50+
return sprintf('https://www.drupal.org/project/%s', $this->getDrupalProjectName($package));
7151
}
7252

7353
/**
@@ -78,15 +58,27 @@ protected function getDomain()
7858
return 'git.drupalcode.org';
7959
}
8060

81-
protected function getDrupalProjectName(PackageInterface $package)
61+
/**
62+
* @return string|null
63+
*/
64+
private function getVersionReference(PackageInterface $package)
8265
{
83-
list(, $name) = explode('/', $package->getName(), 2);
66+
if ($package->getDistReference()) {
67+
return $package->getDistReference();
68+
}
8469

85-
// Special handling for drupal/core only.
86-
if ('core' === $name) {
87-
$name = 'drupal';
70+
return $package->getSourceReference();
71+
}
72+
73+
/**
74+
* @return string
75+
*/
76+
private function getDrupalProjectName(PackageInterface $package)
77+
{
78+
if ($package->getName() === self::DRUPAL_CORE) {
79+
return 'drupal';
8880
}
8981

90-
return $name;
82+
return preg_replace('/^drupal\//', '', $package->getName());
9183
}
9284
}

tests/Url/DrupalGeneratorTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace IonBazan\ComposerDiff\Tests\Url;
44

5+
use Composer\Package\CompletePackageInterface;
6+
use Composer\Package\PackageInterface;
57
use IonBazan\ComposerDiff\Url\DrupalGenerator;
68

79
class DrupalGeneratorTest extends GeneratorTest
@@ -17,6 +19,10 @@ public function releaseUrlProvider()
1719
$this->getPackageWithSource('drupal/webform', '6.0.0', 'https://git.drupalcode.org/project/webform.git', '6.0.0'),
1820
'https://www.drupal.org/project/webform/releases/6.0.0',
1921
),
22+
'semver-semver-dist' => array(
23+
$this->getPackageWithSourceAndDist('drupal/webform', '6.0.0', '6.0.0', 'https://git.drupalcode.org/project/webform.git'),
24+
'https://www.drupal.org/project/webform/releases/6.0.0',
25+
),
2026
'core' => array(
2127
$this->getPackageWithSource('drupal/core', '9.0.0', 'https://github.com/drupal/core.git', '9.0.0'),
2228
'https://www.drupal.org/project/drupal/releases/9.0.0',
@@ -70,7 +76,12 @@ public function compareUrlProvider()
7076
$this->getPackageWithSourceAndDist('drupal/color_field', '2.4.0', '8.x-2.4', 'https://git.drupalcode.org/project/color_field.git'),
7177
$this->getPackageWithSourceAndDist('drupal/color_field', '2.5.0', '8.x-2.5', 'https://git.drupalcode.org/project/color_field.git'),
7278
'https://git.drupalcode.org/project/color_field/compare/8.x-2.4...8.x-2.5',
73-
),
79+
),
80+
'dev-version' => array(
81+
$this->getPackageWithSourceAndDist('drupal/color_field', '2.4.0', '8.x-2.4', 'https://git.drupalcode.org/project/color_field.git'),
82+
$this->getPackageWithSourceAndDist('drupal/color_field', 'dev-2.5.0', '8.x-2.5', 'https://git.drupalcode.org/project/color_field.git', 'd46283075d76ed244f7825b378eeb1cee246af73'),
83+
'https://git.drupalcode.org/project/color_field/compare/8.x-2.4...d462830',
84+
),
7485
);
7586
}
7687

@@ -80,13 +91,13 @@ public function compareUrlProvider()
8091
* @param string|null $sourceUrl
8192
* @param string|null $sourceReference
8293
*
83-
* @return mixed
94+
* @return PackageInterface
8495
*/
85-
protected function getPackageWithSourceAndDist($name, $version, $dist_version, $sourceUrl, $sourceReference = null)
96+
protected function getPackageWithSourceAndDist($name, $version, $distVersion, $sourceUrl, $sourceReference = null)
8697
{
8798
$package = $this->getPackage($name, $version, $sourceReference);
8899
$package->method('getSourceUrl')->willReturn($sourceUrl);
89-
$package->method('getDistReference')->willReturn($dist_version);
100+
$package->method('getDistReference')->willReturn($distVersion);
90101
$package->method('getSourceReference')->willReturn($sourceReference);
91102
$package->method('isDev')->willReturn(0 === strpos($version, 'dev-') || '-dev' === substr($version, -4));
92103

tests/Url/GeneratorContainerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public function testGetsProperGenerator()
1919
$this->assertNotSame($gitlabGenerator, $gitlab2Generator);
2020
$this->assertNull($container->get($this->getPackageWithSource('', '', 'https://gitlab3.org')));
2121
$this->assertNull($container->get($this->getPackageWithSource('', '', null)));
22+
$drupalGenerator = $container->get($this->getPackageWithSource('', '', 'https://git.drupalcode.org'));
23+
$this->assertInstanceOf('IonBazan\ComposerDiff\Url\DrupalGenerator', $drupalGenerator);
24+
$this->assertNotSame($gitlabGenerator, $drupalGenerator);
2225
}
2326

2427
public function testItSupportsPackageSupportedByOneOfTheGenerators()

0 commit comments

Comments
 (0)