44
55namespace PhpList \WebFrontend \Tests \Unit \Controller ;
66
7+ use PhpList \RestApiClient \Entity \Administrator ;
78use PhpList \WebFrontend \Controller \AuthController ;
89use PhpList \RestApiClient \Endpoint \AuthClient ;
910use PHPUnit \Framework \MockObject \MockObject ;
1011use PHPUnit \Framework \TestCase ;
1112use RuntimeException ;
13+ use Symfony \Component \HttpFoundation \JsonResponse ;
1214use Symfony \Component \HttpFoundation \RedirectResponse ;
1315use Symfony \Component \HttpFoundation \Request ;
1416use Symfony \Component \HttpFoundation \Response ;
1517use Symfony \Component \HttpFoundation \Session \SessionInterface ;
1618
1719class AuthControllerTest extends TestCase
1820{
19- private AuthClient &MockObject $ apiClient ;
21+ private AuthClient &MockObject $ authClient ;
2022 private AuthController $ controller ;
2123
2224 protected function setUp (): void
2325 {
24- $ this ->apiClient = $ this ->createMock (AuthClient::class);
26+ $ this ->authClient = $ this ->createMock (AuthClient::class);
2527
2628 $ this ->controller = $ this ->getMockBuilder (AuthController::class)
27- ->setConstructorArgs ([$ this ->apiClient ])
29+ ->setConstructorArgs ([$ this ->authClient ])
2830 ->onlyMethods (['render ' , 'redirectToRoute ' , 'generateUrl ' ])
2931 ->getMock ();
3032
@@ -105,11 +107,12 @@ public function testLoginWithPostRequestSuccess(): void
105107 ['login_error ' , false ]
106108 ]);
107109
108- $ session ->expects ($ this ->exactly (2 ))
110+ $ session ->expects ($ this ->exactly (3 ))
109111 ->method ('set ' )
110112 ->withConsecutive (
111113 ['auth_token ' , 'test-token ' ],
112- ['auth_expiry_date ' , 'test-token ' ]
114+ ['auth_expiry_date ' , 'test-token ' ],
115+ ['auth_id ' , 1 ]
113116 );
114117
115118 $ request = Request::create ('/login ' , 'POST ' , [
@@ -118,9 +121,9 @@ public function testLoginWithPostRequestSuccess(): void
118121 ]);
119122 $ request ->setSession ($ session );
120123
121- $ this ->apiClient ->method ('login ' )
124+ $ this ->authClient ->method ('login ' )
122125 ->with ('testuser ' , 'testpass ' )
123- ->willReturn (['key ' => 'test-token ' ]);
126+ ->willReturn (['key ' => 'test-token ' , ' id ' => 1 ]);
124127
125128 $ response = $ this ->controller ->login ($ request );
126129
@@ -143,7 +146,7 @@ public function testLoginWithPostRequestFailure(): void
143146 ]);
144147 $ request ->setSession ($ session );
145148
146- $ this ->apiClient ->method ('login ' )
149+ $ this ->authClient ->method ('login ' )
147150 ->with ('testuser ' , 'testpass ' )
148151 ->willThrowException (new RuntimeException ('Invalid credentials ' ));
149152
@@ -178,9 +181,12 @@ public function testLoginWithExistingSession(): void
178181 public function testLogout (): void
179182 {
180183 $ session = $ this ->createMock (SessionInterface::class);
181- $ session ->expects ($ this ->once ( ))
184+ $ session ->expects ($ this ->exactly ( 2 ))
182185 ->method ('remove ' )
183- ->with ('auth_token ' );
186+ ->withConsecutive (
187+ ['auth_token ' ],
188+ ['auth_id ' ]
189+ );
184190
185191 $ request = $ this ->createMock (Request::class);
186192 $ request ->method ('getSession ' )
@@ -191,4 +197,29 @@ public function testLogout(): void
191197 $ this ->assertInstanceOf (RedirectResponse::class, $ response );
192198 $ this ->assertStringContainsString ('login ' , $ response ->getTargetUrl ());
193199 }
200+
201+ public function testAbout (): void
202+ {
203+ $ adminMock = $ this ->createMock (Administrator::class);
204+ $ adminMock ->method ('toArray ' )
205+ ->willReturn ([
206+ 'id ' => 123 ,
207+ 'login_name ' => 'testadmin ' ,
208+ 'email ' => 'admin@example.com ' ,
209+ 'super_user ' => true
210+ ]);
211+
212+ $ this ->authClient ->expects ($ this ->once ())
213+ ->method ('getSessionUser ' )
214+ ->willReturn ($ adminMock );
215+
216+ $ response = $ this ->controller ->about ();
217+
218+ $ this ->assertInstanceOf (JsonResponse::class, $ response );
219+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
220+ $ this ->assertEquals (
221+ '{"id":123,"login_name":"testadmin","email":"admin@example.com","super_user":true} ' ,
222+ $ response ->getContent ()
223+ );
224+ }
194225}
0 commit comments