Skip to content

Commit

Permalink
Adds tests for the GitHub tags
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Mar 10, 2024
1 parent b11caee commit dca0ce9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/Shell/GitHubDockerTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ protected function getToken(): string

$response = $this->guzzle->get('https://ghcr.io/token?' . http_build_query([
'scope' => "repository:{$image}:pull",
]));
]), [
"http_errors" => false,

Check failure on line 41 in app/Shell/GitHubDockerTags.php

View workflow job for this annotation

GitHub Actions / PHPCS

String "http_errors" does not require double quotes; use single quotes instead
]);

if ($response->getStatusCode() !== 200) {
throw new RuntimeException("Something went wrong getting the Token from GitHub's registry.");
Expand Down
59 changes: 59 additions & 0 deletions tests/Feature/GitHubTagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\Feature;

use App\Services\Buggregator;
use App\Shell\DockerTags;
use App\Shell\GitHubDockerTags;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Mockery as M;
use RuntimeException;
use Tests\TestCase;

class GitHubTagsTest extends TestCase
{
/** @test */
function it_fetches_latest_tag()
{
$handlerStack = HandlerStack::create($this->mockImagesResponseHandler());
$client = new Client(['handler' => $handlerStack]);

/** @var DockerTags $dockerTags */
$dockerTags = M::mock(GitHubDockerTags::class, [$client, app(Buggregator::class)])->makePartial();

$this->assertEquals('latest', $dockerTags->getLatestTag());
}

/** @test */
function it_throws_exception_when_token_request_fails()
{
$handlerStack = HandlerStack::create($this->mockImagesResponseHandler(false));
$client = new Client(['handler' => $handlerStack]);

/** @var DockerTags $dockerTags */
$dockerTags = M::mock(GitHubDockerTags::class, [$client, app(Buggregator::class)])->makePartial();

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

$dockerTags->getLatestTag();
}

private function mockImagesResponseHandler($tokenWorks = true)
{
return new MockHandler([
new Response($tokenWorks ? 200 : 400, [], json_encode([
'token' => 'fake-token',
])),
new Response(200, [], json_encode([
'tags' => [
'latest',
'1.0.0',
'1.0.0.rc-1',
],
])),
]);
}
}

0 comments on commit dca0ce9

Please sign in to comment.