diff --git a/test/src/Provider/GithubTest.php b/test/src/Provider/GithubTest.php index 51f0aef..fb387ad 100644 --- a/test/src/Provider/GithubTest.php +++ b/test/src/Provider/GithubTest.php @@ -84,7 +84,9 @@ public function testGetAccessToken(): void { $response = m::mock('Psr\Http\Message\ResponseInterface'); $response->shouldReceive('getBody') - ->andReturn('{"access_token":"mock_access_token", "scope":"repo,gist", "token_type":"bearer"}'); + ->andReturn($this->createStream( + '{"access_token":"mock_access_token", "scope":"repo,gist", "token_type":"bearer"}' + )); $response->shouldReceive('getHeader') ->andReturn(['content-type' => 'json']); $response->shouldReceive('getStatusCode') @@ -109,11 +111,11 @@ public function testGithubEnterpriseDomainUrls(): void $response = m::mock('Psr\Http\Message\ResponseInterface'); $response->shouldReceive('getBody') ->times(1) - ->andReturn(http_build_query([ + ->andReturn($this->createStream(http_build_query([ 'access_token' => 'mock_access_token', 'expires' => 3600, 'refresh_token' => 'mock_refresh_token', - ])); + ]))); $response->shouldReceive('getHeader') ->andReturn(['content-type' => 'application/x-www-form-urlencoded']); $response->shouldReceive('getStatusCode') @@ -143,11 +145,11 @@ public function testUserData(): void $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); $postResponse->shouldReceive('getBody') - ->andReturn(http_build_query([ + ->andReturn($this->createStream(http_build_query([ 'access_token' => 'mock_access_token', 'expires' => 3600, 'refresh_token' => 'mock_refresh_token', - ])); + ]))); $postResponse->shouldReceive('getHeader') ->andReturn(['content-type' => 'application/x-www-form-urlencoded']); $postResponse->shouldReceive('getStatusCode') @@ -155,12 +157,12 @@ public function testUserData(): void $userResponse = m::mock('Psr\Http\Message\ResponseInterface'); $userResponse->shouldReceive('getBody') - ->andReturn(json_encode([ + ->andReturn($this->createStream(json_encode([ "id" => $userId, "login" => $nickname, "name" => $name, "email" => $email - ])); + ]))); $userResponse->shouldReceive('getHeader') ->andReturn(['content-type' => 'json']); $userResponse->shouldReceive('getStatusCode') @@ -191,12 +193,12 @@ public function testExceptionThrownWhenErrorObjectReceived(): void $status = rand(400, 600); $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); $postResponse->shouldReceive('getBody') - ->andReturn(json_encode([ + ->andReturn($this->createStream(json_encode([ 'message' => 'Validation Failed', 'errors' => [ ['resource' => 'Issue', 'field' => 'title', 'code' => 'missing_field'], ], - ])); + ]))); $postResponse->shouldReceive('getHeader') ->andReturn(['content-type' => 'json']); $postResponse->shouldReceive('getStatusCode') @@ -218,11 +220,11 @@ public function testExceptionThrownWhenOAuthErrorReceived(): void $status = 200; $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); $postResponse->shouldReceive('getBody') - ->andReturn(json_encode([ + ->andReturn($this->createStream(json_encode([ "error" => "bad_verification_code", "error_description" => "The code passed is incorrect or expired.", "error_uri" => "https =>//developer.github.com/v3/oauth/#bad-verification-code" - ])); + ]))); $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); $postResponse->shouldReceive('getStatusCode')->andReturn($status); @@ -246,11 +248,11 @@ public function testUserDataWithMissingEmail(): void $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); $postResponse->shouldReceive('getBody') - ->andReturn(http_build_query([ + ->andReturn($this->createStream(http_build_query([ 'access_token' => 'mock_access_token', 'expires' => 3600, 'refresh_token' => 'mock_refresh_token', - ])); + ]))); $postResponse->shouldReceive('getHeader') ->andReturn(['content-type' => 'application/x-www-form-urlencoded']); $postResponse->shouldReceive('getStatusCode') @@ -258,12 +260,12 @@ public function testUserDataWithMissingEmail(): void $userResponse = m::mock('Psr\Http\Message\ResponseInterface'); $userResponse->shouldReceive('getBody') - ->andReturn(json_encode([ + ->andReturn($this->createStream(json_encode([ "id" => $userId, "login" => $nickname, "name" => $name, "email" => null - ])); + ]))); $userResponse->shouldReceive('getHeader') ->andReturn(['content-type' => 'json']); $userResponse->shouldReceive('getStatusCode') @@ -271,9 +273,9 @@ public function testUserDataWithMissingEmail(): void $emailResponse = m::mock('Psr\Http\Message\ResponseInterface'); $emailResponse->shouldReceive('getBody') - ->andReturn(json_encode([ + ->andReturn($this->createStream(json_encode([ ['email' => $email], - ])); + ]))); $emailResponse->shouldReceive('getHeader') ->andReturn(['content-type' => 'json']); $emailResponse->shouldReceive('getStatusCode') @@ -298,4 +300,13 @@ public function testUserDataWithMissingEmail(): void $this->assertEquals($email, $user->toArray()['email']); $this->assertStringContainsString($nickname, $user->getUrl()); } + + private function createStream(string $body): \Psr\Http\Message\StreamInterface + { + $stream = m::mock('Psr\Http\Message\StreamInterface'); + $stream->shouldReceive('__toString') + ->andReturn($body); + + return $stream; + } }