Skip to content

Commit

Permalink
Merge pull request #9 from smartondev/more-tests
Browse files Browse the repository at this point in the history
More tests, fix matchers header value adding/setting, remove ETagMatcherResult::matches()
  • Loading branch information
kamarton authored Aug 28, 2024
2 parents 0e6790d + 934028c commit 05def78
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 49 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## dev

- fix: ModifiedMatcher::ifModifiedSinceHeader(), ::withIfModifiedSinceHeader(), ::ifUnmodifiedSinceHeader(),
::withIfUnmodifiedSinceHeader() invalid header value addition
- fix: ETagMatcher::ifMatchHeader(), ::withIfMatchHeader(), ::ifNoneMatchHeader(), ::withIfNoneMatchHeader() invalid
header value addition
- remove: ETagMatcherResult::matches() removed (BC break)

## 0.4.0

Expand Down
4 changes: 2 additions & 2 deletions src/Matchers/ETagMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ETagMatcher extends MatcherHeaderAbstract

public function ifMatchHeader(string|array $ifMatch): static
{
$this->headers = HttpHeaderHelper::replaceHeaders($this->headers, [self::IF_MATCH_HEADER, $ifMatch]);
$this->headers = HttpHeaderHelper::replaceHeaders($this->headers, [self::IF_MATCH_HEADER => $ifMatch]);
return $this;
}

Expand All @@ -22,7 +22,7 @@ public function withIfMatchHeader(string|array $ifMatch): static

public function ifNoneMatchHeaderValue(string|array $ifNoneMatch): static
{
$this->headers = HttpHeaderHelper::replaceHeaders($this->headers, [self::IF_NONE_MATCH_HEADER, $ifNoneMatch]);
$this->headers = HttpHeaderHelper::replaceHeaders($this->headers, [self::IF_NONE_MATCH_HEADER => $ifNoneMatch]);
return $this;
}

Expand Down
5 changes: 0 additions & 5 deletions src/Matchers/ETagMatcherResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ public function notMatchesIfNoneMatchHeader(): bool
{
return !$this->matchesIfNoneMatchHeader;
}

public function matches(): bool
{
return $this->matchesIfMatchHeader || $this->matchesIfNoneMatchHeader;
}
}
20 changes: 10 additions & 10 deletions src/Matchers/ModifiedMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ class ModifiedMatcher extends MatcherHeaderAbstract
private const IF_MODIFIED_SINCE_HEADER = 'if-modified-since';
private const IF_UNMODIFIED_SINCE_HEADER = 'if-unmodified-since';

public function ifModifiedSinceHeader(string|array $ifModifiedSince): static
public function ifModifiedSinceHeader(string|array $ifModifiedSinceHeaderValue): static
{
$this->headers = HttpHeaderHelper::replaceHeaders($this->headers, [self::IF_MODIFIED_SINCE_HEADER, $ifModifiedSince]);
$this->headers = HttpHeaderHelper::replaceHeaders($this->headers, [self::IF_MODIFIED_SINCE_HEADER => $ifModifiedSinceHeaderValue]);
return $this;
}

public function withIfModifiedSinceHeader(string|array $ifModifiedSince): static
public function withIfModifiedSinceHeader(string|array $ifModifiedSinceHeaderValue): static
{
return (clone $this)->ifModifiedSinceHeader($ifModifiedSince);
return (clone $this)->ifModifiedSinceHeader($ifModifiedSinceHeaderValue);
}

public function ifUnmodifiedSinceHeader(string|array $ifUnmodifiedSince): static
public function ifUnmodifiedSinceHeader(string|array $ifUnmodifiedSinceHeaderValue): static
{
$this->headers = HttpHeaderHelper::replaceHeaders($this->headers, [self::IF_UNMODIFIED_SINCE_HEADER, $ifUnmodifiedSince]);
$this->headers = HttpHeaderHelper::replaceHeaders($this->headers, [self::IF_UNMODIFIED_SINCE_HEADER => $ifUnmodifiedSinceHeaderValue]);
return $this;
}

public function withIfUnmodifiedSinceHeader(string|array $ifUnmodifiedSince): static
public function withIfUnmodifiedSinceHeader(string|array $ifUnmodifiedSinceHeaderValue): static
{
return (clone $this)->ifUnmodifiedSinceHeader($ifUnmodifiedSince);
return (clone $this)->ifUnmodifiedSinceHeader($ifUnmodifiedSinceHeaderValue);
}

public function getIfModifiedSinceHeader(): ?string
Expand Down Expand Up @@ -62,7 +62,7 @@ public function getIfModifiedSinceHeaderAsTimestamp(): ?int
return null;
}
if (!$this->isValidIfModifiedSinceHeader()) {
throw new \RuntimeException('Invalid If-Modified-Since header');
throw new \RuntimeException('Invalid If-Modified-Since header value');
}
return TimeHelper::toTimestamp($this->getIfModifiedSinceHeader());
}
Expand All @@ -83,7 +83,7 @@ public function getIfUnmodifiedSinceHeaderAsTimestamp(): ?int
return null;
}
if (!$this->isValidIfUnmodifiedSinceHeader()) {
throw new \RuntimeException('Invalid If-Unmodified-Since header');
throw new \RuntimeException('Invalid If-Unmodified-Since header value');
}
return TimeHelper::toTimestamp($this->getIfUnmodifiedSinceHeader());
}
Expand Down
79 changes: 47 additions & 32 deletions tests/Matchers/ETagMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,36 @@ public function testIsMatchWithNullEtag(): void
{
$ETagCondition = (new ETagMatcher())
->withHeaders(['If-Match' => '"123456"']);
$this->assertFalse(
$ETagCondition
->matches(null)
->matchesIfMatchHeader()
);
$matches = $ETagCondition->matches(null);
$this->assertFalse($matches->matchesIfMatchHeader());
$this->assertTrue($matches->notMatchesIfMatchHeader());
}

public function testIsNoneMatchWithNullEtag(): void
{
$ETagCondition = (new ETagMatcher())
->withHeaders(['If-None-Match' => '"123456"']);
$this->assertFalse(
$ETagCondition
->matches(null)
->matchesIfNoneMatchHeader()
);
$matches = $ETagCondition->matches(null);
$this->assertFalse($matches->matchesIfNoneMatchHeader());
$this->assertTrue($matches->notMatchesIfNoneMatchHeader());
}

public function testIsNoneMatchFail(): void
{
$ETagCondition = (new ETagMatcher())
->withHeaders(['If-None-Match' => '"123456"']);
$this->assertFalse(
$ETagCondition
->matches('"1234567"')
->matchesIfNoneMatchHeader()
);
$matches = $ETagCondition->matches('"1234567"');
$this->assertFalse($matches->matchesIfNoneMatchHeader());
$this->assertTrue($matches->notMatchesIfNoneMatchHeader());
}

public function testIsMatchFail(): void
{
$ETagCondition = (new ETagMatcher())
->withHeaders(['If-Match' => '"123456"']);
$this->assertFalse(
$ETagCondition
->matches('"1234567"')
->matchesIfMatchHeader()
);
$matches = $ETagCondition->matches('"1234567"');
$this->assertFalse($matches->matchesIfMatchHeader());
$this->assertTrue($matches->notMatchesIfMatchHeader());
}

public function testMatchWithETagHeaderBuilder(): void
Expand All @@ -81,17 +73,40 @@ public function testMatchWithETagHeaderBuilder(): void
->withWeekEtag();
$etag = $ETagHeaderBuilder->getETag();
$ETagCondition = new ETagMatcher();
$this->assertTrue(
$ETagCondition
->withHeaders(['If-Match' => $etag])
->matches($etag)
->matchesIfMatchHeader()
);
$this->assertTrue(
$ETagCondition
->withHeaders(['If-None-Match' => $etag])
->matches($etag)
->matchesIfNoneMatchHeader()
);
$matchesIfMatch = $ETagCondition
->withHeaders(['If-Match' => $etag])
->matches($etag);
$this->assertTrue($matchesIfMatch->matchesIfMatchHeader());
$this->assertFalse($matchesIfMatch->notMatchesIfMatchHeader());

$matchesIfNoneMatch = $ETagCondition
->withHeaders(['If-None-Match' => $etag])
->matches($etag);
$this->assertTrue($matchesIfNoneMatch->matchesIfNoneMatchHeader());
$this->assertFalse($matchesIfNoneMatch->notMatchesIfNoneMatchHeader());
}

public function testIfMatchHeader(): void
{
$matcher = new ETagMatcher();
$matcher2 = $matcher->withIfMatchHeader('"123"');
$this->assertFalse($matcher->hasIfMatchHeader());
$this->assertTrue($matcher2->hasIfMatchHeader());
$this->assertSame('"123"', $matcher2->getIfMatchHeader());
$matcher->ifMatchHeader('"456"');
$this->assertTrue($matcher->hasIfMatchHeader());
$this->assertSame('"456"', $matcher->getIfMatchHeader());
}

public function testIfNoneMatchHeader(): void
{
$matcher = new ETagMatcher();
$matcher2 = $matcher->withIfNoneMatchHeaderValue('"123"');
$this->assertFalse($matcher->hasIfNoneMatchHeader());
$this->assertTrue($matcher2->hasIfNoneMatchHeader());
$this->assertSame('"123"', $matcher2->getIfNoneMatchHeader());
$matcher->ifNoneMatchHeaderValue('"456"');
$this->assertTrue($matcher->hasIfNoneMatchHeader());
$this->assertSame('"456"', $matcher->getIfNoneMatchHeader());
}
}
46 changes: 46 additions & 0 deletions tests/Matchers/ModifiedMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ public function testHeaderAsTimestamp(array $headers,
$this->assertSame($expectedIfUnmodifiedSinceAsTimestamp, $matcher->getIfUnmodifiedSinceHeaderAsTimestamp());
}

public function testGetModifiedSinceHeaderAsTimestampInvalidValue(): void
{
$matcher = (new ModifiedMatcher())->headers([
'If-Modified-Since' => 'apple',
]);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid If-Modified-Since header value');
$matcher->getIfModifiedSinceHeaderAsTimestamp();
}

public function testGetUnmodifiedSinceHeaderAsTimestampInvalidValue(): void
{
$matcher = (new ModifiedMatcher())->headers([
'If-Unmodified-Since' => 'apple',
]);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid If-Unmodified-Since header value');
$matcher->getIfUnmodifiedSinceHeaderAsTimestamp();
}

public function testMatchesIfModifiedSince(): void
{
$matcher = (new ModifiedMatcher())->headers([
Expand Down Expand Up @@ -96,4 +118,28 @@ public function testMatchesIfUnmodifiedSince(): void
$this->assertFalse($matcher->matches($dtAfter)->isUnmodifiedSince());
$this->assertFalse($matcher->matches($dtEq)->isModifiedSince());
}

public function testIfModifiedSinceHeader(): void
{
$matcher = new ModifiedMatcher();
$matcher2 = $matcher->withIfModifiedSinceHeader('Tue, 15 Nov 1994 12:45:26 GMT');
$this->assertFalse($matcher->hasIfModifiedSinceHeader());
$this->assertTrue($matcher2->hasIfModifiedSinceHeader());
$this->assertSame('Tue, 15 Nov 1994 12:45:26 GMT', $matcher2->getIfModifiedSinceHeader());
$matcher->ifModifiedSinceHeader('Wed, 16 Nov 1994 12:45:26 GMT');
$this->assertTrue($matcher->hasIfModifiedSinceHeader());
$this->assertSame('Wed, 16 Nov 1994 12:45:26 GMT', $matcher->getIfModifiedSinceHeader());
}

public function testIfUnmodifiedSinceHeader(): void
{
$matcher = new ModifiedMatcher();
$matcher2 = $matcher->withIfUnmodifiedSinceHeader('Tue, 15 Nov 1994 12:45:26 GMT');
$this->assertFalse($matcher->hasIfUnmodifiedSinceHeader());
$this->assertTrue($matcher2->hasIfUnmodifiedSinceHeader());
$this->assertSame('Tue, 15 Nov 1994 12:45:26 GMT', $matcher2->getIfUnmodifiedSinceHeader());
$matcher->ifUnmodifiedSinceHeader('Wed, 16 Nov 1994 12:45:26 GMT');
$this->assertTrue($matcher->hasIfUnmodifiedSinceHeader());
$this->assertSame('Wed, 16 Nov 1994 12:45:26 GMT', $matcher->getIfUnmodifiedSinceHeader());
}
}

0 comments on commit 05def78

Please sign in to comment.