diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 75fc31118b..5486afafc7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -30570,12 +30570,6 @@ parameters: count: 1 path: tests/bundle/Core/Fragment/DirectFragmentRendererTest.php - - - message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with ''Symfony\\\\Component\\\\HttpFoundation\\\\Response'' and Symfony\\Component\\HttpFoundation\\Response will always evaluate to true\.$#' - identifier: method.alreadyNarrowedType - count: 4 - path: tests/bundle/Core/Fragment/DirectFragmentRendererTest.php - - message: '#^Method Ibexa\\Tests\\Bundle\\Core\\Fragment\\FragmentListenerFactoryTest\:\:buildFragmentListenerProvider\(\) has no return type specified\.$#' identifier: missingType.return @@ -58764,12 +58758,6 @@ parameters: count: 1 path: tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php - - - message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with ''Symfony\\\\Component\\\\HttpKernel\\\\Controller\\\\ControllerReference'' and Symfony\\Component\\HttpKernel\\Controller\\ControllerReference will always evaluate to true\.$#' - identifier: method.alreadyNarrowedType - count: 1 - path: tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php - - message: '#^Anonymous function has an unused use \$content\.$#' identifier: closure.unusedUse diff --git a/src/lib/MVC/Symfony/Templating/RenderContentStrategy.php b/src/lib/MVC/Symfony/Templating/RenderContentStrategy.php index fead09659d..dc66831680 100644 --- a/src/lib/MVC/Symfony/Templating/RenderContentStrategy.php +++ b/src/lib/MVC/Symfony/Templating/RenderContentStrategy.php @@ -41,6 +41,7 @@ public function render(ValueObject $valueObject, RenderOptions $options): string $controllerReference = new ControllerReference('ibexa_content::viewAction', [ 'contentId' => $content->id, 'viewType' => $options->get('viewType', self::DEFAULT_VIEW_TYPE), + 'params' => $options->get('params', []), ]); $renderer = $this->getFragmentRenderer($options->get('method', $this->defaultRenderer)); diff --git a/src/lib/MVC/Symfony/Templating/RenderLocationStrategy.php b/src/lib/MVC/Symfony/Templating/RenderLocationStrategy.php index 15b794d85e..f1182c51ac 100644 --- a/src/lib/MVC/Symfony/Templating/RenderLocationStrategy.php +++ b/src/lib/MVC/Symfony/Templating/RenderLocationStrategy.php @@ -42,6 +42,7 @@ public function render(ValueObject $valueObject, RenderOptions $options): string 'contentId' => $content->id, 'locationId' => $location->id, 'viewType' => $options->get('viewType', self::DEFAULT_VIEW_TYPE), + 'params' => $options->get('params', []), ]); $renderer = $this->getFragmentRenderer($options->get('method', $this->defaultRenderer)); diff --git a/tests/bundle/Core/Fragment/DirectFragmentRendererTest.php b/tests/bundle/Core/Fragment/DirectFragmentRendererTest.php index 8d18c1aacf..4e67b3a731 100644 --- a/tests/bundle/Core/Fragment/DirectFragmentRendererTest.php +++ b/tests/bundle/Core/Fragment/DirectFragmentRendererTest.php @@ -65,7 +65,6 @@ public function testSubRequestBuilding(): void $directFragmentRenderer = $this->getDirectFragmentRenderer($controllerResolver); $response = $directFragmentRenderer->render($controllerReference, $request); - $this->assertInstanceOf(Response::class, $response); $this->assertSame('rendered_response', $response->getContent()); } @@ -82,7 +81,6 @@ public function testControllerResponse(): void $directFragmentRenderer = $this->getDirectFragmentRenderer($controllerResolver); $response = $directFragmentRenderer->render('', new Request(), []); - $this->assertInstanceOf(Response::class, $response); $this->assertSame('response_body', $response->getContent()); } @@ -113,7 +111,6 @@ public function testControllerViewResponse(): void ); $response = $directFragmentRenderer->render('', new Request(), []); - $this->assertInstanceOf(Response::class, $response); $this->assertSame('rendered_template_identifier', $response->getContent()); } @@ -130,7 +127,6 @@ public function testControllerStringResponse(): void $directFragmentRenderer = $this->getDirectFragmentRenderer($controllerResolver); $response = $directFragmentRenderer->render('', new Request(), []); - $this->assertInstanceOf(Response::class, $response); $this->assertSame('some_prerendered_response', $response->getContent()); } diff --git a/tests/lib/MVC/Symfony/Templating/ForwardParamOptionsToFragmentRendererTrait.php b/tests/lib/MVC/Symfony/Templating/ForwardParamOptionsToFragmentRendererTrait.php new file mode 100644 index 0000000000..aa49bb5886 --- /dev/null +++ b/tests/lib/MVC/Symfony/Templating/ForwardParamOptionsToFragmentRendererTrait.php @@ -0,0 +1,70 @@ + $renderStrategyClass + */ + public function forwardParamOptionsToFragmentRenderer( + object $fragmentRendererMock, + object $valueObjectMock, + string $renderStrategyClass + ): void { + $params = [ + 'param1' => 'value1', + 'param2' => 'value2', + ]; + + $fragmentRendererMock + ->method('getName') + ->willReturn('fragment_render_mock'); + $fragmentRendererMock->expects(self::once()) + ->method('render') + ->with( + self::callback(static function ($controllerReference) use ($params) { + if (!$controllerReference instanceof ControllerReference) { + return false; + } + + return $controllerReference->attributes['params'] === $params; + }), + self::anything(), + ) + ->willReturn(new Response('fragment_render_mock_rendered')); + + $renderContentStrategy = self::createRenderStrategy( + $renderStrategyClass, + [ + $fragmentRendererMock, + ], + ); + + /** @var \Ibexa\Contracts\Core\Repository\Values\ValueObject&\PHPUnit\Framework\MockObject\MockObject $valueObjectMock */ + TestCase::assertTrue($renderContentStrategy->supports($valueObjectMock)); + + TestCase::assertSame( + 'fragment_render_mock_rendered', + $renderContentStrategy->render($valueObjectMock, new RenderOptions([ + 'method' => 'fragment_render_mock', + 'viewType' => 'awesome', + 'params' => $params, + ])) + ); + } +} diff --git a/tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php b/tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php index 8c12857349..b1e66892cb 100644 --- a/tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php +++ b/tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php @@ -21,6 +21,8 @@ class RenderContentStrategyTest extends BaseRenderStrategyTest { + use ForwardParamOptionsToFragmentRendererTrait; + public function testUnsupportedValueObject(): void { $renderContentStrategy = $this->createRenderStrategy( @@ -32,7 +34,7 @@ public function testUnsupportedValueObject(): void $valueObject = new class() extends ValueObject { }; - $this->assertFalse($renderContentStrategy->supports($valueObject)); + self::assertFalse($renderContentStrategy->supports($valueObject)); $this->expectException(InvalidArgumentException::class); $renderContentStrategy->render($valueObject, new RenderOptions()); @@ -49,9 +51,9 @@ public function testDefaultFragmentRenderer(): void ); $contentMock = $this->createMock(Content::class); - $this->assertTrue($renderContentStrategy->supports($contentMock)); + self::assertTrue($renderContentStrategy->supports($contentMock)); - $this->assertSame( + self::assertSame( 'inline_rendered', $renderContentStrategy->render($contentMock, new RenderOptions()) ); @@ -65,7 +67,7 @@ public function testUnknownFragmentRenderer(): void ); $contentMock = $this->createMock(Content::class); - $this->assertTrue($renderContentStrategy->supports($contentMock)); + self::assertTrue($renderContentStrategy->supports($contentMock)); $this->expectException(InvalidArgumentException::class); $renderContentStrategy->render($contentMock, new RenderOptions()); @@ -83,9 +85,9 @@ public function testMultipleFragmentRenderers(): void ); $contentMock = $this->createMock(Content::class); - $this->assertTrue($renderContentStrategy->supports($contentMock)); + self::assertTrue($renderContentStrategy->supports($contentMock)); - $this->assertSame( + self::assertSame( 'method_b_rendered', $renderContentStrategy->render($contentMock, new RenderOptions([ 'method' => 'method_b', @@ -93,6 +95,15 @@ public function testMultipleFragmentRenderers(): void ); } + public function testForwardParamOptionsToFragmentRenderer(): void + { + $this->forwardParamOptionsToFragmentRenderer( + $this->createMock(FragmentRendererInterface::class), + $this->createMock(Content::class), + RenderContentStrategy::class, + ); + } + public function testDuplicatedFragmentRenderers(): void { $renderContentStrategy = $this->createRenderStrategy( @@ -127,19 +138,19 @@ public function testExpectedMethodRenderArgumentsFormat(): void ->method('getName') ->willReturn('method_b'); - $controllerReferenceCallback = $this->callback(function (ControllerReference $controllerReference) { - $this->assertInstanceOf(ControllerReference::class, $controllerReference); - $this->assertEquals('ibexa_content::viewAction', $controllerReference->controller); - $this->assertSame([ + $controllerReferenceCallback = $this->callback(static function (ControllerReference $controllerReference) { + self::assertEquals('ibexa_content::viewAction', $controllerReference->controller); + self::assertSame([ 'contentId' => 123, 'viewType' => 'awesome', + 'params' => [], ], $controllerReference->attributes); return true; }); - $requestCallback = $this->callback(function (Request $request) use ($siteAccess, $content): bool { - $this->assertSame('TEST/1.0', $request->headers->get('Surrogate-Capability')); + $requestCallback = $this->callback(static function (Request $request): bool { + self::assertSame('TEST/1.0', $request->headers->get('Surrogate-Capability')); return true; }); @@ -162,7 +173,7 @@ public function testExpectedMethodRenderArgumentsFormat(): void $request ); - $this->assertSame('some_rendered_content', $renderContentStrategy->render( + self::assertSame('some_rendered_content', $renderContentStrategy->render( $content, new RenderOptions([ 'method' => 'method_b', diff --git a/tests/lib/MVC/Symfony/Templating/RenderLocationStrategyTest.php b/tests/lib/MVC/Symfony/Templating/RenderLocationStrategyTest.php index 94e28a4f64..4a30f44b99 100644 --- a/tests/lib/MVC/Symfony/Templating/RenderLocationStrategyTest.php +++ b/tests/lib/MVC/Symfony/Templating/RenderLocationStrategyTest.php @@ -21,6 +21,8 @@ class RenderLocationStrategyTest extends BaseRenderStrategyTest { + use ForwardParamOptionsToFragmentRendererTrait; + public function testUnsupportedValueObject(): void { $renderLocationStrategy = $this->createRenderStrategy( @@ -93,6 +95,15 @@ public function testMultipleFragmentRenderers(): void ); } + public function testForwardParamOptionsToFragmentRenderer(): void + { + $this->forwardParamOptionsToFragmentRenderer( + $this->createMock(FragmentRendererInterface::class), + $this->createMock(Location::class), + RenderLocationStrategy::class, + ); + } + public function testExpectedMethodRenderRequestFormat(): void { $request = new Request(); @@ -114,6 +125,7 @@ public function testExpectedMethodRenderRequestFormat(): void 'contentId' => 234, 'locationId' => 345, 'viewType' => 'awesome', + 'params' => [], ], $controllerReference->attributes); return true;