From 4b0b7503c5c85598affbf66837445dcbda1f42cd Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Mon, 1 Jun 2026 11:09:10 +0200 Subject: [PATCH] [Fix] Throw exception when no resolver can handle the URI in UriBasedSectionProvider --- .../UriBasedSectionProvider.php | 2 +- .../UriBasedSectionProviderTest.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Sylius/Bundle/CoreBundle/SectionResolver/UriBasedSectionProvider.php b/src/Sylius/Bundle/CoreBundle/SectionResolver/UriBasedSectionProvider.php index a91f339221..2dfd41fd73 100644 --- a/src/Sylius/Bundle/CoreBundle/SectionResolver/UriBasedSectionProvider.php +++ b/src/Sylius/Bundle/CoreBundle/SectionResolver/UriBasedSectionProvider.php @@ -44,6 +44,6 @@ final class UriBasedSectionProvider implements SectionProviderInterface } } - return null; + throw new SectionCannotBeResolvedException(sprintf('Section cannot be resolved for URI "%s".', $uri)); } } diff --git a/src/Sylius/Bundle/CoreBundle/tests/SectionResolver/UriBasedSectionProviderTest.php b/src/Sylius/Bundle/CoreBundle/tests/SectionResolver/UriBasedSectionProviderTest.php index 6037a8effe..bec895d3e5 100644 --- a/src/Sylius/Bundle/CoreBundle/tests/SectionResolver/UriBasedSectionProviderTest.php +++ b/src/Sylius/Bundle/CoreBundle/tests/SectionResolver/UriBasedSectionProviderTest.php @@ -88,4 +88,36 @@ final class UriBasedSectionProviderTest extends TestCase $this->requestStack->expects($this->once())->method('getMainRequest')->willReturn(null); $this->assertNull($this->uriBasedSectionProvider->getSection()); } + + public function testThrowsExceptionIfNoResolverCanHandleTheUri(): void + { + $request = $this->createMock(Request::class); + + $this->requestStack + ->expects($this->once()) + ->method('getMainRequest') + ->willReturn($request); + $request + ->expects($this->once()) + ->method('getPathInfo') + ->willReturn('/unknown-section'); + + $this->firstSectionResolver + ->expects($this->once()) + ->method('getSection') + ->with('/unknown-section') + ->willThrowException(new SectionCannotBeResolvedException()) + ; + + $this->secondSectionResolver + ->expects($this->once()) + ->method('getSection') + ->with('/unknown-section') + ->willThrowException(new SectionCannotBeResolvedException()) + ; + + $this->expectException(SectionCannotBeResolvedException::class); + + $this->uriBasedSectionProvider->getSection(); + } }