From e743f4419a3d1d546050f3170cce36ca4a382d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sat, 28 Dec 2024 12:39:54 +0100 Subject: [PATCH 1/3] add Parameter to be able to disable adding a revision --- src/View/Helper/RevisionHeadLink.php | 37 +++++++++++++++------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/View/Helper/RevisionHeadLink.php b/src/View/Helper/RevisionHeadLink.php index b36f2d3..6e95edd 100644 --- a/src/View/Helper/RevisionHeadLink.php +++ b/src/View/Helper/RevisionHeadLink.php @@ -63,6 +63,7 @@ public function appendPackage( bool $absolute = true, string $pathPrefix = '', bool $clearQuery = false, + bool $addRevision = true, ): self { $files = $this->minify->getPackageFiles($package); @@ -81,18 +82,18 @@ public function appendPackage( continue; } - if ($this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) { + if ($addRevision && $this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) { $uri = $this->minify->addRevision($uri); } $this->appendStylesheet( - $uri, - $media, - $conditionalStylesheet, - $extras, - $absolute, - $pathPrefix, - false, + href: $uri, + media: $media, + conditionalStylesheet: $conditionalStylesheet, + extras: $extras, + absolute: $absolute, + pathPrefix: $pathPrefix, + addRevision: false, ); } @@ -121,6 +122,7 @@ public function prependPackage( bool $absolute = true, string $pathPrefix = '', bool $clearQuery = false, + bool $addRevision = true, ): self { $files = $this->minify->getPackageFiles($package); @@ -139,18 +141,18 @@ public function prependPackage( continue; } - if ($this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) { + if ($addRevision && $this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) { $uri = $this->minify->addRevision($uri); } $this->prependStylesheet( - $uri, - $media, - $conditionalStylesheet, - $extras, - $absolute, - $pathPrefix, - false, + href: $uri, + media: $media, + conditionalStylesheet: $conditionalStylesheet, + extras: $extras, + absolute: $absolute, + pathPrefix: $pathPrefix, + addRevision: false, ); } @@ -181,6 +183,7 @@ public function listPackage( bool $absolute = true, string $pathPrefix = '', bool $clearQuery = false, + bool $addRevision = true, ): array { $files = $this->minify->getPackageFiles($package); @@ -201,7 +204,7 @@ public function listPackage( continue; } - if ($this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) { + if ($addRevision && $this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) { $uri = $this->minify->addRevision($uri); } From 5b4a496602bfd4e8ebcd4b56b9841f9920396218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sat, 28 Dec 2024 12:53:32 +0100 Subject: [PATCH 2/3] add tests --- phpstan.neon | 2 +- tests/View/Helper/RevisionHeadLinkTest.php | 132 +++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 9a79121..f4e03ec 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -97,7 +97,7 @@ parameters: tooWideThrowType: true cognitive_complexity: - class: 22 + class: 25 function: 9 type_coverage: diff --git a/tests/View/Helper/RevisionHeadLinkTest.php b/tests/View/Helper/RevisionHeadLinkTest.php index 92f14d8..aa68c12 100644 --- a/tests/View/Helper/RevisionHeadLinkTest.php +++ b/tests/View/Helper/RevisionHeadLinkTest.php @@ -200,6 +200,72 @@ public function testAppendPackage4(): void self::assertSame($object, $return); } + /** + * @throws InvalidArgumentException + * @throws BadMethodCallException + */ + public function testAppendPackage5(): void + { + $package = 'test-package'; + + $minify = $this->createMock(MinifyInterface::class); + $minify + ->expects(self::once()) + ->method('getPackageFiles') + ->with($package) + ->willReturn(['files' => ['abc.txt', '', 'bcd.txt']]); + $minify + ->expects(self::never()) + ->method('isItemOkToAddRevision'); + $minify + ->expects(self::never()) + ->method('addRevision'); + + $headLink = $this->createMock(AbstractStandalone::class); + $headLink + ->expects(self::once()) + ->method('__call') + ->with('appendStylesheet', ['https://www.test.de/abc_42.txt', 'screen', '!IE', []]); + + $renderer = $this->createMock(PhpRenderer::class); + $matcher = self::exactly(4); + $renderer + ->expects($matcher) + ->method('__call') + ->willReturnCallback( + static function (string $method, array $argv) use ($matcher, $headLink): string | AbstractStandalone { + match ($matcher->numberOfInvocations()) { + 3 => self::assertSame('serverUrl', $method), + 2 => self::assertSame('headLink', $method), + default => self::assertSame('baseUrl', $method), + }; + + match ($matcher->numberOfInvocations()) { + 1 => self::assertSame(['abc.txt', false, false], $argv), + 2 => self::assertSame([], $argv), + 3 => self::assertSame(['/abc.txt'], $argv), + default => self::assertSame(['bcd.txt', false, false], $argv), + }; + + return match ($matcher->numberOfInvocations()) { + 1 => '/abc.txt', + 2 => $headLink, + 3 => 'https://www.test.de/abc_42.txt', + default => '', + }; + }, + ); + $renderer + ->expects(self::never()) + ->method('plugin'); + + $object = new RevisionHeadLink($minify, $renderer); + + $return = $object->appendPackage($package, 'screen', '!IE', ['rel' => 'prev'], addRevision: false); + + self::assertSame($object, $return); + } + /** * @throws InvalidArgumentException * @throws BadMethodCallException @@ -474,6 +540,72 @@ public function testPrependPackage4(): void self::assertSame($object, $return); } + /** + * @throws InvalidArgumentException + * @throws BadMethodCallException + */ + public function testPrependPackage5(): void + { + $package = 'test-package'; + + $minify = $this->createMock(MinifyInterface::class); + $minify + ->expects(self::once()) + ->method('getPackageFiles') + ->with($package) + ->willReturn(['files' => ['abc.txt', '', 'bcd.txt']]); + $minify + ->expects(self::never()) + ->method('isItemOkToAddRevision'); + $minify + ->expects(self::never()) + ->method('addRevision'); + + $headLink = $this->createMock(AbstractStandalone::class); + $headLink + ->expects(self::once()) + ->method('__call') + ->with('prependStylesheet', ['https://www.test.de/abc_42.txt', 'screen', false, []]); + + $renderer = $this->createMock(PhpRenderer::class); + $matcher = self::exactly(4); + $renderer + ->expects($matcher) + ->method('__call') + ->willReturnCallback( + static function (string $method, array $argv) use ($matcher, $headLink): string | AbstractStandalone { + match ($matcher->numberOfInvocations()) { + 2 => self::assertSame('headLink', $method), + 3 => self::assertSame('serverUrl', $method), + default => self::assertSame('baseUrl', $method), + }; + + match ($matcher->numberOfInvocations()) { + 1 => self::assertSame(['bcd.txt', false, false], $argv), + 2 => self::assertSame([], $argv), + 3 => self::assertSame(['/abc.txt'], $argv), + default => self::assertSame(['abc.txt', false, false], $argv), + }; + + return match ($matcher->numberOfInvocations()) { + 1 => '/abc.txt', + 2 => $headLink, + 3 => 'https://www.test.de/abc_42.txt', + default => '', + }; + }, + ); + $renderer + ->expects(self::never()) + ->method('plugin'); + + $object = new RevisionHeadLink($minify, $renderer); + + $return = $object->prependPackage($package, addRevision: false); + + self::assertSame($object, $return); + } + /** * @throws InvalidArgumentException * @throws BadMethodCallException From 40d15299e0bd6ffd2beb2a865876d31e25dffe56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sat, 28 Dec 2024 12:58:03 +0100 Subject: [PATCH 3/3] fix issue --- tests/View/Helper/RevisionHeadLinkTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/View/Helper/RevisionHeadLinkTest.php b/tests/View/Helper/RevisionHeadLinkTest.php index aa68c12..fb83c4c 100644 --- a/tests/View/Helper/RevisionHeadLinkTest.php +++ b/tests/View/Helper/RevisionHeadLinkTest.php @@ -261,7 +261,13 @@ static function (string $method, array $argv) use ($matcher, $headLink): string $object = new RevisionHeadLink($minify, $renderer); - $return = $object->appendPackage($package, 'screen', '!IE', ['rel' => 'prev'], addRevision: false); + $return = $object->appendPackage( + $package, + 'screen', + '!IE', + ['rel' => 'prev'], + addRevision: false, + ); self::assertSame($object, $return); }