Skip to content

Commit a2ab75a

Browse files
committed
fixup! Added tests for IBX-3035 - Fixed Sonar warning about duplicated code
1 parent dc40058 commit a2ab75a

File tree

3 files changed

+36
-65
lines changed

3 files changed

+36
-65
lines changed

tests/lib/MVC/Symfony/Templating/BaseRenderStrategyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
abstract class BaseRenderStrategyTest extends TestCase
2626
{
27-
public function createRenderStrategy(
27+
public static function createRenderStrategy(
2828
string $typeClass,
2929
array $fragmentRenderers,
3030
string $defaultMethod = 'inline',

tests/lib/MVC/Symfony/Templating/RenderContentStrategyTest.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Ibexa\Core\MVC\Symfony\SiteAccess;
1515
use Ibexa\Core\MVC\Symfony\Templating\RenderContentStrategy;
1616
use Ibexa\Core\MVC\Symfony\Templating\RenderOptions;
17+
use PHPUnit\Framework\MockObject\MockObject;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpFoundation\Response;
1920
use Symfony\Component\HttpKernel\Controller\ControllerReference;
@@ -23,7 +24,7 @@ class RenderContentStrategyTest extends BaseRenderStrategyTest
2324
{
2425
public function testUnsupportedValueObject(): void
2526
{
26-
$renderContentStrategy = $this->createRenderStrategy(
27+
$renderContentStrategy = self::createRenderStrategy(
2728
RenderContentStrategy::class,
2829
[
2930
$this->createFragmentRenderer(),
@@ -40,7 +41,7 @@ public function testUnsupportedValueObject(): void
4041

4142
public function testDefaultFragmentRenderer(): void
4243
{
43-
$renderContentStrategy = $this->createRenderStrategy(
44+
$renderContentStrategy = self::createRenderStrategy(
4445
RenderContentStrategy::class,
4546
[
4647
$this->createFragmentRenderer('inline'),
@@ -59,7 +60,7 @@ public function testDefaultFragmentRenderer(): void
5960

6061
public function testUnknownFragmentRenderer(): void
6162
{
62-
$renderContentStrategy = $this->createRenderStrategy(
63+
$renderContentStrategy = self::createRenderStrategy(
6364
RenderContentStrategy::class,
6465
[],
6566
);
@@ -73,7 +74,7 @@ public function testUnknownFragmentRenderer(): void
7374

7475
public function testMultipleFragmentRenderers(): void
7576
{
76-
$renderContentStrategy = $this->createRenderStrategy(
77+
$renderContentStrategy = self::createRenderStrategy(
7778
RenderContentStrategy::class,
7879
[
7980
$this->createFragmentRenderer('method_a'),
@@ -94,40 +95,48 @@ public function testMultipleFragmentRenderers(): void
9495
}
9596

9697
public function testForwardOptionsToFragmentRenderer(): void
98+
{
99+
static::forwardOptionsToFragmentRenderer(
100+
$this->createMock(FragmentRendererInterface::class),
101+
$this->createMock(Content::class),
102+
RenderContentStrategy::class,
103+
);
104+
}
105+
106+
public static function forwardOptionsToFragmentRenderer(MockObject $fragmentRendererMock, MockObject $valueObjectMock, string $renderStrategyClass): void
97107
{
98108
$params = [
99109
'param1' => 'value1',
100110
'param2' => 'value2',
101111
];
102112

103-
$fragmentRendererMock = $this->createMock(FragmentRendererInterface::class);
104113
$fragmentRendererMock
105114
->method('getName')
106115
->willReturn('fragment_render_mock');
107-
$fragmentRendererMock->expects($this->once())
116+
$fragmentRendererMock->expects(self::once())
108117
->method('render')
109118
->with(
110-
$this->anything(),
111-
$this->anything(),
112-
$this->equalTo([
119+
self::anything(),
120+
self::anything(),
121+
self::equalTo([
113122
'params' => $params,
114123
])
115124
)
116125
->willReturn(new Response('fragment_render_mock_rendered'));
117126

118-
$renderContentStrategy = $this->createRenderStrategy(
119-
RenderContentStrategy::class,
127+
$renderContentStrategy = self::createRenderStrategy(
128+
$renderStrategyClass,
120129
[
121130
$fragmentRendererMock,
122131
],
123132
);
124133

125-
$contentMock = $this->createMock(Content::class);
126-
$this->assertTrue($renderContentStrategy->supports($contentMock));
134+
/** @var \Ibexa\Contracts\Core\Repository\Values\ValueObject&\PHPUnit\Framework\MockObject\MockObject $valueObjectMock */
135+
self::assertTrue($renderContentStrategy->supports($valueObjectMock));
127136

128-
$this->assertSame(
137+
self::assertSame(
129138
'fragment_render_mock_rendered',
130-
$renderContentStrategy->render($contentMock, new RenderOptions([
139+
$renderContentStrategy->render($valueObjectMock, new RenderOptions([
131140
'method' => 'fragment_render_mock',
132141
'viewType' => 'awesome',
133142
'params' => [
@@ -140,7 +149,7 @@ public function testForwardOptionsToFragmentRenderer(): void
140149

141150
public function testDuplicatedFragmentRenderers(): void
142151
{
143-
$renderContentStrategy = $this->createRenderStrategy(
152+
$renderContentStrategy = self::createRenderStrategy(
144153
RenderContentStrategy::class,
145154
[
146155
$this->createFragmentRenderer('method_a', 'decorator service used'),
@@ -195,7 +204,7 @@ public function testExpectedMethodRenderArgumentsFormat(): void
195204
->with($controllerReferenceCallback, $requestCallback)
196205
->willReturn(new Response('some_rendered_content'));
197206

198-
$renderContentStrategy = $this->createRenderStrategy(
207+
$renderContentStrategy = self::createRenderStrategy(
199208
RenderContentStrategy::class,
200209
[
201210
$this->createFragmentRenderer('method_a'),

tests/lib/MVC/Symfony/Templating/RenderLocationStrategyTest.php

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
namespace Ibexa\Tests\Core\MVC\Symfony\Templating;
1010

1111
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
12-
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
1312
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
1413
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
1514
use Ibexa\Core\MVC\Symfony\SiteAccess;
16-
use Ibexa\Core\MVC\Symfony\Templating\RenderContentStrategy;
1715
use Ibexa\Core\MVC\Symfony\Templating\RenderLocationStrategy;
1816
use Ibexa\Core\MVC\Symfony\Templating\RenderOptions;
1917
use Symfony\Component\HttpFoundation\Request;
@@ -25,7 +23,7 @@ class RenderLocationStrategyTest extends BaseRenderStrategyTest
2523
{
2624
public function testUnsupportedValueObject(): void
2725
{
28-
$renderLocationStrategy = $this->createRenderStrategy(
26+
$renderLocationStrategy = self::createRenderStrategy(
2927
RenderLocationStrategy::class,
3028
[
3129
$this->createFragmentRenderer(),
@@ -42,7 +40,7 @@ public function testUnsupportedValueObject(): void
4240

4341
public function testDefaultFragmentRenderer(): void
4442
{
45-
$renderLocationStrategy = $this->createRenderStrategy(
43+
$renderLocationStrategy = self::createRenderStrategy(
4644
RenderLocationStrategy::class,
4745
[
4846
$this->createFragmentRenderer('inline'),
@@ -61,7 +59,7 @@ public function testDefaultFragmentRenderer(): void
6159

6260
public function testUnknownFragmentRenderer(): void
6361
{
64-
$renderLocationStrategy = $this->createRenderStrategy(
62+
$renderLocationStrategy = self::createRenderStrategy(
6563
RenderLocationStrategy::class,
6664
[],
6765
);
@@ -75,7 +73,7 @@ public function testUnknownFragmentRenderer(): void
7573

7674
public function testMultipleFragmentRenderers(): void
7775
{
78-
$renderLocationStrategy = $this->createRenderStrategy(
76+
$renderLocationStrategy = self::createRenderStrategy(
7977
RenderLocationStrategy::class,
8078
[
8179
$this->createFragmentRenderer('method_a'),
@@ -97,46 +95,10 @@ public function testMultipleFragmentRenderers(): void
9795

9896
public function testForwardOptionsToFragmentRenderer(): void
9997
{
100-
$params = [
101-
'param1' => 'value1',
102-
'param2' => 'value2',
103-
];
104-
105-
$fragmentRendererMock = $this->createMock(FragmentRendererInterface::class);
106-
$fragmentRendererMock
107-
->method('getName')
108-
->willReturn('fragment_render_mock');
109-
$fragmentRendererMock->expects($this->once())
110-
->method('render')
111-
->with(
112-
$this->anything(),
113-
$this->anything(),
114-
$this->equalTo([
115-
'params' => $params,
116-
])
117-
)
118-
->willReturn(new Response('fragment_render_mock_rendered'));
119-
120-
$renderContentStrategy = $this->createRenderStrategy(
121-
RenderContentStrategy::class,
122-
[
123-
$fragmentRendererMock,
124-
],
125-
);
126-
127-
$contentMock = $this->createMock(Content::class);
128-
$this->assertTrue($renderContentStrategy->supports($contentMock));
129-
130-
$this->assertSame(
131-
'fragment_render_mock_rendered',
132-
$renderContentStrategy->render($contentMock, new RenderOptions([
133-
'method' => 'fragment_render_mock',
134-
'viewType' => 'awesome',
135-
'params' => [
136-
'param1' => 'value1',
137-
'param2' => 'value2',
138-
],
139-
]))
98+
RenderContentStrategyTest::forwardOptionsToFragmentRenderer(
99+
$this->createMock(FragmentRendererInterface::class),
100+
$this->createMock(Location::class),
101+
RenderLocationStrategy::class,
140102
);
141103
}
142104

@@ -178,7 +140,7 @@ public function testExpectedMethodRenderRequestFormat(): void
178140
->with($controllerReferenceCallback, $requestCallback)
179141
->willReturn(new Response('some_rendered_content'));
180142

181-
$renderLocationStrategy = $this->createRenderStrategy(
143+
$renderLocationStrategy = self::createRenderStrategy(
182144
RenderLocationStrategy::class,
183145
[
184146
$this->createFragmentRenderer('method_a'),

0 commit comments

Comments
 (0)