Files
TijdVoorDeTest/tests/Service/GitHubReleasesServiceTest.php
T
Marijn 4e98909f11 Add GitHub Releases modal to backoffice (#210)
* Add GitHub Releases modal to backoffice

Shows release notes (fetched from the GitHub API, cached for an hour,
Markdown rendered via league/commonmark) from a top-right nav button
available whether logged in or out, with the current version shown in
the modal header. Also clears cache.app on container start so the
release cache can't carry stale data across redeploys of the
var/ Docker volume.

* Render release notes markdown via twig/markdown-extra instead of manual CommonMark

twig/extra-bundle already wires the markdown filter to league/commonmark
automatically once twig/markdown-extra is installed, so the service no
longer needs to instantiate and call CommonMarkConverter itself.

* Explicitly disallow unsafe link protocols in rendered markdown

twig/extra-bundle's default (allow_unsafe_links: true) permits
javascript:/vbscript:/file:/data: URLs in rendered links, kept for
backwards compatibility. Disable it explicitly since we now render
release note markdown through this filter.

* Autolink bare URLs in rendered release notes

The Twig CommonMark converter only registers CommonMarkCoreExtension by
default, which doesn't turn bare URLs into clickable links (unlike
GitHub's own renderer). Register CommonMark's AutolinkExtension via the
twig.markdown.league_extension tag so PR/compare URLs in release notes
render as actual hyperlinks instead of plain text.

* Address review feedback on GitHubReleasesService and tests

- Extract the cache callback into a named fetchReleases() method instead
  of an inline closure with all the fetch/parse logic.
- Simplify the User-Agent header to just "TijdVoorDeTest".
- Add a 5s HTTP timeout so a slow GitHub API can't block the request.
- Cache HTTP failures for only 60s instead of the full hour, so a brief
  GitHub outage doesn't suppress releases for as long.
- Replace deprecated word-wrap with overflow-wrap in release-notes CSS.
- Extract duplicated mock setup in ReleasesControllerTest into a helper.
- Reword the "could not load" translation ("Kan" instead of "Kon").

* Address second round of CodeRabbit feedback

- Escape raw HTML embedded in release-note markdown instead of passing
  it through, via twig_extra.commonmark.html_input: escape.
- Don't cache GitHub API failures at all (set $save = false in the
  cache callback) instead of caching them for a short TTL, so the very
  next request retries immediately.
- Sort releases by published_at descending before mapping, so the
  first entry (used for the current-version badge and default-expanded
  accordion item) is guaranteed to be the newest regardless of the
  order GitHub's API happens to return.
2026-07-12 18:59:36 +00:00

96 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tvdt\Tests\Service;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Safe\DateTimeImmutable;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Tvdt\Service\GitHubReleasesService;
#[CoversClass(GitHubReleasesService::class)]
final class GitHubReleasesServiceTest extends TestCase
{
public function testGetReleasesMapsGitHubResponse(): void
{
$body = json_encode([
[
'tag_name' => 'v0.8.0',
'name' => 'v0.8.0',
'published_at' => '2026-07-12T10:00:00Z',
'body' => "## Added\n- Something new",
'html_url' => 'https://github.com/MarijnDoeve/TijdVoorDeTest/releases/tag/v0.8.0',
],
], \JSON_THROW_ON_ERROR);
$httpClient = new MockHttpClient([new MockResponse((string) $body, ['response_headers' => ['content-type' => 'application/json']])]);
$subject = new GitHubReleasesService($httpClient, new ArrayAdapter());
$releases = $subject->getReleases();
$this->assertEquals([
'tagName' => 'v0.8.0',
'name' => 'v0.8.0',
'publishedAt' => new DateTimeImmutable('2026-07-12T10:00:00Z'),
'body' => "## Added\n- Something new",
'url' => 'https://github.com/MarijnDoeve/TijdVoorDeTest/releases/tag/v0.8.0',
], $releases[0]);
}
public function testGetReleasesReturnsEmptyArrayOnHttpFailure(): void
{
$httpClient = new MockHttpClient(static fn (): MockResponse => new MockResponse('', ['http_code' => 500]));
$subject = new GitHubReleasesService($httpClient, new ArrayAdapter());
$this->assertSame([], $subject->getReleases());
}
public function testHttpFailureIsNotCached(): void
{
$requestCount = 0;
$httpClient = new MockHttpClient(static function () use (&$requestCount): MockResponse {
++$requestCount;
return new MockResponse('', ['http_code' => 500]);
});
$subject = new GitHubReleasesService($httpClient, new ArrayAdapter());
$subject->getReleases();
$subject->getReleases();
$this->assertSame(2, $requestCount);
}
public function testGetReleasesSortsNewestFirst(): void
{
$body = json_encode([
[
'tag_name' => 'v0.7.0',
'name' => 'v0.7.0',
'published_at' => '2026-07-10T10:00:00Z',
'body' => 'Older release',
'html_url' => 'https://github.com/MarijnDoeve/TijdVoorDeTest/releases/tag/v0.7.0',
],
[
'tag_name' => 'v0.8.0',
'name' => 'v0.8.0',
'published_at' => '2026-07-12T10:00:00Z',
'body' => 'Newer release',
'html_url' => 'https://github.com/MarijnDoeve/TijdVoorDeTest/releases/tag/v0.8.0',
],
], \JSON_THROW_ON_ERROR);
$httpClient = new MockHttpClient([new MockResponse((string) $body, ['response_headers' => ['content-type' => 'application/json']])]);
$subject = new GitHubReleasesService($httpClient, new ArrayAdapter());
$releases = $subject->getReleases();
$this->assertSame('v0.8.0', $releases[0]['tagName']);
$this->assertSame('v0.7.0', $releases[1]['tagName']);
}
}