Skip to content

Commit

Permalink
Return StreamInterface instead of string in mocked `ResponseInterfa…
Browse files Browse the repository at this point in the history
…ce::getBody()` functions
  • Loading branch information
fabacino committed Nov 7, 2024
1 parent 84211f6 commit 5d28972
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions test/src/Provider/GithubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -143,24 +145,24 @@ 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')
->andReturn(200);

$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')
Expand Down Expand Up @@ -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')
Expand All @@ -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);

Expand All @@ -246,34 +248,34 @@ 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')
->andReturn(200);

$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')
->andReturn(200);

$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')
Expand All @@ -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;
}
}

0 comments on commit 5d28972

Please sign in to comment.