Skip to content

Commit

Permalink
Add milestone creation tests
Browse files Browse the repository at this point in the history
Signed-off-by: Geert Eltink <[email protected]>
  • Loading branch information
geerteltink committed Aug 30, 2020
1 parent 2019f66 commit 1e59282
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 2 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ composer-validate:
composer validate

static-analysis:
vendor/bin/phpstan analyse
vendor/bin/psalm

test:
Expand Down
2 changes: 1 addition & 1 deletion src/Github/Api/V3/CreateMilestoneThroughApiCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __invoke(
)
->withAddedHeader('Content-Type', 'application/json')
->withAddedHeader('User-Agent', 'Ocramius\'s minimal API V3 client')
->withAddedHeader('Authorization', 'bearer ' . $this->apiToken);
->withAddedHeader('Authorization', 'token ' . $this->apiToken);

$request
->getBody()
Expand Down
174 changes: 174 additions & 0 deletions test/unit/Github/Api/V3/CreateMilestoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?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\CreateMilestoneFailed;
use Laminas\AutomaticReleases\Github\Api\V3\CreateMilestoneThroughApiCall;
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 Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;

use function uniqid;

class CreateMilestoneTest extends TestCase
{
/** @var ClientInterface&MockObject */
private $httpClient;
/** @var RequestFactoryInterface&MockObject */
private $messageFactory;
/** @var LoggerInterface&MockObject */
private $logger;
/** @psalm-var non-empty-string */
private string $apiToken;
private CreateMilestoneThroughApiCall $createMilestone;

protected function setUp(): void
{
parent::setUp();

$this->httpClient = $this->createMock(ClientInterface::class);
$this->messageFactory = $this->createMock(RequestFactoryInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
$apiToken = uniqid('apiToken', true);

Assert::notEmpty($apiToken);

$this->apiToken = $apiToken;
$this->createMilestone = new CreateMilestoneThroughApiCall(
$this->messageFactory,
$this->httpClient,
$this->apiToken,
$this->logger
);
}

public function testSuccessfulRequest(): void
{
$this
->messageFactory
->expects(self::any())
->method('createRequest')
->with('POST', 'https://api.github.com/repos/foo/bar/milestones')
->willReturn(new Request('https://the-domain.com/the-path'));

$validResponse = (new Response())->withStatus(201);

$validResponse->getBody()->write(<<<'JSON'
{
"html_url": "http://another-domain.com/the-pr"
}
JSON
);

$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'
{
"title": "1.2.3"
}
JSON
,
$request->getBody()->__toString()
);

return true;
}))
->willReturn($validResponse);

self::assertEquals(
'http://another-domain.com/the-pr',
$this->createMilestone->__invoke(
RepositoryName::fromFullName('foo/bar'),
SemVerVersion::fromMilestoneName('1.2.3')
)
);
}

public function testExistingMilestone(): void
{
$this
->messageFactory
->expects(self::any())
->method('createRequest')
->with('POST', 'https://api.github.com/repos/foo/bar/milestones')
->willReturn(new Request('https://the-domain.com/the-path'));

$validResponse = (new Response())->withStatus(422);

$validResponse->getBody()->write(<<<'JSON'
{
"documentation_url": "https://docs.github.com/rest/reference/issues#create-a-milestone",
"errors": [
{
"code": "already_exists",
"field": "title",
"resource": "Milestone"
}
],
"message": "Validation Failed"
}
JSON
);

$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'
{
"title": "1.2.3"
}
JSON
,
$request->getBody()->__toString()
);

return true;
}))
->willReturn($validResponse);

$this->expectException(CreateMilestoneFailed::class);

$this->createMilestone->__invoke(
RepositoryName::fromFullName('foo/bar'),
SemVerVersion::fromMilestoneName('1.2.3')
);
}
}

0 comments on commit 1e59282

Please sign in to comment.