diff --git a/composer.json b/composer.json index 0be4a6531e..00fb1c5d48 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,6 @@ "ext-mbstring": "*", "ext-curl": "*", "guzzlehttp/psr7": "^1.8.4|^2.1.1", - "jean85/pretty-package-versions": "^1.5|^2.0.4", "psr/log": "^1.0|^2.0|^3.0", "symfony/options-resolver": "^4.4.30|^5.0.11|^6.0|^7.0" }, diff --git a/src/Integration/ModulesIntegration.php b/src/Integration/ModulesIntegration.php index 860d90151b..affe1e062b 100644 --- a/src/Integration/ModulesIntegration.php +++ b/src/Integration/ModulesIntegration.php @@ -5,8 +5,6 @@ namespace Sentry\Integration; use Composer\InstalledVersions; -use Jean85\PrettyVersions; -use PackageVersions\Versions; use Sentry\Event; use Sentry\SentrySdk; use Sentry\State\Scope; @@ -46,33 +44,18 @@ public function setupOnce(): void private static function getComposerPackages(): array { if (empty(self::$packages)) { - foreach (self::getInstalledPackages() as $package) { + foreach (InstalledVersions::getInstalledPackages() as $package) { try { - self::$packages[$package] = PrettyVersions::getVersion($package)->getPrettyVersion(); - } catch (\Throwable $exception) { - continue; + $version = InstalledVersions::getPrettyVersion($package); + if ($version !== null) { + self::$packages[$package] = $version; + } + } catch (\Throwable $t) { + // Ignore it in case of any error } } } return self::$packages; } - - /** - * @return string[] - */ - private static function getInstalledPackages(): array - { - if (class_exists(InstalledVersions::class)) { - return InstalledVersions::getInstalledPackages(); - } - - if (class_exists(Versions::class)) { - // BC layer for Composer 1, using a transient dependency - return array_keys(Versions::VERSIONS); - } - - // this should not happen - return ['sentry/sentry']; - } } diff --git a/tests/Integration/ModulesIntegrationTest.php b/tests/Integration/ModulesIntegrationTest.php index 4fcd4dd02a..52b96c9d36 100644 --- a/tests/Integration/ModulesIntegrationTest.php +++ b/tests/Integration/ModulesIntegrationTest.php @@ -6,11 +6,15 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Sentry\ClientBuilder; use Sentry\ClientInterface; use Sentry\Event; use Sentry\Integration\ModulesIntegration; use Sentry\SentrySdk; use Sentry\State\Scope; +use Sentry\Transport\Result; +use Sentry\Transport\ResultStatus; +use Sentry\Transport\TransportInterface; use function Sentry\withScope; @@ -57,4 +61,45 @@ public static function invokeDataProvider(): \Generator false, ]; } + + public function testModuleIntegration(): void + { + /** @var TransportInterface&MockObject $transport */ + $transport = $this->createMock(TransportInterface::class); + $transport->expects($this->once()) + ->method('send') + ->with($this->callback(function (Event $event): bool { + $modules = $event->getModules(); + + $this->assertIsArray($modules); + $this->assertNotEmpty($modules); + $this->assertArrayHasKey('psr/log', $modules); + $this->assertMatchesRegularExpression("/^\d+\.\d+\.\d+$/", $modules['psr/log']); + + return true; + })) + ->willReturnCallback(static function (Event $event): Result { + return new Result(ResultStatus::success(), $event); + }); + + $client = ClientBuilder::create() + ->setTransport($transport) + ->getClient(); + + SentrySdk::getCurrentHub()->bindClient($client); + + $client->captureEvent(Event::createEvent(), null, new Scope()); + } + + /** + * Polyfill for older phpunit versions. + */ + public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void + { + if (method_exists(parent::class, 'assertMatchesRegularExpression')) { + parent::assertMatchesRegularExpression($pattern, $string, $message); + } else { + static::assertRegExp($pattern, $string, $message); + } + } }