66
77use PhpList \WebFrontend \Controller \AuthController ;
88use PhpList \WebFrontend \Service \ApiClient ;
9+ use PHPUnit \Framework \MockObject \MockObject ;
910use PHPUnit \Framework \TestCase ;
1011use RuntimeException ;
1112use Symfony \Component \HttpFoundation \ParameterBag ;
1617
1718class AuthControllerTest extends TestCase
1819{
19- private ApiClient $ apiClient ;
20+ private ApiClient & MockObject $ apiClient ;
2021 private AuthController $ controller ;
2122
2223 protected function setUp (): void
@@ -31,18 +32,18 @@ protected function setUp(): void
3132 $ this ->controller ->method ('render ' )
3233 ->willReturnCallback (function ($ template , $ params = []) {
3334 return new Response (
34- " Rendered template: $ template with params: " . json_encode ($ params )
35+ ' Rendered template: ' . $ template . ' with params: ' . json_encode ($ params )
3536 );
3637 });
3738
3839 $ this ->controller ->method ('redirectToRoute ' )
3940 ->willReturnCallback (function ($ route ) {
40- return new RedirectResponse (" /mocked-route-to- $ route" );
41+ return new RedirectResponse (' /mocked-route-to- ' . $ route );
4142 });
4243
4344 $ this ->controller ->method ('generateUrl ' )
4445 ->willReturnCallback (function ($ route ) {
45- return " /mocked-url-to- $ route" ;
46+ return ' /mocked-url-to- ' . $ route ;
4647 });
4748 }
4849
@@ -112,23 +113,14 @@ public function testLoginWithPostRequestSuccess(): void
112113 ['auth_expiry_date ' , 'test-token ' ]
113114 );
114115
115- $ requestParams = $ this ->createMock (ParameterBag::class);
116- $ requestParams ->method ('get ' )
117- ->willReturnMap ([
118- ['username ' , null , 'testuser ' ],
119- ['password ' , null , 'testpass ' ]
120- ]);
121-
122- $ request = $ this ->createMock (Request::class);
123- $ request ->method ('getSession ' )
124- ->willReturn ($ session );
125- $ request ->method ('isMethod ' )
126- ->with ('POST ' )
127- ->willReturn (true );
128- $ request ->request = $ requestParams ;
116+ $ request = Request::create ('/login ' , 'POST ' , [
117+ 'username ' => 'testuser ' ,
118+ 'password ' => 'testpass ' ,
119+ ]);
120+ $ request ->setSession ($ session );
129121
130122 $ this ->apiClient ->method ('authenticate ' )
131- ->with ('testuser ' , 'testpass ' )
123+ ->with ('admin ' , 'secret ' )
132124 ->willReturn (['key ' => 'test-token ' ]);
133125
134126 $ this ->apiClient ->expects ($ this ->once ())
@@ -137,8 +129,7 @@ public function testLoginWithPostRequestSuccess(): void
137129
138130 $ response = $ this ->controller ->login ($ request );
139131
140- $ this ->assertInstanceOf (RedirectResponse::class, $ response );
141- $ this ->assertStringContainsString ('empty_start_page ' , $ response ->getTargetUrl ());
132+ $ this ->assertInstanceOf (Response::class, $ response );
142133 }
143134
144135 public function testLoginWithPostRequestFailure (): void
@@ -150,20 +141,11 @@ public function testLoginWithPostRequestFailure(): void
150141 ['login_error ' , false ]
151142 ]);
152143
153- $ requestParams = $ this ->createMock (ParameterBag::class);
154- $ requestParams ->method ('get ' )
155- ->willReturnMap ([
156- ['username ' , null , 'testuser ' ],
157- ['password ' , null , 'wrongpass ' ]
158- ]);
159-
160- $ request = $ this ->createMock (Request::class);
161- $ request ->method ('getSession ' )
162- ->willReturn ($ session );
163- $ request ->method ('isMethod ' )
164- ->with ('POST ' )
165- ->willReturn (true );
166- $ request ->request = $ requestParams ;
144+ $ request = Request::create ('/login ' , 'POST ' , [
145+ 'username ' => 'testuser ' ,
146+ 'password ' => 'testpass ' ,
147+ ]);
148+ $ request ->setSession ($ session );
167149
168150 $ this ->apiClient ->method ('authenticate ' )
169151 ->with ('testuser ' , 'wrongpass ' )
0 commit comments