Skip to content

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
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/Github/Api/V3/CreateReleaseThroughApiCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __invoke(

$response = $this->client->sendRequest($request);

Psl\invariant($response->getStatusCode() >= 200 || $response->getStatusCode() <= 299, 'Failed to create release through GitHub API.');
Psl\invariant($response->getStatusCode() >= 200 && $response->getStatusCode() <= 299, 'Failed to create release through GitHub API.');

$responseBody = $response
->getBody()
Expand Down
186 changes: 186 additions & 0 deletions test/unit/Github/Api/V3/CreateReleaseThroughApiCallTest.php
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
{
/** @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',
);
}
}