ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DefaultRendererTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/TestComponent.php");
22require_once(__DIR__ . "/../Base.php");
23
36
38{
41
42 public function testGetRendererSuccessfully(): void
43 {
44 // There should be a renderer for Glyph...
45 $dr = $this->getDefaultRenderer();
46 $r = $dr->_getRendererFor(new Glyph("up", "up"));
47 $this->assertInstanceOf(ComponentRenderer::class, $r);
48 }
49
50 public function testGetRendererCaching(): void
51 {
52 $dr = $this->getDefaultRenderer();
53 $r1 = $dr->_getRendererFor(new Glyph("up", "up"));
54 $r2 = $dr->_getRendererFor(new Glyph("up", "up"));
55 $this->assertTrue($r1 === $r2, "Instances not equal");
56 }
57
59 {
60 $this->resource_registry = parent::getResourceRegistry();
62 }
63
64 public function testInvokesRegistry(): void
65 {
66 $dr = $this->getDefaultRenderer();
67 $component = new TestComponent("foo");
68
69 $dr->render($component);
70
71 $this->assertEquals(array("test.js"), $this->resource_registry->resources);
72 }
73
78 public function testRenderingChainAndContextStack(): void
79 {
80 $component1 = new TestComponent("component1");
81 $component2 = new TestComponent("component2");
82 $component3 = new TestComponent("component3");
83 $glue = '.';
84
85 $component_renderer = $this->createMock(ComponentRenderer::class);
86 $component_renderer->method('render')->willReturnCallback(
87 static function (TestComponent $component, DefaultRenderer $renderer) use (
88 $component1,
89 $component2,
90 $component3,
91 $glue
92 ) {
93 return match ($component->text) {
94 $component1->text => $component1->text . $glue . $renderer->render($component2),
95 $component2->text => $component2->text . $glue . $renderer->render($component3),
96 $component3->text => $component3->text,
97 };
98 }
99 );
100
101 $renderer = new class (
102 $this->createMock(Render\Loader::class),
103 $this->createMock(JavaScriptBinding::class),
104 $this->createMock(\ILIAS\Language\Language::class),
106 ) extends DefaultRenderer {
107 public array $context_history = [];
108
109 public function __construct(
110 Render\Loader $component_renderer_loader,
111 JavaScriptBinding $java_script_binding,
112 \ILIAS\Language\Language $language,
114 ) {
115 parent::__construct($component_renderer_loader, $java_script_binding, $language);
116 }
117
118 protected function getRendererFor(Component $component): ComponentRenderer
119 {
121 }
122
123 protected function pushContext(Component $component): void
124 {
125 parent::pushContext($component);
126 $this->context_history[] = $this->getContexts();
127 }
128
129 protected function popContext(): void
130 {
131 parent::popContext();
132 $this->context_history[] = $this->getContexts();
133 }
134 };
135
136 $expected_context_history = [
137 [$component1],
138 [$component1, $component2],
139 [$component1, $component2, $component3],
140 [$component1, $component2],
141 [$component1],
142 []
143 ];
144
145 $expected_html =
146 $component1->text . $glue .
147 $component2->text . $glue .
148 $component3->text;
149
150 $html = $renderer->render($component1);
151
152 $this->assertEquals($expected_context_history, $renderer->context_history);
153 $this->assertEquals($expected_html, $html);
154 }
155
156 public function testRender(): void
157 {
158 $c1 = new TestComponent("foo");
159 $renderer = $this->getDefaultRenderer();
160 $html = $renderer->render($c1);
161 $this->assertEquals("foo", $html);
162 }
163
164 public function testRenderAsyncNoJs(): void
165 {
166 $c1 = new TestComponent("foo");
167 $renderer = $this->getDefaultRenderer(
169 $this->getTemplateFactory()->getTemplate("tpl.main.html", false, false)
170 )
171 );
172 $html = $renderer->renderAsync($c1);
173 $this->assertEquals("foo", $html);
174 }
175
176 public function testRenderAsyncWithJs(): void
177 {
178 $c1 = new JSTestComponent("foo");
179 $renderer = $this->getDefaultRenderer(
180 new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
181 );
182 $html = $renderer->renderAsync($c1);
183 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
184 }
185
186 public function testRenderAsyncWithJsTwice(): void
187 {
188 $c1 = new TestComponent("foo");
189 $c2 = new JSTestComponent("foo");
190 $renderer = $this->getDefaultRenderer(
191 new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
192 );
193 $html = $renderer->renderAsync($c2);
194 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
195 $html = $renderer->renderAsync($c1);
196 $this->assertEquals("foo", $html);
197 $html = $renderer->renderAsync($c2);
198 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
199 }
200
201 public function testRenderAsyncArray(): void
202 {
203 $c1 = new TestComponent("foo");
204
205 $renderer = $this->getDefaultRenderer(
206 new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
207 );
208 $html = $renderer->renderAsync([$c1,$c1]);
209 $this->assertEquals('foofoo', $html);
210 }
211
212 #[\PHPUnit\Framework\Attributes\DataProvider('getRenderType')]
213 public function testPassesSelfAsRootIfNoRootExist($render_type)
214 {
215 $this->component_renderer = $this->createMock(ComponentRenderer::class);
216 $component = $this->createMock(C\Component::class);
217
218 $loader = $this->createMock(Loader::class);
219 $loader->method('getRendererFor')->willReturn($this->component_renderer);
220
221 $renderer = new TestDefaultRenderer($loader, $this->getJavaScriptBinding(), $this->getLanguage());
222
223 $this->component_renderer->expects($this->once())
224 ->method("render")
225 ->with($component, $renderer);
226
227 $renderer->$render_type($component);
228 }
229
230 #[\PHPUnit\Framework\Attributes\DataProvider('getRenderType')]
231 public function testPassesOtherOnAsRoot($render_type)
232 {
233 $this->component_renderer = $this->createMock(ComponentRenderer::class);
234 $component = $this->createMock(C\Component::class);
235 $root = $this->createMock(Renderer::class);
236
237 $loader = $this->createMock(Loader::class);
238 $loader->method('getRendererFor')->willReturn($this->component_renderer);
239
240 $renderer = new TestDefaultRenderer($loader, $this->getJavaScriptBinding(), $this->getLanguage());
241
242 $this->component_renderer->expects($this->once())
243 ->method("render")
244 ->with($component, $root);
245
246 $renderer->$render_type($component, $root);
247 }
248
249 public static function getRenderType()
250 {
251 return [
252 ["render"],
253 ["renderAsync"]
254 ];
255 }
256
258 {
259 $component = $this->createMock(C\Component::class);
260 $root = $this->createMock(Renderer::class);
261
262 $renderer = $this->getDefaultRenderer();
263
264 $root->expects($this->exactly(2))
265 ->method("render")
266 ->with($component)
267 ->willReturn(".");
268
269 $res = $renderer->render([$component, $component], $root);
270 $this->assertEquals("..", $res);
271 }
272}
$renderer
testPassesOtherOnAsRoot($render_type)
testPassesSelfAsRootIfNoRootExist($render_type)
ComponentRenderer $component_renderer
LoggingRegistry $resource_registry
testRenderingChainAndContextStack()
Simulates the rendering chain and tests if the contexts are properly stacked.
Renderer that dispatches rendering of UI components to a Renderer found in the same namespace as the ...
Wraps global ilTemplate to provide JavaScriptBinding.
Provides common functionality for UI tests.
Definition: Base.php:337
A component is the most general form of an entity in the UI.
Definition: Component.php:28
An entity that renders components to a string output.
Provides methods to interface with javascript.
Loads renderers for components.
Definition: Loader.php:30
An entity that renders components to a string output.
Definition: Renderer.php:31
$res
Definition: ltiservices.php:69
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
getLanguage()