diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6cedc78f98..5dbee0e914 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -30462,12 +30462,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 @@ -58567,23 +58561,11 @@ parameters: path: tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest.php - - message: '#^Method Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface@anonymous/tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest\.php\:51\:\:render\(\) has parameter \$options with no value type specified in iterable type array\.$#' + message: '#^Method Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface@anonymous/tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest\.php\:53\:\:render\(\) has parameter \$options with no value type specified in iterable type array\.$#' identifier: missingType.iterableValue count: 1 path: tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest.php - - - message: '#^Property Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface@anonymous/tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest\.php\:51\:\:\$rendered \(string\) does not accept string\|null\.$#' - identifier: assign.propertyType - count: 1 - path: tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest.php - - - - message: '#^Property Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface@anonymous/tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest\.php\:51\:\:\$rendered \(string\) on left side of \?\? is not nullable\.$#' - identifier: nullCoalesce.property - count: 1 - path: tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest.php - - message: '#^Method Ibexa\\Tests\\Core\\MVC\\Symfony\\Templating\\GlobalHelperTest\:\:testGetAvailableLanguages\(\) has no return type specified\.$#' identifier: missingType.return @@ -58644,24 +58626,6 @@ parameters: count: 1 path: tests/lib/MVC/Symfony/Templating/GlobalHelperTest.php - - - message: '#^Anonymous function has an unused use \$content\.$#' - identifier: closure.unusedUse - count: 1 - path: tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php - - - - message: '#^Anonymous function has an unused use \$siteAccess\.$#' - identifier: closure.unusedUse - 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/BaseRenderStrategyTest.php b/tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest.php index 66f71a4bf1..23334da133 100644 --- a/tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest.php +++ b/tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest.php @@ -13,6 +13,7 @@ use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; use Ibexa\Contracts\Core\Repository\Values\Content\Location as APILocation; use Ibexa\Core\MVC\Symfony\SiteAccess; +use Ibexa\Core\MVC\Symfony\Templating\RenderOptions; use Ibexa\Core\Repository\Values\Content\Content; use Ibexa\Core\Repository\Values\Content\Location; use Ibexa\Core\Repository\Values\Content\VersionInfo; @@ -20,6 +21,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Controller\ControllerReference; use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface; abstract class BaseRenderStrategyTest extends TestCase @@ -52,7 +54,7 @@ public function createFragmentRenderer( /** @var string */ private $name; - /** @var string */ + /** @var string|null */ private $rendered; public function __construct( @@ -97,6 +99,58 @@ public function createContent(int $id): APIContent ]), ]); } + + /** + * @param \Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface|\PHPUnit\Framework\MockObject\MockObject $fragmentRendererMock + * @param \Ibexa\Contracts\Core\Repository\Values\ValueObject|\PHPUnit\Framework\MockObject\MockObject $valueObjectMock + * @param class-string $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): bool { + if (!$controllerReference instanceof ControllerReference) { + return false; + } + + return $controllerReference->attributes['params'] === $params; + }), + self::anything(), + ) + ->willReturn(new Response('fragment_render_mock_rendered')); + + $renderContentStrategy = $this->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, + ])) + ); + } } class_alias(BaseRenderStrategyTest::class, 'eZ\Publish\Core\MVC\Symfony\Templating\Tests\BaseRenderStrategyTest'); diff --git a/tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php b/tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php index 8c12857349..47862de4af 100644 --- a/tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php +++ b/tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php @@ -32,7 +32,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 +49,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 +65,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 +83,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 +93,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 +136,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): bool { + 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 +171,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..50dbf3f2af 100644 --- a/tests/lib/MVC/Symfony/Templating/RenderLocationStrategyTest.php +++ b/tests/lib/MVC/Symfony/Templating/RenderLocationStrategyTest.php @@ -93,6 +93,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 +123,7 @@ public function testExpectedMethodRenderRequestFormat(): void 'contentId' => 234, 'locationId' => 345, 'viewType' => 'awesome', + 'params' => [], ], $controllerReference->attributes); return true;