Skip to content

Commit 84e1133

Browse files
author
MateuszKolankowski
committed
Merge branch '4.6'
2 parents 6d2456c + 86a4a53 commit 84e1133

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

src/lib/Component/TemplateComponent.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public function __construct(
4040
*/
4141
public function render(array $parameters = []): string
4242
{
43-
return $this->twig->render($this->template, array_merge($parameters + $this->parameters));
43+
$parameters = $this->getParameters($parameters);
44+
45+
return $this->twig->render($this->template, $parameters);
46+
}
47+
48+
/**
49+
* @param array<mixed> $parameters
50+
*
51+
* @return array<mixed>
52+
*/
53+
protected function getParameters(array $parameters): array
54+
{
55+
return $parameters + $this->parameters;
4456
}
4557
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Bundle\TwigComponents\Component;
10+
11+
use Ibexa\TwigComponents\Component\TemplateComponent;
12+
use PHPUnit\Framework\TestCase;
13+
use Twig\Environment;
14+
15+
final class TemplateComponentTest extends TestCase
16+
{
17+
public function testRenderWithoutParameters(): void
18+
{
19+
$twig = $this->configureTwig([
20+
'__parameter__' => true,
21+
]);
22+
23+
$component = new TemplateComponent(
24+
$twig,
25+
'__template__',
26+
[
27+
'__parameter__' => true,
28+
],
29+
);
30+
31+
$component->render();
32+
}
33+
34+
public function testRenderWithParameters(): void
35+
{
36+
$twig = $this->configureTwig([
37+
'__parameter__' => false,
38+
]);
39+
40+
$component = new TemplateComponent(
41+
$twig,
42+
'__template__',
43+
[
44+
'__parameter__' => true,
45+
],
46+
);
47+
48+
$component->render([
49+
'__parameter__' => false,
50+
]);
51+
}
52+
53+
public function testRenderWithNewParameter(): void
54+
{
55+
$twig = $this->configureTwig([
56+
'__parameter__' => true,
57+
'__new_parameter__' => 'foo',
58+
]);
59+
60+
$component = new TemplateComponent(
61+
$twig,
62+
'__template__',
63+
[
64+
'__parameter__' => true,
65+
],
66+
);
67+
68+
$component->render([
69+
'__new_parameter__' => 'foo',
70+
]);
71+
}
72+
73+
/**
74+
* @param array<mixed> $parameters
75+
*
76+
* @return \Twig\Environment|\PHPUnit\Framework\MockObject\MockObject
77+
*/
78+
private function configureTwig(array $parameters): Environment
79+
{
80+
$twig = $this->createMock(Environment::class);
81+
$twig->expects(self::once())
82+
->method('render')
83+
->with(
84+
'__template__',
85+
$parameters,
86+
);
87+
88+
return $twig;
89+
}
90+
}

0 commit comments

Comments
 (0)