|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\SentryBundle\Test\EventListener; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Sentry\SentryBundle\EventListener\SubRequestListener; |
| 7 | +use Sentry\State\Hub; |
| 8 | +use Sentry\State\HubInterface; |
| 9 | +use Sentry\State\Scope; |
| 10 | +use Symfony\Component\HttpKernel\Event\FinishRequestEvent; |
| 11 | +use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
| 12 | + |
| 13 | +class SubRequestListenerTest extends TestCase |
| 14 | +{ |
| 15 | + private $currentHub; |
| 16 | + |
| 17 | + protected function setUp() |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + $this->currentHub = $this->prophesize(HubInterface::class); |
| 22 | + |
| 23 | + Hub::setCurrent($this->currentHub->reveal()); |
| 24 | + } |
| 25 | + |
| 26 | + public function testOnKernelRequestWithMasterRequest(): void |
| 27 | + { |
| 28 | + $listener = new SubRequestListener(); |
| 29 | + |
| 30 | + $subRequestEvent = $this->prophesize(GetResponseEvent::class); |
| 31 | + $subRequestEvent->isMasterRequest() |
| 32 | + ->willReturn(true); |
| 33 | + |
| 34 | + $this->currentHub->pushScope() |
| 35 | + ->shouldNotBeCalled(); |
| 36 | + |
| 37 | + $listener->onKernelRequest($subRequestEvent->reveal()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testOnKernelRequestWithSubRequest(): void |
| 41 | + { |
| 42 | + $listener = new SubRequestListener(); |
| 43 | + |
| 44 | + $subRequestEvent = $this->prophesize(GetResponseEvent::class); |
| 45 | + $subRequestEvent->isMasterRequest() |
| 46 | + ->willReturn(false); |
| 47 | + |
| 48 | + $this->currentHub->pushScope() |
| 49 | + ->shouldBeCalledTimes(1) |
| 50 | + ->willReturn(new Scope()); |
| 51 | + |
| 52 | + $listener->onKernelRequest($subRequestEvent->reveal()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testOnKernelFinishRequestWithMasterRequest(): void |
| 56 | + { |
| 57 | + $listener = new SubRequestListener(); |
| 58 | + |
| 59 | + $subRequestEvent = $this->prophesize(FinishRequestEvent::class); |
| 60 | + $subRequestEvent->isMasterRequest() |
| 61 | + ->willReturn(true); |
| 62 | + |
| 63 | + $this->currentHub->popScope() |
| 64 | + ->shouldNotBeCalled(); |
| 65 | + |
| 66 | + $listener->onKernelFinishRequest($subRequestEvent->reveal()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testOnKernelFinishRequestWithSubRequest(): void |
| 70 | + { |
| 71 | + $listener = new SubRequestListener(); |
| 72 | + |
| 73 | + $subRequestEvent = $this->prophesize(FinishRequestEvent::class); |
| 74 | + $subRequestEvent->isMasterRequest() |
| 75 | + ->willReturn(false); |
| 76 | + |
| 77 | + $this->currentHub->popScope() |
| 78 | + ->shouldBeCalledTimes(1) |
| 79 | + ->willReturn(true); |
| 80 | + |
| 81 | + $listener->onKernelFinishRequest($subRequestEvent->reveal()); |
| 82 | + } |
| 83 | +} |
0 commit comments