Skip to content

Commit

Permalink
Merge pull request #181 from mimmi20/updates
Browse files Browse the repository at this point in the history
add Parameter to be able to disable adding a revision
  • Loading branch information
mimmi20 authored Dec 28, 2024
2 parents d410af6 + 40d1529 commit a5fdf70
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 18 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ parameters:
tooWideThrowType: true

cognitive_complexity:
class: 22
class: 25
function: 9

type_coverage:
Expand Down
37 changes: 20 additions & 17 deletions src/View/Helper/RevisionHeadLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function appendPackage(
bool $absolute = true,
string $pathPrefix = '',
bool $clearQuery = false,
bool $addRevision = true,
): self {
$files = $this->minify->getPackageFiles($package);

Expand All @@ -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,
);
}

Expand Down Expand Up @@ -121,6 +122,7 @@ public function prependPackage(
bool $absolute = true,
string $pathPrefix = '',
bool $clearQuery = false,
bool $addRevision = true,
): self {
$files = $this->minify->getPackageFiles($package);

Expand All @@ -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,
);
}

Expand Down Expand Up @@ -181,6 +183,7 @@ public function listPackage(
bool $absolute = true,
string $pathPrefix = '',
bool $clearQuery = false,
bool $addRevision = true,
): array {
$files = $this->minify->getPackageFiles($package);

Expand All @@ -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);
}

Expand Down
138 changes: 138 additions & 0 deletions tests/View/Helper/RevisionHeadLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,78 @@ 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
Expand Down Expand Up @@ -474,6 +546,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
Expand Down

0 comments on commit a5fdf70

Please sign in to comment.