Skip to content
Advertisement

How to “mock” Sentry client in the PHPUnit tests

In case if you need to test PHP application error handlers, you have to “mock” or just disable sending errors on remote servers in the Sentry client. What is the right way to do this?

Advertisement

Answer

This is the example for Laravel, but this approach should work for any framework.

use SentryClient;
use SentryEventFactory;
use SentryOptions;
use SentrySerializerRepresentationSerializer;
use SentrySerializerSerializer;
use SentryTransportNullTransport;

private function mockSentry(): void
{
    /** @var SentryStateHub $sentry */
    $sentry = $this->app['sentry']; // Get sentry object from Laravel's container

    $client = new Client(
        new Options(),
        new NullTransport(),
        new EventFactory(
            new Serializer(new Options()),
            new RepresentationSerializer(new Options()),
            new Options(),
            '1',
            '1',
        ),
    );
    $sentry->bindClient($client);
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement