-
-
Notifications
You must be signed in to change notification settings - Fork 22
Check the response code is >= 200 && <= 299
on release
#204
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
Merged
Ocramius
merged 2 commits into
laminas:1.15.x
from
internalsystemerror:hotfix/incorrect-release-api-operand
Aug 17, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
test/unit/Github/Api/V3/CreateReleaseThroughApiCallTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\Test\Unit\Github\Api\V3; | ||
|
||
use Laminas\AutomaticReleases\Git\Value\SemVerVersion; | ||
use Laminas\AutomaticReleases\Github\Api\V3\CreateReleaseThroughApiCall; | ||
use Laminas\AutomaticReleases\Github\Value\RepositoryName; | ||
use Laminas\Diactoros\Request; | ||
use Laminas\Diactoros\Response; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Psl\Exception\InvariantViolationException; | ||
use Psl\Json\Exception\DecodeException; | ||
use Psl\SecureRandom; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** @covers \Laminas\AutomaticReleases\Github\Api\V3\CreateReleaseThroughApiCall */ | ||
final class CreateReleaseThroughApiCallTest extends TestCase | ||
Ocramius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** @var ClientInterface&MockObject */ | ||
private ClientInterface $httpClient; | ||
/** @var RequestFactoryInterface&MockObject */ | ||
private RequestFactoryInterface $messageFactory; | ||
/** @psalm-var non-empty-string */ | ||
private string $apiToken; | ||
private CreateReleaseThroughApiCall $createRelease; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->httpClient = $this->createMock(ClientInterface::class); | ||
$this->messageFactory = $this->createMock(RequestFactoryInterface::class); | ||
$this->apiToken = 'apiToken' . SecureRandom\string(8); | ||
$this->createRelease = new CreateReleaseThroughApiCall( | ||
$this->messageFactory, | ||
$this->httpClient, | ||
$this->apiToken | ||
); | ||
} | ||
|
||
/** | ||
* @psalm-param positive-int $responseCode | ||
* | ||
* @dataProvider exampleValidResponseCodes | ||
*/ | ||
public function testSuccessfulRequest(int $responseCode): void | ||
{ | ||
$this->messageFactory | ||
->method('createRequest') | ||
->with('POST', 'https://api.github.com/repos/foo/bar/releases') | ||
->willReturn(new Request('https://the-domain.com/the-path')); | ||
|
||
$validResponse = (new Response()) | ||
->withStatus($responseCode); | ||
|
||
$validResponse->getBody() | ||
->write('{"html_url": "http://the-domain.com/release"}'); | ||
|
||
$this->httpClient | ||
->expects(self::once()) | ||
->method('sendRequest') | ||
->with(self::callback(function (RequestInterface $request): bool { | ||
self::assertSame( | ||
[ | ||
'Host' => ['the-domain.com'], | ||
'Content-Type' => ['application/json'], | ||
'User-Agent' => ['Ocramius\'s minimal API V3 client'], | ||
'Authorization' => ['token ' . $this->apiToken], | ||
], | ||
$request->getHeaders() | ||
); | ||
|
||
self::assertJsonStringEqualsJsonString( | ||
<<<'JSON' | ||
{ | ||
"body": "A description for my awesome release", | ||
"name": "1.2.3", | ||
"tag_name": "1.2.3" | ||
} | ||
JSON | ||
, | ||
$request->getBody() | ||
->__toString() | ||
); | ||
|
||
return true; | ||
})) | ||
->willReturn($validResponse); | ||
|
||
self::assertEquals( | ||
'http://the-domain.com/release', | ||
$this->createRelease->__invoke( | ||
RepositoryName::fromFullName('foo/bar'), | ||
SemVerVersion::fromMilestoneName('1.2.3'), | ||
'A description for my awesome release', | ||
) | ||
); | ||
} | ||
|
||
/** @psalm-return non-empty-list<array{positive-int}> */ | ||
public function exampleValidResponseCodes(): array | ||
{ | ||
return [ | ||
[200], | ||
[201], | ||
[204], | ||
]; | ||
} | ||
|
||
/** | ||
* @psalm-param positive-int $responseCode | ||
* | ||
* @dataProvider exampleFailureResponseCodes | ||
*/ | ||
public function testRequestFailedToCreateReleaseDueToInvalidResponseCode(int $responseCode): void | ||
{ | ||
$this->messageFactory | ||
->method('createRequest') | ||
->with('POST', 'https://api.github.com/repos/foo/bar/releases') | ||
->willReturn(new Request('https://the-domain.com/the-path')); | ||
|
||
$invalidResponse = (new Response()) | ||
->withStatus($responseCode); | ||
|
||
$invalidResponse->getBody() | ||
->write('{"html_url": "http://the-domain.com/release"}'); | ||
|
||
$this->httpClient | ||
->expects(self::once()) | ||
->method('sendRequest') | ||
->willReturn($invalidResponse); | ||
|
||
$this->expectException(InvariantViolationException::class); | ||
$this->expectExceptionMessage('Failed to create release through GitHub API.'); | ||
|
||
$this->createRelease->__invoke( | ||
RepositoryName::fromFullName('foo/bar'), | ||
SemVerVersion::fromMilestoneName('1.2.3'), | ||
'A description for my awesome release', | ||
); | ||
} | ||
|
||
/** @psalm-return non-empty-list<array{positive-int}> */ | ||
public function exampleFailureResponseCodes(): array | ||
{ | ||
return [ | ||
[199], | ||
[400], | ||
[401], | ||
[500], | ||
]; | ||
} | ||
|
||
public function testRequestFailedToCreateReleaseDueToInvalidResponseBody(): void | ||
{ | ||
$this->messageFactory | ||
->method('createRequest') | ||
->with('POST', 'https://api.github.com/repos/foo/bar/releases') | ||
->willReturn(new Request('https://the-domain.com/the-path')); | ||
|
||
$invalidResponse = (new Response()) | ||
->withStatus(200); | ||
|
||
$invalidResponse->getBody() | ||
->write('{"invalid": "response"}'); | ||
|
||
$this->httpClient | ||
->expects(self::once()) | ||
->method('sendRequest') | ||
->willReturn($invalidResponse); | ||
|
||
$this->expectException(DecodeException::class); | ||
$this->expectExceptionMessage('"array{\'html_url\': non-empty-string}"'); | ||
|
||
$this->createRelease->__invoke( | ||
RepositoryName::fromFullName('foo/bar'), | ||
SemVerVersion::fromMilestoneName('1.2.3'), | ||
'A description for my awesome release', | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.