Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return StreamInterface instead of string in mocked ResponseInterface::getBody() functions #27

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading