Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix method name and more test #7

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## dev

- enh: CacheHeaderBuilder::withoutMustRevalidate() added
- refactor: ETagHeaderBuilder::resetETagWeek() -> resetWeekETag() (BC break)

## 0.3.1

Expand Down
4 changes: 2 additions & 2 deletions src/Builders/ETagHeaderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public function withWeekETag(bool $week = true): static
return (clone $this)->weekETag($week);
}

public function resetETagWeek(): static
public function resetWeekETag(): static
{
$this->weekETag = false;
return $this;
}

public function withoutWeekETag(): static
{
return (clone $this)->resetETagWeek();
return (clone $this)->resetWeekETag();
}

public function toHeaders(): array
Expand Down
69 changes: 66 additions & 3 deletions tests/Builders/ETagHeaderBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testWithETag(string $etag, array $expectedHeaders, array $expect
$this->assertSame($expectedHeaders, $builder->toHeaders());
}

public static function dataProviderWithComputedEtag()
public static function dataProviderComputedEtag(): array
{
return [
['content123', 'md5', false, ['etag' => '"' . md5('content123') . '"']],
Expand All @@ -44,8 +44,8 @@ public static function dataProviderWithComputedEtag()
];
}

#[DataProvider('dataProviderWithComputedEtag')]
public function testWithComputedEtag(mixed $data, callable $func, bool $weekEtag, array $expectedHeaders): void
#[DataProvider('dataProviderComputedEtag')]
public function testComputedEtag(mixed $data, callable $func, bool $weekEtag, array $expectedHeaders): void
{
$builder = (new ETagHeaderBuilder())
->computedETag($data, $func);
Expand All @@ -55,6 +55,17 @@ public function testWithComputedEtag(mixed $data, callable $func, bool $weekEtag
$this->assertSame($expectedHeaders, $builder->toHeaders());
}

#[DataProvider('dataProviderComputedEtag')]
public function testWithComputedEtag(mixed $data, callable $func, bool $weekEtag, array $expectedHeaders): void
{
$builder = (new ETagHeaderBuilder())
->withComputedETag($data, $func);
if ($weekEtag) {
$builder = $builder->withWeekETag();
}
$this->assertSame($expectedHeaders, $builder->toHeaders());
}

public function testEmptyETag(): void
{
$builder = (new ETagHeaderBuilder())
Expand All @@ -65,4 +76,56 @@ public function testEmptyETag(): void
->etag(' ');
$this->assertNull($builder->getETag());
}

public function testIsNotEmptyETag(): void
{
$builder = (new ETagHeaderBuilder())
->etag('123456');
$this->assertTrue($builder->isNotEmpty());

$builder = (new ETagHeaderBuilder())
->etag(' ');
$this->assertFalse($builder->isNotEmpty());
}

public function testResetETag(): void
{
$builder = (new ETagHeaderBuilder())
->etag('123456');
$this->assertTrue($builder->isNotEmpty());
$this->assertFalse($builder->withoutETag()->isNotEmpty());
$this->assertTrue($builder->isNotEmpty());
$builder->resetETag();
$this->assertFalse($builder->isNotEmpty());
}

public function testResetWeekETag(): void
{
$builder = (new ETagHeaderBuilder())
->etag('123456')
->weekETag();
$this->assertSame('W/"123456"', $builder->getETag());
$this->assertSame('"123456"', $builder->withoutWeekETag()->getETag());
$this->assertSame('W/"123456"', $builder->getETag());
$builder->resetWeekETag();
$this->assertSame('"123456"', $builder->getETag());
}

public function testEmpty(): void
{
$builder = new ETagHeaderBuilder();
$this->assertSame([], $builder->toHeaders());
}

public function testToString(): void
{
$builder = (new ETagHeaderBuilder())
->etag('123456');
$this->assertSame('"123456"', (string)$builder);
$this->assertSame($builder->getETag(), (string)$builder);

$builder->weekETag();
$this->assertSame('W/"123456"', (string)$builder);
$this->assertSame($builder->getETag(), (string)$builder);
}
}