From 850c41b34290d4dc2b968410507ff6ce812e3f5a Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Wed, 15 Nov 2023 06:11:32 +0000 Subject: [PATCH] Iterate 1) Fix indentation 2) Simplify `runTestCase` method in AuthTokenMiddlewareTest 3) Improve testing. --- tests/FetchAuthTokenTest.php | 8 ++-- tests/Middleware/AuthTokenMiddlewareTest.php | 39 +++++++++++++------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/tests/FetchAuthTokenTest.php b/tests/FetchAuthTokenTest.php index d4196a3dd..6fe7df242 100644 --- a/tests/FetchAuthTokenTest.php +++ b/tests/FetchAuthTokenTest.php @@ -57,10 +57,10 @@ class_implements($fetcherClass) if (is_a($fetcherClass, UpdateMetadataInterface::class, true)) { $mockFetcher->updateMetadata(Argument::cetera()) - ->shouldBeCalledTimes(1)->will(function () use (&$httpHandlerCalled) { - $httpHandlerCalled = true; - return ['authorization' => ['Bearer xyz']]; - }); + ->shouldBeCalledTimes(1)->will(function () use (&$httpHandlerCalled) { + $httpHandlerCalled = true; + return ['authorization' => ['Bearer xyz']]; + }); } else { $mockFetcher->fetchAuthToken(Argument::any()) ->shouldBeCalledTimes(1) diff --git a/tests/Middleware/AuthTokenMiddlewareTest.php b/tests/Middleware/AuthTokenMiddlewareTest.php index 231d8ff6a..cf0eb4182 100644 --- a/tests/Middleware/AuthTokenMiddlewareTest.php +++ b/tests/Middleware/AuthTokenMiddlewareTest.php @@ -50,7 +50,10 @@ public function testOnlyTouchesWhenAuthConfigScoped() ->willReturn([]); $this->mockRequest->withHeader()->shouldNotBeCalled(); - $this->runTestCase($this->mockFetcher->reveal(), false); + $middleware = new AuthTokenMiddleware($this->mockFetcher->reveal()); + $mock = new MockHandler([new Response(200)]); + $callable = $middleware($mock); + $callable($this->mockRequest->reveal(), ['auth' => 'not_google_auth']); } public function testAddsTheTokenAsAnAuthorizationHeader() @@ -63,7 +66,7 @@ public function testAddsTheTokenAsAnAuthorizationHeader() ->shouldBeCalledTimes(1) ->willReturn($this->mockRequest->reveal()); - $this->runTestCase($this->mockFetcher->reveal(), true); + $this->runTestCase($this->mockFetcher->reveal()); } public function testDoesNotAddAnAuthorizationHeaderOnNoAccessToken() @@ -75,7 +78,7 @@ public function testDoesNotAddAnAuthorizationHeaderOnNoAccessToken() $this->mockRequest->withHeader('authorization', 'Bearer ') ->willReturn($this->mockRequest->reveal()); - $this->runTestCase($this->mockFetcher->reveal(), true); + $this->runTestCase($this->mockFetcher->reveal()); } public function testUsesIdTokenWhenAccessTokenDoesNotExist() @@ -89,7 +92,7 @@ public function testUsesIdTokenWhenAccessTokenDoesNotExist() ->shouldBeCalledTimes(1) ->willReturn($this->mockRequest->reveal()); - $this->runTestCase($this->mockFetcher->reveal(), true); + $this->runTestCase($this->mockFetcher->reveal()); } @@ -122,7 +125,7 @@ public function testUsesCachedAccessToken() null, $this->mockCache->reveal() ); - $this->runTestCase($cachedFetcher, true); + $this->runTestCase($cachedFetcher); } public function testUsesCachedIdToken() @@ -154,7 +157,7 @@ public function testUsesCachedIdToken() null, $this->mockCache->reveal() ); - $this->runTestCase($cachedFetcher, true); + $this->runTestCase($cachedFetcher); } public function testGetsCachedAuthTokenUsingCacheOptions() @@ -187,7 +190,7 @@ public function testGetsCachedAuthTokenUsingCacheOptions() ['prefix' => $prefix], $this->mockCache->reveal() ); - $this->runTestCase($cachedFetcher, true); + $this->runTestCase($cachedFetcher); } public function testShouldSaveValueInCacheWithSpecifiedPrefix() @@ -228,7 +231,7 @@ public function testShouldSaveValueInCacheWithSpecifiedPrefix() ['prefix' => $prefix, 'lifetime' => $lifetime], $this->mockCache->reveal() ); - $this->runTestCase($cachedFetcher, true); + $this->runTestCase($cachedFetcher); } /** @@ -301,9 +304,15 @@ public function testAddAuthHeadersFromUpdateMetadata() $request = new Request('GET', 'http://foo.com'); $middleware = new AuthTokenMiddleware($this->mockFetcher->reveal()); - $mock = new MockHandler([new Response(200)]); + $mockHandlerCalled = false; + $mock = new MockHandler([function ($request, $options) use ($authResult, &$mockHandlerCalled) { + $this->assertEquals($authResult['authorization'], $request->getHeaderLine('authorization')); + $mockHandlerCalled = true; + return new Response(200); + }]); $callable = $middleware($mock); $callable($request, ['auth' => 'google_auth']); + $this->assertTrue($mockHandlerCalled); } public function testOverlappingAddAuthHeadersFromUpdateMetadata() @@ -314,7 +323,6 @@ public function testOverlappingAddAuthHeadersFromUpdateMetadata() ]; $request = new Request('GET', 'http://foo.com'); - $request->withHeader('x-goog-api-client', 'default-value'); $this->mockFetcher->willImplement(UpdateMetadataInterface::class); $this->mockFetcher->updateMetadata(Argument::cetera()) @@ -324,22 +332,25 @@ public function testOverlappingAddAuthHeadersFromUpdateMetadata() ->willReturn(['access_token' => '1/abcdef1234567890']); $middleware = new AuthTokenMiddleware($this->mockFetcher->reveal()); - $mock = new MockHandler([function ($request, $options) use ($authHeaders) { + + $mockHandlerCalled = false; + $mock = new MockHandler([function ($request, $options) use ($authHeaders, &$mockHandlerCalled) { $this->assertEquals($authHeaders['authorization'], $request->getHeaderLine('authorization')); $this->assertArrayHasKey('x-goog-api-client', $request->getHeaders()); + $mockHandlerCalled = true; return new Response(200); }]); $callable = $middleware($mock); $callable($request, ['auth' => 'google_auth']); + $this->assertTrue($mockHandlerCalled); } - private function runTestCase($fetcher, bool $isGoogleAuth) + private function runTestCase($fetcher) { - $authType = $isGoogleAuth ? 'google_auth' :'not_google_auth'; $middleware = new AuthTokenMiddleware($fetcher); $mock = new MockHandler([new Response(200)]); $callable = $middleware($mock); - $callable($this->mockRequest->reveal(), ['auth' => $authType]); + $callable($this->mockRequest->reveal(), ['auth' => 'google_auth']); } public function provideShouldNotifyTokenCallback()