Skip to content

Commit a5fdf70

Browse files
authored
Merge pull request #181 from mimmi20/updates
add Parameter to be able to disable adding a revision
2 parents d410af6 + 40d1529 commit a5fdf70

File tree

3 files changed

+159
-18
lines changed

3 files changed

+159
-18
lines changed

Diff for: phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ parameters:
9797
tooWideThrowType: true
9898

9999
cognitive_complexity:
100-
class: 22
100+
class: 25
101101
function: 9
102102

103103
type_coverage:

Diff for: src/View/Helper/RevisionHeadLink.php

+20-17
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function appendPackage(
6363
bool $absolute = true,
6464
string $pathPrefix = '',
6565
bool $clearQuery = false,
66+
bool $addRevision = true,
6667
): self {
6768
$files = $this->minify->getPackageFiles($package);
6869

@@ -81,18 +82,18 @@ public function appendPackage(
8182
continue;
8283
}
8384

84-
if ($this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) {
85+
if ($addRevision && $this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) {
8586
$uri = $this->minify->addRevision($uri);
8687
}
8788

8889
$this->appendStylesheet(
89-
$uri,
90-
$media,
91-
$conditionalStylesheet,
92-
$extras,
93-
$absolute,
94-
$pathPrefix,
95-
false,
90+
href: $uri,
91+
media: $media,
92+
conditionalStylesheet: $conditionalStylesheet,
93+
extras: $extras,
94+
absolute: $absolute,
95+
pathPrefix: $pathPrefix,
96+
addRevision: false,
9697
);
9798
}
9899

@@ -121,6 +122,7 @@ public function prependPackage(
121122
bool $absolute = true,
122123
string $pathPrefix = '',
123124
bool $clearQuery = false,
125+
bool $addRevision = true,
124126
): self {
125127
$files = $this->minify->getPackageFiles($package);
126128

@@ -139,18 +141,18 @@ public function prependPackage(
139141
continue;
140142
}
141143

142-
if ($this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) {
144+
if ($addRevision && $this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) {
143145
$uri = $this->minify->addRevision($uri);
144146
}
145147

146148
$this->prependStylesheet(
147-
$uri,
148-
$media,
149-
$conditionalStylesheet,
150-
$extras,
151-
$absolute,
152-
$pathPrefix,
153-
false,
149+
href: $uri,
150+
media: $media,
151+
conditionalStylesheet: $conditionalStylesheet,
152+
extras: $extras,
153+
absolute: $absolute,
154+
pathPrefix: $pathPrefix,
155+
addRevision: false,
154156
);
155157
}
156158

@@ -181,6 +183,7 @@ public function listPackage(
181183
bool $absolute = true,
182184
string $pathPrefix = '',
183185
bool $clearQuery = false,
186+
bool $addRevision = true,
184187
): array {
185188
$files = $this->minify->getPackageFiles($package);
186189

@@ -201,7 +204,7 @@ public function listPackage(
201204
continue;
202205
}
203206

204-
if ($this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) {
207+
if ($addRevision && $this->minify->isItemOkToAddRevision(Minify::FILETYPE_CSS, $uri)) {
205208
$uri = $this->minify->addRevision($uri);
206209
}
207210

Diff for: tests/View/Helper/RevisionHeadLinkTest.php

+138
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,78 @@ public function testAppendPackage4(): void
200200
self::assertSame($object, $return);
201201
}
202202

203+
/**
204+
* @throws InvalidArgumentException
205+
* @throws BadMethodCallException
206+
*/
207+
public function testAppendPackage5(): void
208+
{
209+
$package = 'test-package';
210+
211+
$minify = $this->createMock(MinifyInterface::class);
212+
$minify
213+
->expects(self::once())
214+
->method('getPackageFiles')
215+
->with($package)
216+
->willReturn(['files' => ['abc.txt', '', 'bcd.txt']]);
217+
$minify
218+
->expects(self::never())
219+
->method('isItemOkToAddRevision');
220+
$minify
221+
->expects(self::never())
222+
->method('addRevision');
223+
224+
$headLink = $this->createMock(AbstractStandalone::class);
225+
$headLink
226+
->expects(self::once())
227+
->method('__call')
228+
->with('appendStylesheet', ['https://www.test.de/abc_42.txt', 'screen', '!IE', []]);
229+
230+
$renderer = $this->createMock(PhpRenderer::class);
231+
$matcher = self::exactly(4);
232+
$renderer
233+
->expects($matcher)
234+
->method('__call')
235+
->willReturnCallback(
236+
static function (string $method, array $argv) use ($matcher, $headLink): string | AbstractStandalone {
237+
match ($matcher->numberOfInvocations()) {
238+
3 => self::assertSame('serverUrl', $method),
239+
2 => self::assertSame('headLink', $method),
240+
default => self::assertSame('baseUrl', $method),
241+
};
242+
243+
match ($matcher->numberOfInvocations()) {
244+
1 => self::assertSame(['abc.txt', false, false], $argv),
245+
2 => self::assertSame([], $argv),
246+
3 => self::assertSame(['/abc.txt'], $argv),
247+
default => self::assertSame(['bcd.txt', false, false], $argv),
248+
};
249+
250+
return match ($matcher->numberOfInvocations()) {
251+
1 => '/abc.txt',
252+
2 => $headLink,
253+
3 => 'https://www.test.de/abc_42.txt',
254+
default => '',
255+
};
256+
},
257+
);
258+
$renderer
259+
->expects(self::never())
260+
->method('plugin');
261+
262+
$object = new RevisionHeadLink($minify, $renderer);
263+
264+
$return = $object->appendPackage(
265+
$package,
266+
'screen',
267+
'!IE',
268+
['rel' => 'prev'],
269+
addRevision: false,
270+
);
271+
272+
self::assertSame($object, $return);
273+
}
274+
203275
/**
204276
* @throws InvalidArgumentException
205277
* @throws BadMethodCallException
@@ -474,6 +546,72 @@ public function testPrependPackage4(): void
474546
self::assertSame($object, $return);
475547
}
476548

549+
/**
550+
* @throws InvalidArgumentException
551+
* @throws BadMethodCallException
552+
*/
553+
public function testPrependPackage5(): void
554+
{
555+
$package = 'test-package';
556+
557+
$minify = $this->createMock(MinifyInterface::class);
558+
$minify
559+
->expects(self::once())
560+
->method('getPackageFiles')
561+
->with($package)
562+
->willReturn(['files' => ['abc.txt', '', 'bcd.txt']]);
563+
$minify
564+
->expects(self::never())
565+
->method('isItemOkToAddRevision');
566+
$minify
567+
->expects(self::never())
568+
->method('addRevision');
569+
570+
$headLink = $this->createMock(AbstractStandalone::class);
571+
$headLink
572+
->expects(self::once())
573+
->method('__call')
574+
->with('prependStylesheet', ['https://www.test.de/abc_42.txt', 'screen', false, []]);
575+
576+
$renderer = $this->createMock(PhpRenderer::class);
577+
$matcher = self::exactly(4);
578+
$renderer
579+
->expects($matcher)
580+
->method('__call')
581+
->willReturnCallback(
582+
static function (string $method, array $argv) use ($matcher, $headLink): string | AbstractStandalone {
583+
match ($matcher->numberOfInvocations()) {
584+
2 => self::assertSame('headLink', $method),
585+
3 => self::assertSame('serverUrl', $method),
586+
default => self::assertSame('baseUrl', $method),
587+
};
588+
589+
match ($matcher->numberOfInvocations()) {
590+
1 => self::assertSame(['bcd.txt', false, false], $argv),
591+
2 => self::assertSame([], $argv),
592+
3 => self::assertSame(['/abc.txt'], $argv),
593+
default => self::assertSame(['abc.txt', false, false], $argv),
594+
};
595+
596+
return match ($matcher->numberOfInvocations()) {
597+
1 => '/abc.txt',
598+
2 => $headLink,
599+
3 => 'https://www.test.de/abc_42.txt',
600+
default => '',
601+
};
602+
},
603+
);
604+
$renderer
605+
->expects(self::never())
606+
->method('plugin');
607+
608+
$object = new RevisionHeadLink($minify, $renderer);
609+
610+
$return = $object->prependPackage($package, addRevision: false);
611+
612+
self::assertSame($object, $return);
613+
}
614+
477615
/**
478616
* @throws InvalidArgumentException
479617
* @throws BadMethodCallException

0 commit comments

Comments
 (0)