diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a574bd1..cd2f698 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -23,15 +23,9 @@ jobs: with: php: "8.2" - test81: - name: "Nette Tester" - uses: contributte/.github/.github/workflows/nette-tester.yml@master - with: - php: "8.1" - testlower: name: "Nette Tester" uses: contributte/.github/.github/workflows/nette-tester.yml@master with: - php: "8.1" + php: "8.2" composer: "composer update --no-interaction --no-progress --prefer-dist --prefer-stable --prefer-lowest" diff --git a/ruleset.xml b/ruleset.xml index bfa454d..4f7e768 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -1,7 +1,7 @@ - + diff --git a/src/LinkGenerator/NetteLinkGenerator.php b/src/LinkGenerator/NetteLinkGenerator.php index fe83b11..386c708 100644 --- a/src/LinkGenerator/NetteLinkGenerator.php +++ b/src/LinkGenerator/NetteLinkGenerator.php @@ -4,6 +4,7 @@ use Contributte\MenuControl\IMenuItem; use Nette\Application\LinkGenerator; +use Nette\Application\UI\InvalidLinkException; use Nette\Http\IRequest; final class NetteLinkGenerator implements ILinkGenerator @@ -23,7 +24,10 @@ public function link(IMenuItem $item): string { $action = $item->getActionTarget(); if ($action !== null) { - return $this->linkGenerator->link($action, $item->getActionParameters()); + $generatedLink = $this->tryLink($action, $item->getActionParameters()); + if ($generatedLink !== null) { + return $generatedLink; + } } $link = $item->getLink(); @@ -46,4 +50,16 @@ public function absoluteLink(IMenuItem $item): string return $prefix . $this->link($item); } + /** + * @param array $parameters + */ + private function tryLink(string $action, array $parameters): ?string + { + try { + return $this->linkGenerator->link($action, $parameters); + } catch (InvalidLinkException) { + return null; + } + } + } diff --git a/tests/Cases/LinkGenerator/NetteLinkGeneratorTest.php b/tests/Cases/LinkGenerator/NetteLinkGeneratorTest.php index 00ae806..f778d7b 100644 --- a/tests/Cases/LinkGenerator/NetteLinkGeneratorTest.php +++ b/tests/Cases/LinkGenerator/NetteLinkGeneratorTest.php @@ -4,6 +4,7 @@ use Contributte\MenuControl\LinkGenerator\NetteLinkGenerator; use Mockery\MockInterface; +use Nette\Application\UI\InvalidLinkException; use Tester\Assert; use Tests\Toolkit\AbstractTestCase; @@ -71,6 +72,24 @@ public function testLinkLink(): void Assert::same('/', $linkGenerator->link($item)); } + public function testLinkActionWithInvalidGeneratedLinkFallsBackToItemLink(): void + { + $request = $this->createMockHttpRequest(); + $netteLinkGenerator = $this->createMockNetteLinkGenerator(function (MockInterface $netteLinkGenerator): void { + $netteLinkGenerator->shouldReceive('link')->andThrow(new InvalidLinkException()); + }); + + $item = $this->createMockMenuItem(function (MockInterface $item): void { + $item->shouldReceive('getActionTarget')->andReturn('Home:default'); + $item->shouldReceive('getActionParameters')->andReturn([]); + $item->shouldReceive('getLink')->andReturn('/fallback'); + }); + + $linkGenerator = new NetteLinkGenerator($request, $netteLinkGenerator); + + Assert::same('/fallback', $linkGenerator->link($item)); + } + public function testLinkEmpty(): void { $request = $this->createMockHttpRequest();