Skip to content

Commit c649790

Browse files
authored
LinkGenerator: fix types (#66)
1 parent ed98b4f commit c649790

4 files changed

Lines changed: 38 additions & 9 deletions

File tree

.github/workflows/tests.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@ jobs:
2323
with:
2424
php: "8.2"
2525

26-
test81:
27-
name: "Nette Tester"
28-
uses: contributte/.github/.github/workflows/nette-tester.yml@master
29-
with:
30-
php: "8.1"
31-
3226
testlower:
3327
name: "Nette Tester"
3428
uses: contributte/.github/.github/workflows/nette-tester.yml@master
3529
with:
36-
php: "8.1"
30+
php: "8.2"
3731
composer: "composer update --no-interaction --no-progress --prefer-dist --prefer-stable --prefer-lowest"

ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ruleset name="Contributte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
33
<!-- Rulesets -->
4-
<rule ref="./vendor/contributte/qa/ruleset-8.0.xml"/>
4+
<rule ref="./vendor/contributte/qa/ruleset-8.2.xml"/>
55

66
<!-- Rules -->
77
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">

src/LinkGenerator/NetteLinkGenerator.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Contributte\MenuControl\IMenuItem;
66
use Nette\Application\LinkGenerator;
7+
use Nette\Application\UI\InvalidLinkException;
78
use Nette\Http\IRequest;
89

910
final class NetteLinkGenerator implements ILinkGenerator
@@ -23,7 +24,10 @@ public function link(IMenuItem $item): string
2324
{
2425
$action = $item->getActionTarget();
2526
if ($action !== null) {
26-
return $this->linkGenerator->link($action, $item->getActionParameters());
27+
$generatedLink = $this->tryLink($action, $item->getActionParameters());
28+
if ($generatedLink !== null) {
29+
return $generatedLink;
30+
}
2731
}
2832

2933
$link = $item->getLink();
@@ -46,4 +50,16 @@ public function absoluteLink(IMenuItem $item): string
4650
return $prefix . $this->link($item);
4751
}
4852

53+
/**
54+
* @param array<string, mixed> $parameters
55+
*/
56+
private function tryLink(string $action, array $parameters): ?string
57+
{
58+
try {
59+
return $this->linkGenerator->link($action, $parameters);
60+
} catch (InvalidLinkException) {
61+
return null;
62+
}
63+
}
64+
4965
}

tests/Cases/LinkGenerator/NetteLinkGeneratorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Contributte\MenuControl\LinkGenerator\NetteLinkGenerator;
66
use Mockery\MockInterface;
7+
use Nette\Application\UI\InvalidLinkException;
78
use Tester\Assert;
89
use Tests\Toolkit\AbstractTestCase;
910

@@ -71,6 +72,24 @@ public function testLinkLink(): void
7172
Assert::same('/', $linkGenerator->link($item));
7273
}
7374

75+
public function testLinkActionWithInvalidGeneratedLinkFallsBackToItemLink(): void
76+
{
77+
$request = $this->createMockHttpRequest();
78+
$netteLinkGenerator = $this->createMockNetteLinkGenerator(function (MockInterface $netteLinkGenerator): void {
79+
$netteLinkGenerator->shouldReceive('link')->andThrow(new InvalidLinkException());
80+
});
81+
82+
$item = $this->createMockMenuItem(function (MockInterface $item): void {
83+
$item->shouldReceive('getActionTarget')->andReturn('Home:default');
84+
$item->shouldReceive('getActionParameters')->andReturn([]);
85+
$item->shouldReceive('getLink')->andReturn('/fallback');
86+
});
87+
88+
$linkGenerator = new NetteLinkGenerator($request, $netteLinkGenerator);
89+
90+
Assert::same('/fallback', $linkGenerator->link($item));
91+
}
92+
7493
public function testLinkEmpty(): void
7594
{
7695
$request = $this->createMockHttpRequest();

0 commit comments

Comments
 (0)