diff --git a/spec/Tracing/Propagation/Doctrine/TraceContextInfoProviderSpec.php b/spec/Tracing/Propagation/Doctrine/TraceContextInfoProviderSpec.php new file mode 100644 index 0000000..992fdec --- /dev/null +++ b/spec/Tracing/Propagation/Doctrine/TraceContextInfoProviderSpec.php @@ -0,0 +1,78 @@ + + */ + +namespace spec\Instrumentation\Tracing\Propagation\Doctrine; + +use Composer\InstalledVersions; +use Instrumentation\Tracing\Propagation\Doctrine\TraceContextInfoProviderInterface; +use OpenTelemetry\SDK\Common\Attribute\AttributesInterface; +use OpenTelemetry\SDK\Resource\ResourceInfo; +use OpenTelemetry\SemConv\ResourceAttributes; +use PhpSpec\ObjectBehavior; +use Symfony\Component\HttpFoundation\ParameterBag; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpKernel\Kernel; + +class TraceContextInfoProviderSpec extends ObjectBehavior +{ + public function let(ResourceInfo $info, AttributesInterface $attributes): void + { + $attributes->has(ResourceAttributes::SERVICE_NAME)->willReturn(false); + $info->getAttributes()->willReturn($attributes); + + $this->beConstructedWith($info); + } + + public function it_implements_interface(): void + { + $this->shouldBeAnInstanceOf(TraceContextInfoProviderInterface::class); + } + + public function it_gets_trace_context(): void + { + $this->getTraceContext()->shouldBeLike($this->getMinimalInfo()); + } + + public function it_gets_service_name(ResourceInfo $info, AttributesInterface $attributes, RequestStack $requestStack, Request $request, ParameterBag $requestParameters): void + { + $attributes->has(ResourceAttributes::SERVICE_NAME)->willReturn(true); + $attributes->get(ResourceAttributes::SERVICE_NAME)->willReturn('dummy-app'); + $info->getAttributes()->willReturn($attributes); + + $this->beConstructedWith($info); + + $this->getTraceContext()->shouldBeLike(array_merge($this->getMinimalInfo(), [ + 'application' => 'dummy-app', + ])); + } + + public function it_gets_controller_and_route(ResourceInfo $info, AttributesInterface $attributes, RequestStack $requestStack, Request $request, ParameterBag $requestParameters): void + { + $info->getAttributes()->willReturn($attributes); + + $this->beConstructedWith($info, null, $requestStack); + + $requestStack->getCurrentRequest()->willReturn($request); + $request->attributes = $requestParameters; + $requestParameters->get('_route')->willReturn('some_route'); + $requestParameters->get('_controller')->willReturn('Some\Controller'); + + $this->getTraceContext()->shouldBeLike(array_merge($this->getMinimalInfo(), [ + 'controller' => 'Some\\\\Controller', + 'route' => 'some_route', + ])); + } + + private function getMinimalInfo(): array + { + return [ + 'db_driver' => sprintf('doctrine/dbal-%s', InstalledVersions::getVersion('doctrine/dbal')), + 'framework' => sprintf('symfony-%s', Kernel::VERSION), + ]; + } +}