From 1bb7a7b447f771d7561562a5ffd2708959fb9d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Somogyi?= Date: Sun, 25 Aug 2024 06:32:18 +0200 Subject: [PATCH 1/2] refactor: ETagHeaderBuilder::resetETagWeek() -> resetWeekETag() (BC break) --- CHANGELOG.md | 1 + src/Builders/ETagHeaderBuilder.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd44269..dfd999a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## dev - enh: CacheHeaderBuilder::withoutMustRevalidate() added +- refactor: ETagHeaderBuilder::resetETagWeek() -> resetWeekETag() (BC break) ## 0.3.1 diff --git a/src/Builders/ETagHeaderBuilder.php b/src/Builders/ETagHeaderBuilder.php index 5a932a1..e24cffd 100644 --- a/src/Builders/ETagHeaderBuilder.php +++ b/src/Builders/ETagHeaderBuilder.php @@ -58,7 +58,7 @@ 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; @@ -66,7 +66,7 @@ public function resetETagWeek(): static public function withoutWeekETag(): static { - return (clone $this)->resetETagWeek(); + return (clone $this)->resetWeekETag(); } public function toHeaders(): array From 92211a0d59f3de06235d6f7b8dcedb665bcb2894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Somogyi?= Date: Sun, 25 Aug 2024 06:32:44 +0200 Subject: [PATCH 2/2] test: more test added --- tests/Builders/ETagHeaderBuilderTest.php | 69 ++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/tests/Builders/ETagHeaderBuilderTest.php b/tests/Builders/ETagHeaderBuilderTest.php index f6ff8b0..246e27b 100644 --- a/tests/Builders/ETagHeaderBuilderTest.php +++ b/tests/Builders/ETagHeaderBuilderTest.php @@ -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') . '"']], @@ -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); @@ -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()) @@ -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); + } } \ No newline at end of file