1818use League \OAuth2 \Server \Repositories \RefreshTokenRepositoryInterface ;
1919use League \OAuth2 \Server \Repositories \ScopeRepositoryInterface ;
2020use League \OAuth2 \Server \RequestTypes \AuthorizationRequest ;
21- use League \OAuth2 \Server \ResponseTypes \ResponseTypeInterface ;
2221use LeagueTests \Stubs \AccessTokenEntity ;
2322use LeagueTests \Stubs \AuthCodeEntity ;
2423use LeagueTests \Stubs \ClientEntity ;
2524use LeagueTests \Stubs \RefreshTokenEntity ;
2625use LeagueTests \Stubs \ScopeEntity ;
2726use LogicException ;
2827use PHPUnit \Framework \TestCase ;
29- use Psr \Http \Message \ServerRequestInterface ;
3028use ReflectionClass ;
3129
3230use function base64_encode ;
@@ -35,7 +33,9 @@ class AbstractGrantTest extends TestCase
3533{
3634 public function testHttpBasicWithPassword (): void
3735 {
38- $ grantMock = $ this ->getAbstractGrantStub ();
36+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
37+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
38+ ->getMock ();
3939 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
4040
4141 $ serverRequest = (new ServerRequest ())->withHeader ('Authorization ' , 'Basic ' . base64_encode ('Open:Sesame ' ));
@@ -47,7 +47,9 @@ public function testHttpBasicWithPassword(): void
4747
4848 public function testHttpBasicNoPassword (): void
4949 {
50- $ grantMock = $ this ->getAbstractGrantStub ();
50+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
51+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
52+ ->getMock ();
5153 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
5254
5355 $ serverRequest = (new ServerRequest ())->withHeader ('Authorization ' , 'Basic ' . base64_encode ('Open: ' ));
@@ -59,7 +61,9 @@ public function testHttpBasicNoPassword(): void
5961
6062 public function testHttpBasicNotBasic (): void
6163 {
62- $ grantMock = $ this ->getAbstractGrantStub ();
64+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
65+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
66+ ->getMock ();
6367 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
6468
6569 $ serverRequest = (new ServerRequest ())->withHeader ('Authorization ' , 'Foo ' . base64_encode ('Open:Sesame ' ));
@@ -71,7 +75,9 @@ public function testHttpBasicNotBasic(): void
7175
7276 public function testHttpBasicCaseInsensitive (): void
7377 {
74- $ grantMock = $ this ->getAbstractGrantStub ();
78+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
79+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
80+ ->getMock ();
7581 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
7682
7783 $ serverRequest = (new ServerRequest ())->withHeader ('Authorization ' , 'bAsIc ' . base64_encode ('Open:Sesame ' ));
@@ -83,7 +89,9 @@ public function testHttpBasicCaseInsensitive(): void
8389
8490 public function testHttpBasicNotBase64 (): void
8591 {
86- $ grantMock = $ this ->getAbstractGrantStub ();
92+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
93+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
94+ ->getMock ();
8795 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
8896
8997 $ serverRequest = (new ServerRequest ())->withHeader ('Authorization ' , 'Basic || ' );
@@ -95,7 +103,9 @@ public function testHttpBasicNotBase64(): void
95103
96104 public function testHttpBasicNoColon (): void
97105 {
98- $ grantMock = $ this ->getAbstractGrantStub ();
106+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
107+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
108+ ->getMock ();
99109 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
100110
101111 $ serverRequest = (new ServerRequest ())->withHeader ('Authorization ' , 'Basic ' . base64_encode ('OpenSesame ' ));
@@ -109,7 +119,9 @@ public function testGetClientCredentialsClientSecretNotAString(): void
109119 {
110120 $ clientRepositoryMock = $ this ->getMockBuilder (ClientRepositoryInterface::class)->getMock ();
111121
112- $ grantMock = $ this ->getAbstractGrantStub ();
122+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
123+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
124+ ->getMock ();
113125 $ grantMock ->setClientRepository ($ clientRepositoryMock );
114126
115127 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -146,7 +158,9 @@ public function testValidateClientPublic(): void
146158 $ clientRepositoryMock ->method ('getClientEntity ' )->willReturn ($ client );
147159 $ clientRepositoryMock ->method ('validateClient ' )->willReturn (true );
148160
149- $ grantMock = $ this ->getAbstractGrantStub ();
161+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
162+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
163+ ->getMock ();
150164 $ grantMock ->setClientRepository ($ clientRepositoryMock );
151165
152166 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -172,7 +186,9 @@ public function testValidateClientConfidential(): void
172186 $ clientRepositoryMock ->method ('getClientEntity ' )->willReturn ($ client );
173187 $ clientRepositoryMock ->method ('validateClient ' )->willReturn (true );
174188
175- $ grantMock = $ this ->getAbstractGrantStub ();
189+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
190+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
191+ ->getMock ();
176192 $ grantMock ->setClientRepository ($ clientRepositoryMock );
177193
178194 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -196,7 +212,9 @@ public function testValidateClientMissingClientId(): void
196212 $ clientRepositoryMock = $ this ->getMockBuilder (ClientRepositoryInterface::class)->getMock ();
197213 $ clientRepositoryMock ->method ('getClientEntity ' )->willReturn ($ client );
198214
199- $ grantMock = $ this ->getAbstractGrantStub ();
215+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
216+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
217+ ->getMock ();
200218 $ grantMock ->setClientRepository ($ clientRepositoryMock );
201219
202220 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -215,7 +233,9 @@ public function testValidateClientMissingClientSecret(): void
215233 $ clientRepositoryMock = $ this ->getMockBuilder (ClientRepositoryInterface::class)->getMock ();
216234 $ clientRepositoryMock ->method ('validateClient ' )->willReturn (false );
217235
218- $ grantMock = $ this ->getAbstractGrantStub ();
236+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
237+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
238+ ->getMock ();
219239 $ grantMock ->setClientRepository ($ clientRepositoryMock );
220240
221241 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -237,7 +257,9 @@ public function testValidateClientInvalidClientSecret(): void
237257 $ clientRepositoryMock = $ this ->getMockBuilder (ClientRepositoryInterface::class)->getMock ();
238258 $ clientRepositoryMock ->method ('validateClient ' )->willReturn (false );
239259
240- $ grantMock = $ this ->getAbstractGrantStub ();
260+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
261+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
262+ ->getMock ();
241263 $ grantMock ->setClientRepository ($ clientRepositoryMock );
242264
243265 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -260,7 +282,9 @@ public function testValidateClientBadClient(): void
260282 $ clientRepositoryMock = $ this ->getMockBuilder (ClientRepositoryInterface::class)->getMock ();
261283 $ clientRepositoryMock ->method ('validateClient ' )->willReturn (false );
262284
263- $ grantMock = $ this ->getAbstractGrantStub ();
285+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
286+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
287+ ->getMock ();
264288 $ grantMock ->setClientRepository ($ clientRepositoryMock );
265289
266290 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -280,7 +304,10 @@ public function testValidateClientBadClient(): void
280304
281305 public function testCanRespondToRequest (): void
282306 {
283- $ grantMock = $ this ->getAbstractGrantStub ();
307+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
308+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
309+ ->getMock ();
310+ $ grantMock ->method ('getIdentifier ' )->willReturn ('foobar ' );
284311 $ grantMock ->setDefaultScope ('defaultScope ' );
285312
286313 $ serverRequest = (new ServerRequest ())->withParsedBody ([
@@ -298,7 +325,9 @@ public function testIssueRefreshToken(): void
298325 ->method ('getNewRefreshToken ' )
299326 ->willReturn (new RefreshTokenEntity ());
300327
301- $ grantMock = $ this ->getAbstractGrantStub ();
328+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
329+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
330+ ->getMock ();
302331 $ grantMock ->setRefreshTokenTTL (new DateInterval ('PT1M ' ));
303332 $ grantMock ->setRefreshTokenRepository ($ refreshTokenRepoMock );
304333
@@ -323,7 +352,9 @@ public function testIssueNullRefreshToken(): void
323352 ->method ('getNewRefreshToken ' )
324353 ->willReturn (null );
325354
326- $ grantMock = $ this ->getAbstractGrantStub ();
355+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
356+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
357+ ->getMock ();
327358 $ grantMock ->setRefreshTokenTTL (new DateInterval ('PT1M ' ));
328359 $ grantMock ->setRefreshTokenRepository ($ refreshTokenRepoMock );
329360
@@ -348,7 +379,9 @@ public function testIssueNullRefreshTokenUnauthorizedClient(): void
348379 $ refreshTokenRepoMock = $ this ->getMockBuilder (RefreshTokenRepositoryInterface::class)->getMock ();
349380 $ refreshTokenRepoMock ->expects (self ::never ())->method ('getNewRefreshToken ' );
350381
351- $ grantMock = $ this ->getAbstractGrantStub ();
382+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
383+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
384+ ->getMock ();
352385 $ grantMock ->setRefreshTokenTTL (new DateInterval ('PT1M ' ));
353386 $ grantMock ->setRefreshTokenRepository ($ refreshTokenRepoMock );
354387
@@ -367,7 +400,9 @@ public function testIssueAccessToken(): void
367400 $ accessTokenRepoMock = $ this ->getMockBuilder (AccessTokenRepositoryInterface::class)->getMock ();
368401 $ accessTokenRepoMock ->method ('getNewToken ' )->willReturn (new AccessTokenEntity ());
369402
370- $ grantMock = $ this ->getAbstractGrantStub ();
403+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
404+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
405+ ->getMock ();
371406 $ grantMock ->setPrivateKey (new CryptKey ('file:// ' . __DIR__ . '/../Stubs/private.key ' ));
372407 $ grantMock ->setAccessTokenRepository ($ accessTokenRepoMock );
373408
@@ -392,7 +427,9 @@ public function testIssueAuthCode(): void
392427 $ authCodeRepoMock = $ this ->getMockBuilder (AuthCodeRepositoryInterface::class)->getMock ();
393428 $ authCodeRepoMock ->expects (self ::once ())->method ('getNewAuthCode ' )->willReturn (new AuthCodeEntity ());
394429
395- $ grantMock = $ this ->getAbstractGrantStub ();
430+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
431+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
432+ ->getMock ();
396433 $ grantMock ->setAuthCodeRepository ($ authCodeRepoMock );
397434
398435 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -417,7 +454,9 @@ public function testIssueAuthCode(): void
417454
418455 public function testGetCookieParameter (): void
419456 {
420- $ grantMock = $ this ->getAbstractGrantStub ();
457+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
458+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
459+ ->getMock ();
421460
422461 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
423462 $ method = $ abstractGrantReflection ->getMethod ('getCookieParameter ' );
@@ -433,7 +472,9 @@ public function testGetCookieParameter(): void
433472
434473 public function testGetQueryStringParameter (): void
435474 {
436- $ grantMock = $ this ->getAbstractGrantStub ();
475+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
476+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
477+ ->getMock ();
437478
438479 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
439480 $ method = $ abstractGrantReflection ->getMethod ('getQueryStringParameter ' );
@@ -453,7 +494,9 @@ public function testValidateScopes(): void
453494 $ scopeRepositoryMock = $ this ->getMockBuilder (ScopeRepositoryInterface::class)->getMock ();
454495 $ scopeRepositoryMock ->expects (self ::exactly (3 ))->method ('getScopeEntityByIdentifier ' )->willReturn ($ scope );
455496
456- $ grantMock = $ this ->getAbstractGrantStub ();
497+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
498+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
499+ ->getMock ();
457500 $ grantMock ->setScopeRepository ($ scopeRepositoryMock );
458501
459502 self ::assertEquals ([$ scope , $ scope , $ scope ], $ grantMock ->validateScopes ('basic test 0 ' ));
@@ -464,7 +507,9 @@ public function testValidateScopesBadScope(): void
464507 $ scopeRepositoryMock = $ this ->getMockBuilder (ScopeRepositoryInterface::class)->getMock ();
465508 $ scopeRepositoryMock ->method ('getScopeEntityByIdentifier ' )->willReturn (null );
466509
467- $ grantMock = $ this ->getAbstractGrantStub ();
510+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
511+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
512+ ->getMock ();
468513 $ grantMock ->setScopeRepository ($ scopeRepositoryMock );
469514
470515 $ this ->expectException (OAuthServerException::class);
@@ -474,7 +519,9 @@ public function testValidateScopesBadScope(): void
474519
475520 public function testGenerateUniqueIdentifier (): void
476521 {
477- $ grantMock = $ this ->getAbstractGrantStub ();
522+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
523+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
524+ ->getMock ();
478525
479526 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
480527 $ method = $ abstractGrantReflection ->getMethod ('generateUniqueIdentifier ' );
@@ -485,13 +532,17 @@ public function testGenerateUniqueIdentifier(): void
485532
486533 public function testCanRespondToAuthorizationRequest (): void
487534 {
488- $ grantMock = $ this ->getAbstractGrantStub ();
535+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
536+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
537+ ->getMock ();
489538 self ::assertFalse ($ grantMock ->canRespondToAuthorizationRequest (new ServerRequest ()));
490539 }
491540
492541 public function testValidateAuthorizationRequest (): void
493542 {
494- $ grantMock = $ this ->getAbstractGrantStub ();
543+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
544+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
545+ ->getMock ();
495546
496547 $ this ->expectException (LogicException::class);
497548
@@ -500,7 +551,9 @@ public function testValidateAuthorizationRequest(): void
500551
501552 public function testCompleteAuthorizationRequest (): void
502553 {
503- $ grantMock = $ this ->getAbstractGrantStub ();
554+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
555+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
556+ ->getMock ();
504557
505558 $ this ->expectException (LogicException::class);
506559
@@ -519,7 +572,9 @@ public function testUnauthorizedClient(): void
519572 ->with ('foo ' )
520573 ->willReturn ($ client );
521574
522- $ grantMock = $ this ->getAbstractGrantStub ();
575+ $ grantMock = $ this ->getMockBuilder (AbstractGrant::class)
576+ ->onlyMethods (['getIdentifier ' , 'respondToAccessTokenRequest ' ])
577+ ->getMock ();
523578 $ grantMock ->setClientRepository ($ clientRepositoryMock );
524579
525580 $ abstractGrantReflection = new ReflectionClass ($ grantMock );
@@ -531,19 +586,4 @@ public function testUnauthorizedClient(): void
531586
532587 $ getClientEntityOrFailMethod ->invoke ($ grantMock , 'foo ' , new ServerRequest ());
533588 }
534-
535- private function getAbstractGrantStub (): AbstractGrant
536- {
537- return new class () extends AbstractGrant {
538- public function getIdentifier (): string
539- {
540- return 'foobar ' ;
541- }
542-
543- public function respondToAccessTokenRequest (ServerRequestInterface $ request , ResponseTypeInterface $ responseType , DateInterval $ accessTokenTTL ): ResponseTypeInterface
544- {
545- return $ responseType ;
546- }
547- };
548- }
549589}
0 commit comments