Skip to content

Commit

Permalink
Merge pull request #407 from jasperweyne/feature/testable-caching
Browse files Browse the repository at this point in the history
chore: make caching testable
  • Loading branch information
A-Daneel committed Sep 21, 2024
2 parents c16e0a0 + 35035ad commit f76dbbb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
run: "composer check-platform-reqs --working-dir ./kiwi --lock --no-dev --no-ansi 2>&1 | sed '1,1d' | tr '\n' ',' | sed 's/.$//' | jq -R 'split(\",\") | map(. | gsub(\" +\"; \" \") | split(\" \") | { key: .[0], value: .[1] }) | from_entries' > requirements.json"

- name: Upload requirements file
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
if: github.ref == 'refs/heads/master'
with:
name: requirements
Expand Down Expand Up @@ -101,7 +101,7 @@ jobs:
run: zip -qq -r prod.zip kiwi public_html

- name: Upload build
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
if: github.ref == 'refs/heads/master'
with:
name: prod
Expand Down Expand Up @@ -132,13 +132,13 @@ jobs:
DATE: ${{ steps.author-date.outputs.result }}

- name: Download build
uses: actions/download-artifact@v1
uses: actions/download-artifact@v4
with:
name: prod
path: .

- name: Download requirements
uses: actions/download-artifact@v1
uses: actions/download-artifact@v4
with:
name: requirements
path: .
Expand Down
23 changes: 11 additions & 12 deletions src/Template/UpdateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ class UpdateChecker

public function __construct(
KernelInterface $kernel,
?HttpClientInterface $httpClient = null
) {
if (null === $httpClient) {
if ('test' === $kernel->getEnvironment()) {
throw new \InvalidArgumentException('An explicit HttpClient must be provided during testing.');
}
$this->client = new CachingHttpClient(
HttpClient::create(),
new Store("{$kernel->getCacheDir()}/releases")
);
} else {
$this->client = $httpClient;
}
$this->client = new CachingHttpClient(
HttpClient::create(),
new Store("{$kernel->getCacheDir()}/releases")
);
}

public function setHttpClient(HttpClientInterface $httpClient): self
{
$this->client = $httpClient;

return $this;
}

public function newestVersion(): string
Expand Down
18 changes: 4 additions & 14 deletions tests/Unit/Template/UpdateCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function testNewestVersion(): void

$httpClient = new MockHttpClient($mockResponse);

$updateChecker = new UpdateChecker($kernel, $httpClient);
$newestVersion = $updateChecker->newestVersion();
$updateChecker = new UpdateChecker($kernel);
$newestVersion = $updateChecker->setHttpClient($httpClient)->newestVersion();
self::assertEquals($expectedVersion, $newestVersion);
}

Expand All @@ -50,18 +50,8 @@ public function testNewestVersionRateLimited(): void

$httpClient = new MockHttpClient($mockResponse);

$updateChecker = new UpdateChecker($kernel, $httpClient);
$newestVersion = $updateChecker->newestVersion();
$updateChecker = new UpdateChecker($kernel);
$newestVersion = $updateChecker->setHttpClient($httpClient)->newestVersion();
self::assertEquals($expectedVersion, $newestVersion);
}

public function testNewestVersionException(): void
{
$kernel = self::bootKernel();

self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage('An explicit HttpClient must be provided during testing.');

new UpdateChecker($kernel);
}
}

0 comments on commit f76dbbb

Please sign in to comment.