55namespace PhpList \WebFrontend \Tests \Unit \EventSubscriber ;
66
77use Exception ;
8- use GuzzleHttp \Exception \ClientException ;
9- use GuzzleHttp \Psr7 \Request as GuzzleRequest ;
10- use GuzzleHttp \Psr7 \Response as GuzzleResponse ;
8+ use PhpList \RestApiClient \Exception \AuthenticationException ;
119use PhpList \WebFrontend \EventSubscriber \UnauthorizedSubscriber ;
1210use PHPUnit \Framework \MockObject \MockObject ;
1311use PHPUnit \Framework \TestCase ;
12+ use Symfony \Component \HttpFoundation \JsonResponse ;
1413use Symfony \Component \HttpFoundation \RedirectResponse ;
1514use Symfony \Component \HttpFoundation \Request ;
15+ use Symfony \Component \HttpFoundation \Session \Flash \FlashBagInterface ;
1616use Symfony \Component \HttpFoundation \Session \SessionInterface ;
1717use Symfony \Component \HttpKernel \Event \ExceptionEvent ;
1818use Symfony \Component \HttpKernel \HttpKernelInterface ;
@@ -23,11 +23,13 @@ class UnauthorizedSubscriberTest extends TestCase
2323{
2424 private UnauthorizedSubscriber $ subscriber ;
2525 private UrlGeneratorInterface &MockObject $ urlGenerator ;
26+ private FlashBagInterface &MockObject $ flashBag ;
2627
2728 protected function setUp (): void
2829 {
2930 $ this ->urlGenerator = $ this ->createMock (UrlGeneratorInterface::class);
30- $ this ->subscriber = new UnauthorizedSubscriber ($ this ->urlGenerator );
31+ $ this ->flashBag = $ this ->createMock (FlashBagInterface::class);
32+ $ this ->subscriber = new UnauthorizedSubscriber ($ this ->urlGenerator , $ this ->flashBag );
3133 }
3234
3335 public function testGetSubscribedEvents (): void
@@ -40,33 +42,25 @@ public function testGetSubscribedEvents(): void
4042
4143 public function testOnKernelExceptionWithUnauthorizedException (): void
4244 {
43- $ guzzleRequest = new GuzzleRequest ('GET ' , 'http://example.com ' );
44- $ guzzleResponse = new GuzzleResponse (401 );
45- $ clientException = new ClientException ('Unauthorized ' , $ guzzleRequest , $ guzzleResponse );
45+ $ authException = new AuthenticationException ('Unauthorized ' );
4646
4747 $ session = $ this ->createMock (SessionInterface::class);
48- $ session ->expects ($ this ->once ())
49- ->method ('has ' )
50- ->with ('auth_token ' )
51- ->willReturn (true );
48+ $ session ->expects ($ this ->once ())->method ('invalidate ' );
5249
53- $ session ->expects ($ this ->once ())
54- ->method ('remove ' )
55- ->with ('auth_token ' );
56-
57- $ session ->expects ($ this ->once ())
58- ->method ('set ' )
59- ->with ('login_error ' , 'Your session has expired. Please log in again. ' );
50+ $ this ->flashBag ->expects ($ this ->once ())
51+ ->method ('add ' )
52+ ->with ('error ' , 'Your session has expired. Please log in again. ' );
6053
6154 $ request = $ this ->createMock (Request::class);
55+ $ request ->method ('hasSession ' )->willReturn (true );
6256 $ request ->method ('getSession ' )->willReturn ($ session );
6357
6458 $ kernel = $ this ->createMock (HttpKernelInterface::class);
6559 $ event = new ExceptionEvent (
6660 $ kernel ,
6761 $ request ,
6862 HttpKernelInterface::MAIN_REQUEST ,
69- $ clientException
63+ $ authException
7064 );
7165
7266 $ loginUrl = '/login ' ;
@@ -100,35 +94,55 @@ public function testOnKernelExceptionWithOtherException(): void
10094 $ this ->assertNull ($ event ->getResponse ());
10195 }
10296
103- public function testOnKernelExceptionWithNonAuthTokenSession (): void
97+ public function testOnKernelExceptionWithXmlHttpRequest (): void
10498 {
105- $ guzzleRequest = new GuzzleRequest ('GET ' , 'http://example.com ' );
106- $ guzzleResponse = new GuzzleResponse (401 );
107- $ clientException = new ClientException ('Unauthorized ' , $ guzzleRequest , $ guzzleResponse );
99+ $ authException = new AuthenticationException ('Unauthorized ' );
108100
109101 $ session = $ this ->createMock (SessionInterface::class);
110- $ session ->expects ($ this ->once ())
111- ->method ('has ' )
112- ->with ('auth_token ' )
113- ->willReturn (false );
102+ $ session ->expects ($ this ->once ())->method ('invalidate ' );
114103
115- $ session ->expects ($ this ->never ())
116- ->method ('remove ' )
117- ->with ('auth_token ' );
104+ $ request = $ this ->createMock (Request::class);
105+ $ request ->method ('hasSession ' )->willReturn (true );
106+ $ request ->method ('getSession ' )->willReturn ($ session );
107+ $ request ->method ('isXmlHttpRequest ' )->willReturn (true );
118108
119- $ session ->expects ($ this ->once ())
120- ->method ('set ' )
121- ->with ('login_error ' , 'Your session has expired. Please log in again. ' );
109+ $ kernel = $ this ->createMock (HttpKernelInterface::class);
110+ $ event = new ExceptionEvent (
111+ $ kernel ,
112+ $ request ,
113+ HttpKernelInterface::MAIN_REQUEST ,
114+ $ authException
115+ );
116+
117+ $ loginUrl = '/login ' ;
118+ $ this ->urlGenerator ->method ('generate ' )
119+ ->with ('login ' )
120+ ->willReturn ($ loginUrl );
121+
122+ $ this ->subscriber ->onKernelException ($ event );
123+
124+ $ response = $ event ->getResponse ();
125+ $ this ->assertInstanceOf (JsonResponse::class, $ response );
126+ $ this ->assertEquals (401 , $ response ->getStatusCode ());
127+
128+ $ data = json_decode ($ response ->getContent (), true );
129+ $ this ->assertEquals ('session_expired ' , $ data ['error ' ]);
130+ $ this ->assertEquals ($ loginUrl , $ data ['redirect ' ]);
131+ }
132+
133+ public function testOnKernelExceptionWithoutSession (): void
134+ {
135+ $ authException = new AuthenticationException ('Unauthorized ' );
122136
123137 $ request = $ this ->createMock (Request::class);
124- $ request ->method ('getSession ' )->willReturn ($ session );
138+ $ request ->method ('hasSession ' )->willReturn (false );
125139
126140 $ kernel = $ this ->createMock (HttpKernelInterface::class);
127141 $ event = new ExceptionEvent (
128142 $ kernel ,
129143 $ request ,
130144 HttpKernelInterface::MAIN_REQUEST ,
131- $ clientException
145+ $ authException
132146 );
133147
134148 $ loginUrl = '/login ' ;
0 commit comments