Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Mar 30, 2024
1 parent 30c19d2 commit 542ccf7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/services/CacheRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,9 @@ public function getCachedResponse(SiteUriModel $siteUri): ?Response
}

$response = $event->response;
$response->content = $content;
$this->addCraftHeaders($response);
$this->prepareResponse($response, $siteUri, $content, $encoded);
$this->prepareResponse($response, $siteUri, $encoded);
$this->appendServedByComment($response, $siteUri, $encoded);

if ($this->hasEventHandlers(self::EVENT_AFTER_GET_RESPONSE)) {
Expand Down Expand Up @@ -502,7 +503,8 @@ public function saveAndPrepareResponse(?Response $response, SiteUriModel $siteUr
$content = Blitz::$plugin->generateCache->save($response->content, $siteUri);

if ($content) {
$this->prepareResponse($response, $siteUri, $content);
$response->content = $content;
$this->prepareResponse($response, $siteUri);
}
}

Expand Down Expand Up @@ -668,10 +670,8 @@ private function addCraftHeaders(Response $response): void
*
* @since 3.12.0
*/
private function prepareResponse(Response $response, SiteUriModel $siteUri, string $content, bool $encoded = false): void
private function prepareResponse(Response $response, SiteUriModel $siteUri, bool $encoded = false): void
{
$response->content = $content;

$cacheControlHeader = Blitz::$plugin->settings->cacheControlHeader;

if ($this->getIsExpiredSiteUri($siteUri)) {
Expand Down
18 changes: 11 additions & 7 deletions tests/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ This document outlines the test specification for the Blitz plugin.

---

## Feature Tests

### [GenerateCache](pest/Feature/GenerateCacheTest.php)

_Tests the saving of cached values, element cache records and element query records._

![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Cached value is saved with output comments.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Cached value is saved without output comments.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Cached value is saved with output comments when file extension is ".html".
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Cached value is saved without output comments when file extension is not `.html`.

## Interface Tests

### [WebResponse](pest/Interface/WebResponseTest.php)

_Tests that cached web responses contain the correct headers and comments._

![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response contains the default cache control header when the page is not cacheable.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response contains the cache control header when the page is cacheable.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response contains the expired cache control header and the cache is refreshed when the page is expired.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response adds the powered by header.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response contains output comments when enabled.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response does not contain output comments when disabled.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response with mime type has headers and does not contain output comments.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response is encoded when compression is enabled.
35 changes: 19 additions & 16 deletions tests/pest/Interface/WebResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

beforeEach(function() {
Blitz::$plugin->cacheStorage->deleteAll();
Blitz::$plugin->generateCache->options->outputComments = null;
Blitz::$plugin->set('refreshCache', Mockery::mock(RefreshCacheService::class . '[refresh]'));
});

Expand Down Expand Up @@ -60,25 +61,27 @@
->toContainEqual('Blitz');
});

test('Response contains output comments when enabled', function() {
foreach ([true, SettingsModel::OUTPUT_COMMENTS_SERVED] as $value) {
Blitz::$plugin->settings->outputComments = $value;
$response = sendRequest();
test('Response contains output comments when enabled', function(bool|int $value) {
Blitz::$plugin->settings->outputComments = $value;
$response = sendRequest();

expect($response->content)
->toContain('Cached by Blitz');
}
});
expect($response->content)
->toContain('Cached by Blitz');
})->with([
'true' => true,
'SettingsModel::OUTPUT_COMMENTS_CACHED' => SettingsModel::OUTPUT_COMMENTS_CACHED,
]);

test('Response does not contain output comments when disabled', function() {
foreach ([false, SettingsModel::OUTPUT_COMMENTS_CACHED] as $value) {
Blitz::$plugin->settings->outputComments = $value;
$response = sendRequest();
test('Response does not contain output comments when disabled', function(bool|int $value) {
Blitz::$plugin->settings->outputComments = $value;
$response = sendRequest();

expect($response->content)
->not()->toContain('Served by Blitz on');
}
});
expect($response->content)
->not()->toContain('Cached by Blitz');
})->with([
'false' => false,
'SettingsModel::OUTPUT_COMMENTS_SERVED' => SettingsModel::OUTPUT_COMMENTS_SERVED,
]);

test('Response with mime type has headers and does not contain output comments', function() {
$output = createOutput();
Expand Down

0 comments on commit 542ccf7

Please sign in to comment.