From e53f943ad8fd6351b431dacdffbfc1fdd649acc8 Mon Sep 17 00:00:00 2001 From: Simon Berger Date: Tue, 14 Mar 2023 18:18:36 +0100 Subject: [PATCH 1/2] #700 Added a failing test --- .../Compiler/HttpClientTracingPassTest.php | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php b/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php index 9f3d12a7..9c212478 100644 --- a/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php +++ b/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php @@ -6,10 +6,14 @@ use PHPUnit\Framework\TestCase; use Sentry\SentryBundle\DependencyInjection\Compiler\HttpClientTracingPass; +use Sentry\SentryBundle\Tracing\HttpClient\AbstractTraceableHttpClient; use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient; +use Sentry\State\Hub; use Sentry\State\HubInterface; +use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpClient\HttpClient; +use Symfony\Component\HttpClient\ScopingHttpClient; use Symfony\Contracts\HttpClient\HttpClientInterface; final class HttpClientTracingPassTest extends TestCase @@ -29,6 +33,53 @@ public function testProcess(): void $this->assertSame(TraceableHttpClient::class, $container->findDefinition('http.client')->getClass()); } + public function testScopedClients(): void + { + $container = $this->createContainerBuilder(true, true); + + $container->setParameter('kernel.debug', false); + $container->setParameter('kernel.project_dir', ''); + $container->setParameter('kernel.container_class', ''); + $container->setParameter('kernel.build_dir', ''); + $container->setParameter('kernel.charset', ''); + $container->setParameter('kernel.cache_dir', ''); + $container->setParameter('kernel.logs_dir', ''); + $container->setParameter('kernel.runtime_environment', 'dev'); + + $frameworkExtension = new FrameworkExtension(); + $frameworkExtension->load( + [ + 'framework' => [ + 'http_client' => [ + 'scoped_clients' => [ + 'scoped.http.client' => [ + 'base_uri' => 'https://example.com', + ], + ], + ], + ], + ], + $container + ); + + $container->getDefinition('scoped.http.client')->setPublic(true); + $container->compile(); + + $service = $container->get('scoped.http.client'); + $this->assertInstanceOf(AbstractTraceableHttpClient::class, $service); + + $reflection = new \ReflectionProperty(AbstractTraceableHttpClient::class, 'client'); + $reflection->setAccessible(true); + $parentClient = $reflection->getValue($service); + $this->assertInstanceOf(ScopingHttpClient::class, $parentClient); + + $reflection = new \ReflectionProperty(\get_class($parentClient), 'client'); + $reflection->setAccessible(true); + $thirdClient = $reflection->getValue($service); + // Failing check + $this->assertNotInstanceOf(AbstractTraceableHttpClient::class, $thirdClient); + } + /** * @dataProvider processDoesNothingIfConditionsForEnablingTracingAreMissingDataProvider */ @@ -68,7 +119,7 @@ private function createContainerBuilder(bool $isTracingEnabled, bool $isHttpClie $container->setParameter('sentry.tracing.enabled', $isTracingEnabled); $container->setParameter('sentry.tracing.http_client.enabled', $isHttpClientTracingEnabled); - $container->register(HubInterface::class, HubInterface::class) + $container->register(HubInterface::class, Hub::class) ->setPublic(true); $container->register('http.client', HttpClient::class) From 6b817abbd1598f6f837c8d4b269e77477a229723 Mon Sep 17 00:00:00 2001 From: Simon Berger Date: Tue, 14 Mar 2023 18:28:16 +0100 Subject: [PATCH 2/2] Fixed test --- .../Compiler/HttpClientTracingPassTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php b/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php index 9c212478..30ec32c1 100644 --- a/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php +++ b/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php @@ -75,9 +75,11 @@ public function testScopedClients(): void $reflection = new \ReflectionProperty(\get_class($parentClient), 'client'); $reflection->setAccessible(true); - $thirdClient = $reflection->getValue($service); + $thirdClient = $reflection->getValue($parentClient); // Failing check $this->assertNotInstanceOf(AbstractTraceableHttpClient::class, $thirdClient); + + // $this->assertInstanceOf(CurlHttpClient::class, $thirdClient); } /**