From 844bd110a84340adcf28cd107e359ae3161cac19 Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Thu, 7 Nov 2024 11:42:02 +0800 Subject: [PATCH] Fix compatibility issues and add test --- src/Url/WordPressGenerator.php | 44 ++++++---------------------- tests/Url/WordPressGeneratorTest.php | 10 +++++++ 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/Url/WordPressGenerator.php b/src/Url/WordPressGenerator.php index 975338e..8fc1691 100644 --- a/src/Url/WordPressGenerator.php +++ b/src/Url/WordPressGenerator.php @@ -7,19 +7,15 @@ class WordPressGenerator implements UrlGenerator { /** - * Determines if the generator supports the given package. - * - * @return bool + * {@inheritdoc} */ public function supportsPackage(PackageInterface $package) { - return 0 === strpos($package->getName(), 'wpackagist-plugin/') || 0 === strpos($package->getName(), 'wpackagist-theme/'); + return (bool) preg_match('#^wpackagist-(plugin|theme)/#', $package->getName()); } /** - * Generates a compare URL for two versions of the same package. - * - * @return string|null + * {@inheritdoc} */ public function getCompareUrl(PackageInterface $initialPackage, PackageInterface $targetPackage) { @@ -27,9 +23,7 @@ public function getCompareUrl(PackageInterface $initialPackage, PackageInterface } /** - * Generates URL for viewing a release or commit of a package. - * - * @return string|null + * {@inheritdoc} */ public function getReleaseUrl(PackageInterface $package) { @@ -37,38 +31,18 @@ public function getReleaseUrl(PackageInterface $package) } /** - * Generates URL for viewing the project page of a package (usually repository root). - * - * @return string|null + * {@inheritdoc} */ public function getProjectUrl(PackageInterface $package) { - $type = $this->getPackageType($package); + preg_match('#wpackagist-(plugin|theme)/(.+)#', $package->getName(), $matches); - if (null === $type) { + if (empty($matches)) { return null; } - return sprintf('https://wordpress.org/%ss/%s', $type, $this->getPackageSlug($package)); - } - - /** - * @return string|null - */ - protected function getPackageType(PackageInterface $package) - { - [$type] = explode('/', $package->getName(), 2); - - return 0 === strpos($type, 'wpackagist-') ? substr($type, 11) : null; - } - - /** - * @return string - */ - protected function getPackageSlug(PackageInterface $package) - { - [, $slug] = explode('/', $package->getName(), 2); + list (, $type, $slug) = $matches; - return $slug; + return sprintf('https://wordpress.org/%ss/%s', $type, $slug); } } diff --git a/tests/Url/WordPressGeneratorTest.php b/tests/Url/WordPressGeneratorTest.php index 8d352a7..5d31f06 100644 --- a/tests/Url/WordPressGeneratorTest.php +++ b/tests/Url/WordPressGeneratorTest.php @@ -6,6 +6,16 @@ class WordPressGeneratorTest extends GeneratorTest { + public function testItSupportsOnlyWpackagistPackages() + { + $generator = $this->getGenerator(); + + $this->assertFalse($generator->supportsPackage($this->getPackage('acme/package', '3.12.1'))); + $this->assertTrue($generator->supportsPackage($this->getPackage('wpackagist-plugin/my-plugin', '3.12.1'))); + $this->assertTrue($generator->supportsPackage($this->getPackage('wpackagist-theme/my-theme', '3.12.1'))); + $this->assertFalse($generator->supportsPackage($this->getPackage('acme-wpackagist-theme/my-theme', '3.12.1'))); + } + public function releaseUrlProvider() { return array(