44
55use PHPUnit \Framework \TestCase ;
66use Prophecy \Argument ;
7+ use Sentry \ClientInterface ;
8+ use Sentry \Options ;
79use Sentry \SentryBundle \EventListener \RequestListener ;
810use Sentry \State \Hub ;
911use Sentry \State \HubInterface ;
@@ -21,13 +23,24 @@ class RequestListenerTest extends TestCase
2123{
2224 private $ currentScope ;
2325 private $ currentHub ;
26+ private $ options ;
2427
2528 protected function setUp ()
2629 {
2730 parent ::setUp ();
2831
2932 $ this ->currentScope = $ scope = new Scope ();
3033 $ this ->currentHub = $ this ->prophesize (HubInterface::class);
34+
35+ $ client = $ this ->prophesize (ClientInterface::class);
36+ $ this ->options = new Options ();
37+
38+ $ this ->currentHub ->getClient ()
39+ ->willReturn ($ client ->reveal ());
40+ $ client ->getOptions ()
41+ ->willReturn ($ this ->options );
42+ $ this ->options ->setSendDefaultPii (true );
43+
3144 $ this ->currentHub ->configureScope (Argument::type ('callable ' ))
3245 ->shouldBeCalled ()
3346 ->will (function ($ arguments ) use ($ scope ): void {
@@ -96,6 +109,56 @@ public function userDataProvider(): \Generator
96109 yield [new ToStringUser ('john-doe ' )];
97110 }
98111
112+ public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled (): void
113+ {
114+ $ tokenStorage = $ this ->prophesize (TokenStorageInterface::class);
115+ $ authorizationChecker = $ this ->prophesize (AuthorizationCheckerInterface::class);
116+ $ event = $ this ->prophesize (GetResponseEvent::class);
117+
118+ $ event ->isMasterRequest ()
119+ ->willReturn (true );
120+
121+ $ this ->options ->setSendDefaultPii (false );
122+
123+ $ this ->currentHub ->configureScope (Argument::type ('callable ' ))
124+ ->shouldNotBeCalled ();
125+
126+ $ listener = new RequestListener (
127+ $ this ->currentHub ->reveal (),
128+ $ tokenStorage ->reveal (),
129+ $ authorizationChecker ->reveal ()
130+ );
131+
132+ $ listener ->onKernelRequest ($ event ->reveal ());
133+
134+ $ this ->assertEquals ([], $ this ->currentScope ->getUser ());
135+ }
136+
137+ public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent (): void
138+ {
139+ $ tokenStorage = $ this ->prophesize (TokenStorageInterface::class);
140+ $ authorizationChecker = $ this ->prophesize (AuthorizationCheckerInterface::class);
141+ $ event = $ this ->prophesize (GetResponseEvent::class);
142+
143+ $ event ->isMasterRequest ()
144+ ->willReturn (true );
145+
146+ $ this ->currentHub ->getClient ()
147+ ->willReturn (null );
148+ $ this ->currentHub ->configureScope (Argument::type ('callable ' ))
149+ ->shouldNotBeCalled ();
150+
151+ $ listener = new RequestListener (
152+ $ this ->currentHub ->reveal (),
153+ $ tokenStorage ->reveal (),
154+ $ authorizationChecker ->reveal ()
155+ );
156+
157+ $ listener ->onKernelRequest ($ event ->reveal ());
158+
159+ $ this ->assertEquals ([], $ this ->currentScope ->getUser ());
160+ }
161+
99162 public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent (): void
100163 {
101164 $ authorizationChecker = $ this ->prophesize (AuthorizationCheckerInterface::class);
0 commit comments