3535use Phauthentic \Authentication \UrlChecker \DefaultUrlChecker ;
3636use Phauthentic \Authentication \UrlChecker \UrlCheckerInterface ;
3737use Phauthentic \PasswordHasher \DefaultPasswordHasher ;
38+ use Psr \Http \Message \ResponseInterface ;
39+ use Psr \Http \Message \ServerRequestInterface ;
40+ use Psr \Http \Message \StreamInterface ;
41+ use Psr \Http \Message \UriInterface ;
3842use RuntimeException ;
3943use Zend \Diactoros \Response ;
40- use Zend \Diactoros \ServerRequestFactory ;
4144
45+ /**
46+ * Authentication Service Test
47+ */
4248class AuthenticationServiceTest extends TestCase
4349{
50+ /**
51+ * Create a new PasswordIdentifier instance
52+ *
53+ * @return \Phauthentic\Authentication\Identifier\PasswordIdentifier
54+ */
4455 protected function createPasswordIdentifier ()
4556 {
4657 $ resolver = new TestResolver ($ this ->getConnection ()->getConnection ());
@@ -49,6 +60,11 @@ protected function createPasswordIdentifier()
4960 return new PasswordIdentifier ($ resolver , $ passwordHasher );
5061 }
5162
63+ /**
64+ * Create a new SessionAuthenticator instance
65+ *
66+ * @return \Phauthentic\Authentication\Authenticator\SessionAuthenticator
67+ */
5268 protected function createSessionAuthenticator (IdentifierInterface $ identifier = null , StorageInterface $ storage = null )
5369 {
5470 if (!$ identifier ) {
@@ -61,6 +77,11 @@ protected function createSessionAuthenticator(IdentifierInterface $identifier =
6177 return new SessionAuthenticator ($ identifier , $ storage );
6278 }
6379
80+ /**
81+ * Create a new FormAuthenticator instance
82+ *
83+ * @return \Phauthentic\Authentication\Authenticator\FormAuthenticator
84+ */
6485 protected function createFormAuthenticator (IdentifierInterface $ identifier = null , UrlCheckerInterface $ urlChecker = null )
6586 {
6687 if (!$ identifier ) {
@@ -74,6 +95,13 @@ protected function createFormAuthenticator(IdentifierInterface $identifier = nul
7495 return new FormAuthenticator ($ identifier , $ urlChecker );
7596 }
7697
98+ /**
99+ * Create Authenticators
100+ *
101+ * @param \Phauthentic\Authentication\Identifier\IdentifierInterface
102+ * @param \Phauthentic\Authentication\Authenticator\Storage\StorageInterface
103+ * @return \Phauthentic\Authentication\Authenticator\AuthenticatorCollectionInterface
104+ */
77105 protected function createAuthenticators (IdentifierInterface $ identifier = null , StorageInterface $ storage = null )
78106 {
79107 $ authenticators = new AuthenticatorCollection ();
@@ -87,16 +115,56 @@ protected function createAuthenticators(IdentifierInterface $identifier = null,
87115 return $ authenticators ;
88116 }
89117
118+ /**
119+ * Gets a mocked request
120+ *
121+ * @param string $path Path
122+ * @param array $body Parsed body data as array
123+ * @param array §server Server environment
124+ * @return mixed
125+ */
126+ protected function getMockRequest ($ path , $ body , $ server = [])
127+ {
128+ $ request = $ this ->getMockBuilder (ServerRequestInterface::class)
129+ ->getMock ();
130+
131+ $ uri = $ this ->getMockBuilder (UriInterface::class)
132+ ->getMock ();
133+
134+ $ uri ->expects ($ this ->any ())
135+ ->method ('getPath ' )
136+ ->willReturn ($ path );
137+
138+ $ uri ->expects ($ this ->any ())
139+ ->method ('__toString ' )
140+ ->willReturn ('http://localhost ' . $ path );
141+
142+ $ request ->expects ($ this ->any ())
143+ ->method ('getUri ' )
144+ ->willReturn ($ uri );
145+
146+ $ request ->expects ($ this ->any ())
147+ ->method ('getParsedBody ' )
148+ ->willReturn ($ body );
149+
150+ if (!empty ($ server )) {
151+ $ request ->expects ($ this ->any ())
152+ ->method ('getServerParams ' )
153+ ->willReturn ($ server );
154+ }
155+
156+ return $ request ;
157+ }
158+
90159 /**
91160 * testAuthenticate
92161 *
93162 * @return void
94163 */
95- public function testAuthenticate ()
164+ public function testAuthenticate (): void
96165 {
97- $ request = ServerRequestFactory::fromGlobals (
98- ['REQUEST_URI ' => '/testpath ' ],
99- [],
166+ $ request = $ this ->getMockRequest (
167+ '/testpath ' ,
100168 ['username ' => 'robert ' , 'password ' => 'robert ' ]
101169 );
102170
@@ -130,11 +198,10 @@ public function testAuthenticate()
130198 *
131199 * @return void
132200 */
133- public function testAuthenticateFailure ()
201+ public function testAuthenticateFailure (): void
134202 {
135- $ request = ServerRequestFactory::fromGlobals (
136- ['REQUEST_URI ' => '/testpath ' ],
137- [],
203+ $ request = $ this ->getMockRequest (
204+ '/testpath ' ,
138205 ['username ' => 'robert ' , 'password ' => 'invalid ' ]
139206 );
140207
@@ -174,10 +241,11 @@ public function testAuthenticateFailure()
174241 *
175242 * @return void
176243 */
177- public function testAuthenticateStorage ()
244+ public function testAuthenticateStorage (): void
178245 {
179- $ request = ServerRequestFactory::fromGlobals (
180- ['REQUEST_URI ' => '/testpath ' ]
246+ $ request = $ this ->getMockRequest (
247+ '/testpath ' ,
248+ []
181249 );
182250
183251 $ storage = $ this ->createMock (StorageInterface::class);
@@ -221,14 +289,18 @@ public function testAuthenticateStorage()
221289 *
222290 * @return void
223291 */
224- public function testAuthenticateWithChallenge ()
292+ public function testAuthenticateWithChallenge (): void
225293 {
226- $ request = ServerRequestFactory::fromGlobals ([
227- 'SERVER_NAME ' => 'example.com ' ,
228- 'REQUEST_URI ' => '/testpath ' ,
229- 'PHP_AUTH_USER ' => 'robert ' ,
230- 'PHP_AUTH_PW ' => 'WRONG '
231- ]);
294+ $ request = $ this ->getMockRequest (
295+ '/testpath ' ,
296+ [],
297+ [
298+ 'SERVER_NAME ' => 'example.com ' ,
299+ 'REQUEST_URI ' => '/testpath ' ,
300+ 'PHP_AUTH_USER ' => 'robert ' ,
301+ 'PHP_AUTH_PW ' => 'WRONG '
302+ ]
303+ );
232304
233305 $ identifier = $ this ->createPasswordIdentifier ();
234306 $ authenticators = new AuthenticatorCollection ([
@@ -247,13 +319,13 @@ public function testAuthenticateWithChallenge()
247319 *
248320 * @return void
249321 */
250- public function testPersistAuthenticatedIdentity ()
322+ public function testPersistAuthenticatedIdentity (): void
251323 {
252- $ request = ServerRequestFactory::fromGlobals (
253- ['REQUEST_URI ' => '/testpath ' ],
254- [],
324+ $ request = $ this ->getMockRequest (
325+ '/testpath ' ,
255326 ['username ' => 'robert ' , 'password ' => 'robert ' ]
256327 );
328+
257329 $ response = new Response ();
258330
259331 $ storage = $ this ->createMock (StorageInterface::class);
@@ -286,13 +358,13 @@ public function testPersistAuthenticatedIdentity()
286358 *
287359 * @return void
288360 */
289- public function testPersistCustomIdentity ()
361+ public function testPersistCustomIdentity (): void
290362 {
291- $ request = ServerRequestFactory::fromGlobals (
292- ['REQUEST_URI ' => '/testpath ' ],
293- [],
363+ $ request = $ this ->getMockRequest (
364+ '/testpath ' ,
294365 ['username ' => 'robert ' , 'password ' => 'robert ' ]
295366 );
367+
296368 $ response = new Response ();
297369
298370 $ storage = $ this ->createMock (StorageInterface::class);
@@ -318,11 +390,10 @@ public function testPersistCustomIdentity()
318390 *
319391 * @return void
320392 */
321- public function testClearIdentity ()
393+ public function testClearIdentity (): void
322394 {
323- $ request = ServerRequestFactory::fromGlobals (
324- ['REQUEST_URI ' => '/testpath ' ],
325- [],
395+ $ request = $ this ->getMockRequest (
396+ '/testpath ' ,
326397 ['username ' => 'robert ' , 'password ' => 'robert ' ]
327398 );
328399 $ response = new Response ();
@@ -352,11 +423,10 @@ public function testClearIdentity()
352423 *
353424 * @return void
354425 */
355- public function testNoAuthenticatorsLoadedException ()
426+ public function testNoAuthenticatorsLoadedException (): void
356427 {
357- $ request = ServerRequestFactory::fromGlobals (
358- ['REQUEST_URI ' => '/testpath ' ],
359- [],
428+ $ request = $ this ->getMockRequest (
429+ '/testpath ' ,
360430 ['username ' => 'robert ' , 'password ' => 'robert ' ]
361431 );
362432
@@ -373,7 +443,7 @@ public function testNoAuthenticatorsLoadedException()
373443 *
374444 * @return void
375445 */
376- public function testBuildIdentity ()
446+ public function testBuildIdentity (): void
377447 {
378448 $ data = new ArrayObject (['username ' => 'robert ' ]);
379449 $ identity = new Identity ($ data );
@@ -390,11 +460,10 @@ public function testBuildIdentity()
390460 $ this ->assertSame ($ identity , $ result );
391461 }
392462
393- public function testGetIdentity ()
463+ public function testGetIdentity (): void
394464 {
395- $ request = ServerRequestFactory::fromGlobals (
396- ['REQUEST_URI ' => '/testpath ' ],
397- [],
465+ $ request = $ this ->getMockRequest (
466+ '/testpath ' ,
398467 ['username ' => 'robert ' , 'password ' => 'robert ' ]
399468 );
400469
0 commit comments