Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
31 changes: 7 additions & 24 deletions src/Integration/ModulesIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Comment thread
cleptric marked this conversation as resolved.
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'];
}
}
45 changes: 45 additions & 0 deletions tests/Integration/ModulesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
Loading