File tree Expand file tree Collapse file tree
tests/Cases/LinkGenerator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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" >
Original file line number Diff line number Diff line change 44
55use Contributte \MenuControl \IMenuItem ;
66use Nette \Application \LinkGenerator ;
7+ use Nette \Application \UI \InvalidLinkException ;
78use Nette \Http \IRequest ;
89
910final 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}
Original file line number Diff line number Diff line change 44
55use Contributte \MenuControl \LinkGenerator \NetteLinkGenerator ;
66use Mockery \MockInterface ;
7+ use Nette \Application \UI \InvalidLinkException ;
78use Tester \Assert ;
89use 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 ();
You can’t perform that action at this time.
0 commit comments