Skip to content

Commit

Permalink
add a couple of tests and changelog line
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Apr 14, 2022
1 parent d7b3418 commit a951725
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add `TracingDriverConnectionInterface::getNativeConnection()` method to get the original driver connection (#597)
- Add support for tracing of http client requests (#606)

## 4.2.6 (2022-01-10)

Expand Down
19 changes: 19 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -91,4 +91,23 @@
<code>PostResponseEvent</code>
</UndefinedClass>
</file>
<file src="src/DependencyInjection/Compiler/CacheTracingPass.php">
<UndefinedDocblockClass occurrences="1">
<code>$container-&gt;getParameter('sentry.tracing.cache.enabled')</code>
</UndefinedDocblockClass>
</file>
<file src="src/DependencyInjection/Compiler/HttpClientTracingPass.php">
<UndefinedDocblockClass occurrences="2">
<code>$container-&gt;getParameter('sentry.tracing.enabled')</code>
<code>$container-&gt;getParameter('sentry.tracing.http_client.enabled')</code>
</UndefinedDocblockClass>
</file>
<file src="src/DependencyInjection/Compiler/DbalTracingPass.php">
<UndefinedDocblockClass occurrences="4">
<code>$container-&gt;getParameter('sentry.tracing.enabled')</code>
<code>$container-&gt;getParameter('sentry.tracing.dbal.enabled')</code>
<code>$container-&gt;getParameter('sentry.tracing.dbal.connections')</code>
<code>$container-&gt;getParameter('doctrine.connections')</code>
</UndefinedDocblockClass>
</file>
</files>
73 changes: 73 additions & 0 deletions tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\DependencyInjection\Compiler\HttpClientTracingPass;
use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient;
use Sentry\State\HubAdapter;
use Sentry\State\HubInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class HttpClientTracingPassTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (!self::isHttpClientPackageInstalled()) {
self::markTestSkipped('This test requires the "symfony/http-client" Composer package to be installed.');
}
}

/**
* @param array<string, mixed> $params
*
* @dataProvider provideDisableContainerParameters
*/
public function testShouldNotDecorateHttpClientServicesIfDisabled(array $params): void
{
$container = new ContainerBuilder(new ParameterBag($params));
$container->register('http.client', HttpClient::class)
->setPublic(true)
->addTag('http_client.client');

$container->addCompilerPass(new HttpClientTracingPass());
$container->compile();

self::assertEquals(HttpClient::class, $container->getDefinition('http.client')->getClass());
}

/**
* @return iterable<mixed[]>
*/
public function provideDisableContainerParameters(): iterable
{
yield [['sentry.tracing.enabled' => true, 'sentry.tracing.http_client.enabled' => false]];
yield [['sentry.tracing.enabled' => false, 'sentry.tracing.http_client.enabled' => false]];
yield [['sentry.tracing.enabled' => false, 'sentry.tracing.http_client.enabled' => true]];
}

public function testShouldDecorateHttpClients(): void
{
$container = new ContainerBuilder(new ParameterBag(['sentry.tracing.enabled' => true, 'sentry.tracing.http_client.enabled' => true]));
$container->register(HubInterface::class)
->setFactory([HubAdapter::class, 'getInstance']);
$container->register('http.client', HttpClient::class)
->setPublic(true)
->addTag('http_client.client');

$container->addCompilerPass(new HttpClientTracingPass());
$container->compile();

self::assertEquals(TraceableHttpClient::class, $container->findDefinition('http.client')->getClass());
}

private static function isHttpClientPackageInstalled(): bool
{
return interface_exists(HttpClientInterface::class);
}
}
29 changes: 27 additions & 2 deletions tests/Tracing/HttpClient/TraceableHttpClientTest.php
Expand Up @@ -7,13 +7,16 @@
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\NullLogger;
use Sentry\SentryBundle\Tracing\HttpClient\AbstractTraceableResponse;
use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient;
use Sentry\State\HubInterface;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\Service\ResetInterface;

final class TraceableHttpClientTest extends TestCase
{
Expand All @@ -23,7 +26,7 @@ final class TraceableHttpClientTest extends TestCase
private $hub;

/**
* @var MockObject&HttpClientInterface
* @var MockObject&HttpClientInterface&LoggerAwareInterface&ResetInterface
*/
private $client;

Expand All @@ -42,7 +45,7 @@ public static function setUpBeforeClass(): void
protected function setUp(): void
{
$this->hub = $this->createMock(HubInterface::class);
$this->client = $this->createMock(HttpClientInterface::class);
$this->client = $this->createMock(TestableHttpClientInterface::class);
$this->httpClient = new TraceableHttpClient($this->client, $this->hub);
}

Expand Down Expand Up @@ -91,8 +94,30 @@ public function testRequest(): void
], $spans[1]->getTags());
}

public function testSetLoggerShouldBeForwardedToDecoratedInstance(): void
{
$logger = new NullLogger();
$this->client->expects($this->once())
->method('setLogger')
->with($logger);

$this->httpClient->setLogger($logger);
}

public function testResetCallShouldBeForwardedToDecoratedInstance(): void
{
$this->client->expects($this->once())
->method('reset');

$this->httpClient->reset();
}

private static function isHttpClientPackageInstalled(): bool
{
return interface_exists(HttpClientInterface::class);
}
}

interface TestableHttpClientInterface extends HttpClientInterface, LoggerAwareInterface, ResetInterface
{
}

0 comments on commit a951725

Please sign in to comment.