diff --git a/src/Handler/EmbeddedShortcodeHandler.php b/src/Handler/EmbeddedShortcodeHandler.php index 8dec477..cfb6b97 100644 --- a/src/Handler/EmbeddedShortcodeHandler.php +++ b/src/Handler/EmbeddedShortcodeHandler.php @@ -33,17 +33,18 @@ class EmbeddedShortcodeHandler /** @var RequestStack */ private $requestStack; - /** - * @param string $controllerName - * @param string $renderer - */ public function __construct( FragmentHandler $fragmentHandler, - $controllerName, - $renderer, + string $controllerName, + string $renderer, RequestStack $requestStack, - ?LoggerInterface $logger = null + LoggerInterface $logger = null ) { + $callableFragments = explode('::', $controllerName); + if (!is_array($callableFragments) || !isset($callableFragments[1]) || !method_exists($callableFragments[0], $callableFragments[1])) { + throw new \InvalidArgumentException('The controller method: "'.$controllerName.'" does not exist.'); + } + $this->fragmentHandler = $fragmentHandler; $this->controllerName = $controllerName; $this->renderer = $renderer; diff --git a/tests/Functional/EmbeddedShortcodeHandlerTest.php b/tests/Functional/EmbeddedShortcodeHandlerTest.php index fa5977e..c727559 100644 --- a/tests/Functional/EmbeddedShortcodeHandlerTest.php +++ b/tests/Functional/EmbeddedShortcodeHandlerTest.php @@ -6,6 +6,9 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpKernel\Fragment\FragmentHandler; +use Webfactory\ShortcodeBundle\Controller\GuideController; +use Webfactory\ShortcodeBundle\Handler\EmbeddedShortcodeHandler; use Webfactory\ShortcodeBundle\Test\EndToEndTestHelper; /** @@ -37,7 +40,7 @@ public function expand_shortcodes_registered_in_different_ways(string $shortcode self::assertSame('test foo=bar', $this->processShortcodes("[$shortcodeName foo=bar]")); } - public static function provideShortcodeNames(): Generator + public function provideShortcodeNames(): Generator { yield 'Inline shortcode defined in bundle config' => ['test-config-inline']; yield 'ESI-based shortcode defined in bundle config' => ['test-config-esi']; @@ -58,13 +61,48 @@ public function processing_with_esi_fragments(string $shortcodeName): void self::assertStringContainsString('processShortcodes("[$shortcodeName foo=bar]", $request)); } - public static function provideEsiShortcodes(): Generator + public function provideEsiShortcodes(): Generator { yield 'ESI-based shortcode defined in bundle configuration' => ['test-config-esi']; yield 'ESI-based shortcode defined in service configuration' => ['test-service-esi']; } - private function processShortcodes(string $content, ?Request $request = null): string + /** + * @test + */ + public function validate_valid_controller_names(): void + { + $this->expectNotToPerformAssertions(); + + new EmbeddedShortcodeHandler( + $this->createMock(FragmentHandler::class), + GuideController::class.'::detailAction', + 'inline', + $this->createMock(RequestStack::class) + ); + } + + /** + * @test + * + * @dataProvider provideControllerNames + */ + public function validate_invalid_controller_names(string $controllerName): void + { + $this->expectException(\InvalidArgumentException::class); + + new EmbeddedShortcodeHandler($this->createMock(FragmentHandler::class), $controllerName, 'inline', $this->createMock(RequestStack::class)); + } + + public function provideControllerNames(): Generator + { + yield 'Empty string' => ['']; + yield 'Not existing controller' => ['Foo/Bar/Not/Exist::method']; + yield 'Missing method name' => [GuideController::class]; + yield 'Not existing method' => [GuideController::class.'_notExistingMethod']; + } + + private function processShortcodes(string $content, Request $request = null): string { self::bootKernel();