Skip to content

Commit

Permalink
enh: more php pest tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
kamarton committed Jan 6, 2025
1 parent 1cb2cfa commit 605d045
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
38 changes: 38 additions & 0 deletions tests/Builders/CacheHeaderBuilderPestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,41 @@
'max age' => [(new CacheHeaderBuilder())->withMaxAge(3600)],
'last modified' => [(new CacheHeaderBuilder())->withLastModified(1)],
]);

it('mixed', function (CacheHeaderBuilder $builder, array $expectedHeaders) {
foreach ($builder->toHeaders() as $name => $value) {
expect($expectedHeaders[$name])
->toEqualCanonicalizing(preg_split('!\s*,\s*!', $value));
}
})->with([
'public maxage=3600' => [
(new CacheHeaderBuilder())
->withPublic()
->withMaxAge(3600),
['cache-control' => ['public', 'max-age=3600']],
],
'public notransform smaxage=100 maxage=200' => [
(new CacheHeaderBuilder())
->withPublic()
->withNoTransform()
->withSharedMaxAge(100)
->withMaxAge(200),
['cache-control' => ['public', 'no-transform', 's-maxage=100', 'max-age=200']],
],
'mustrevalidate nostore' => [
(new CacheHeaderBuilder())
->withMustRevalidate()
->withNoStore(),
['cache-control' => ['must-revalidate', 'no-store']],
],
'public notransform smaxage=100 maxage=200 stalewhile=300 staleiferror=400' => [
(new CacheHeaderBuilder())
->withPublic()
->withNoTransform()
->withSharedMaxAge(100)
->withMaxAge(200)
->withStaleWhileRevalidate(300)
->withStaleIfError(400),
['cache-control' => ['public', 'no-transform', 's-maxage=100', 'max-age=200', 'stale-while-revalidate=300', 'stale-if-error=400']],
],
]);
86 changes: 84 additions & 2 deletions tests/Matchers/ETagMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use SmartonDev\HttpCache\Matchers\ETagMatcher;

it('if match header', function (array $headers, ?string $etag, bool $expected) {
it('if match header matches', function (array $headers, ?string $etag, bool $expected) {
$matcher = (new ETagMatcher())
->headers($headers);
expect($matcher->matches($etag)->matchesIfMatchHeader())->toBe($expected)
Expand Down Expand Up @@ -42,7 +42,7 @@
],
]);

it('if none match header', function (array $headers, ?string $etag, bool $expected) {
it('if none match header matches', function (array $headers, ?string $etag, bool $expected) {
$matcher = (new ETagMatcher())
->headers($headers);
expect($matcher->matches($etag)->matchesIfNoneMatchHeader())->toBe($expected)
Expand Down Expand Up @@ -79,3 +79,85 @@
false,
],
]);

it('has if match header', function (array $headers, bool $expected) {
$matcher = (new ETagMatcher())
->headers($headers);
expect($matcher->hasIfMatchHeader())->toBe($expected);
})->with([
'has' => [
['If-Match' => '"123"'],
true,
],
'has empty' => [
['If-Match' => ''],
true,
],
'empty' => [
[],
false,
],
'another header' => [
['x-foo' => '"123"'],
false,
],
]);

it('if has none match header', function (array $headers, bool $expected) {
$matcher = (new ETagMatcher())
->headers($headers);
expect($matcher->hasIfNoneMatchHeader())->toBe($expected);
})->with([
'has' => [
['If-None-Match' => '"123"'],
true,
],
'has empty' => [
['If-None-Match' => ''],
true,
],
'empty' => [
[],
false,
],
'another header' => [
['x-foo' => '"123"'],
false,
],
]);

it('if match header', function (string $ifMatchHeader, ?string $etag, bool $expected) {
$matcher = (new ETagMatcher())
->ifMatchHeaderValue($ifMatchHeader);
expect($matcher->matches($etag)->matchesIfMatchHeader())->toBe($expected)
->and($matcher->matches($etag)->notMatchesIfMatchHeader())->toBe(!$expected);
})->with([
'match' => [
'"123"',
'"123"',
true,
],
'not match' => [
'"123"',
'"456"',
false,
],
]);

it('if none match header', function (string $ifNoneMatchHeader, ?string $etag, bool $expected) {
$matcher = (new ETagMatcher())
->ifNoneMatchHeaderValue($ifNoneMatchHeader);
expect($matcher->matches($etag)->matchesIfNoneMatchHeader())->toBe($expected)
->and($matcher->matches($etag)->notMatchesIfNoneMatchHeader())->toBe(!$expected);
})->with([
'match' => [
'"123"',
'"123"',
true,
],
'not match' => [
'"123"',
'"456"',
false,
],
]);

0 comments on commit 605d045

Please sign in to comment.