Skip to content

Commit

Permalink
Fix DrupalGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan committed Apr 26, 2024
1 parent cb1c968 commit cca2afc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
run: |
composer config --no-plugins allow-plugins.infection/extension-installer true
composer req infection/infection -W
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4 --only-covered
- name: Run phpstan
if: ${{ matrix.php-versions == 8.3 && matrix.operating-system == 'ubuntu-latest' }}
run: |
Expand Down
50 changes: 18 additions & 32 deletions src/Url/DrupalGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

class DrupalGenerator extends GitlabGenerator
{
const DRUPAL_CORE = 'drupal/core';

/**
* {@inheritdoc}
*/
public function supportsPackage(PackageInterface $package)
{
return 'drupal/core' === $package->getName() || in_array($package->getType(), array('drupal-module', 'drupal-theme')) || parent::supportsPackage($package);
return self::DRUPAL_CORE === $package->getName() || parent::supportsPackage($package);
}

/**
Expand All @@ -24,13 +26,7 @@ protected function getCompareRef(PackageInterface $package)
return $package->getDistReference();
}

$reference = $package->getSourceReference();

if (40 === \strlen($reference)) {
return \substr($reference, 0, 7);
}

return $reference;
return parent::getCompareRef($package);
}

/**
Expand All @@ -43,31 +39,15 @@ public function getReleaseUrl(PackageInterface $package)
return null;
}

if ($package->getDistReference()) {
$version = $package->getDistReference();
}
elseif ($package->getSourceReference()) {
$version = $package->getSourceReference();
}
else {
return null;
}

return sprintf('%s/releases/%s', $this->getProjectUrl($package), $version);
return sprintf('%s/releases/%s', $this->getProjectUrl($package), $this->getVersionReference($package));
}

/**
* {@inheritdoc}
*/
public function getProjectUrl(PackageInterface $package)
{
if ($package instanceof CompletePackageInterface) {
return $package->getHomepage();
}

$name = $this->getDrupalProjectName($package);

return sprintf('https://www.drupal.org/project/%s', $name);
return sprintf('https://www.drupal.org/project/%s', $this->getDrupalProjectName($package));
}

/**
Expand All @@ -78,15 +58,21 @@ protected function getDomain()
return 'git.drupalcode.org';
}

protected function getDrupalProjectName(PackageInterface $package)
private function getVersionReference(PackageInterface $package)

Check failure on line 61 in src/Url/DrupalGenerator.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 on ubuntu-latest Composer v2 (highest)

Method IonBazan\ComposerDiff\Url\DrupalGenerator::getVersionReference() has no return type specified.
{
list(, $name) = explode('/', $package->getName(), 2);
if ($package->getDistReference()) {
return $package->getDistReference();
}

return $package->getSourceReference();
}

// Special handling for drupal/core only.
if ('core' === $name) {
$name = 'drupal';
private function getDrupalProjectName(PackageInterface $package)

Check failure on line 70 in src/Url/DrupalGenerator.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 on ubuntu-latest Composer v2 (highest)

Method IonBazan\ComposerDiff\Url\DrupalGenerator::getDrupalProjectName() has no return type specified.
{
if ($package->getName() === self::DRUPAL_CORE) {
return 'drupal';
}

return $name;
return preg_replace('/^drupal\//', '', $package->getName());
}
}
19 changes: 15 additions & 4 deletions tests/Url/DrupalGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace IonBazan\ComposerDiff\Tests\Url;

use Composer\Package\CompletePackageInterface;
use Composer\Package\PackageInterface;
use IonBazan\ComposerDiff\Url\DrupalGenerator;

class DrupalGeneratorTest extends GeneratorTest
Expand All @@ -17,6 +19,10 @@ public function releaseUrlProvider()
$this->getPackageWithSource('drupal/webform', '6.0.0', 'https://git.drupalcode.org/project/webform.git', '6.0.0'),
'https://www.drupal.org/project/webform/releases/6.0.0',
),
'semver-semver-dist' => array(
$this->getPackageWithSourceAndDist('drupal/webform', '6.0.0', '6.0.0', 'https://git.drupalcode.org/project/webform.git'),
'https://www.drupal.org/project/webform/releases/6.0.0',
),
'core' => array(
$this->getPackageWithSource('drupal/core', '9.0.0', 'https://github.com/drupal/core.git', '9.0.0'),
'https://www.drupal.org/project/drupal/releases/9.0.0',
Expand Down Expand Up @@ -70,7 +76,12 @@ public function compareUrlProvider()
$this->getPackageWithSourceAndDist('drupal/color_field', '2.4.0', '8.x-2.4', 'https://git.drupalcode.org/project/color_field.git'),
$this->getPackageWithSourceAndDist('drupal/color_field', '2.5.0', '8.x-2.5', 'https://git.drupalcode.org/project/color_field.git'),
'https://git.drupalcode.org/project/color_field/compare/8.x-2.4...8.x-2.5',
),
),
'dev-version' => array(
$this->getPackageWithSourceAndDist('drupal/color_field', '2.4.0', '8.x-2.4', 'https://git.drupalcode.org/project/color_field.git'),
$this->getPackageWithSourceAndDist('drupal/color_field', 'dev-2.5.0', '8.x-2.5', 'https://git.drupalcode.org/project/color_field.git', 'd46283075d76ed244f7825b378eeb1cee246af73'),
'https://git.drupalcode.org/project/color_field/compare/8.x-2.4...d462830',
),
);
}

Expand All @@ -80,13 +91,13 @@ public function compareUrlProvider()
* @param string|null $sourceUrl
* @param string|null $sourceReference
*
* @return mixed
* @return PackageInterface
*/
protected function getPackageWithSourceAndDist($name, $version, $dist_version, $sourceUrl, $sourceReference = null)
protected function getPackageWithSourceAndDist($name, $version, $distVersion, $sourceUrl, $sourceReference = null)
{
$package = $this->getPackage($name, $version, $sourceReference);
$package->method('getSourceUrl')->willReturn($sourceUrl);
$package->method('getDistReference')->willReturn($dist_version);
$package->method('getDistReference')->willReturn($distVersion);
$package->method('getSourceReference')->willReturn($sourceReference);
$package->method('isDev')->willReturn(0 === strpos($version, 'dev-') || '-dev' === substr($version, -4));

Expand Down
3 changes: 3 additions & 0 deletions tests/Url/GeneratorContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function testGetsProperGenerator()
$this->assertNotSame($gitlabGenerator, $gitlab2Generator);
$this->assertNull($container->get($this->getPackageWithSource('', '', 'https://gitlab3.org')));
$this->assertNull($container->get($this->getPackageWithSource('', '', null)));
$drupalGenerator = $container->get($this->getPackageWithSource('', '', 'https://git.drupalcode.org'));
$this->assertInstanceOf('IonBazan\ComposerDiff\Url\DrupalGenerator', $drupalGenerator);
$this->assertNotSame($gitlabGenerator, $drupalGenerator);
}

public function testItSupportsPackageSupportedByOneOfTheGenerators()
Expand Down

0 comments on commit cca2afc

Please sign in to comment.