ILIAS  release_8 Revision v8.24
DefaultRendererTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21require_once(__DIR__ . "/TestComponent.php");
22require_once(__DIR__ . "/../Base.php");
23
33
35{
36 public function test_getRenderer_successfully(): void
37 {
38 // There should be a renderer for Glyph...
39 $dr = $this->getDefaultRenderer();
40 $r = $dr->_getRendererFor(new Glyph("up", "up"));
41 $this->assertInstanceOf(ComponentRenderer::class, $r);
42 }
43
44 public function test_getRenderer_caching(): void
45 {
46 $dr = $this->getDefaultRenderer();
47 $r1 = $dr->_getRendererFor(new Glyph("up", "up"));
48 $r2 = $dr->_getRendererFor(new Glyph("up", "up"));
49 $this->assertTrue($r1 === $r2, "Instances not equal");
50 }
51
53 {
54 $this->resource_registry = parent::getResourceRegistry();
55 return $this->resource_registry;
56 }
57
58 public function test_invokesRegistry(): void
59 {
60 $dr = $this->getDefaultRenderer();
61 $component = new TestComponent("foo");
62
63 $dr->render($component);
64
65 $this->assertEquals(array("test.js"), $this->resource_registry->resources);
66 }
67
68 public function test_withAdditionalContext_clones(): void
69 {
70 $dr = $this->getDefaultRenderer();
71 $component = new TestComponent("foo");
72 $dr2 = $dr->withAdditionalContext($component);
73 $this->assertNotSame($dr, $dr2);
74 }
75
76 public function test_getContexts(): void
77 {
78 $dr = $this->getDefaultRenderer();
79 $c1 = new TestComponent("foo");
80 $c2 = new TestComponent("bar");
81 $dr2 = $dr
82 ->withAdditionalContext($c1)
83 ->withAdditionalContext($c2);
84 $this->assertEquals([], $dr->_getContexts());
85 $this->assertEquals([$c1, $c2], $dr2->_getContexts());
86 }
87
89 {
90 $loader = $this
91 ->getMockBuilder(Loader::class)
92 ->onlyMethods(["getRendererFor", "getRendererFactoryFor"])
93 ->getMock();
94
95 $renderer = new TestDefaultRenderer($loader);
96
97 $c1 = new TestComponent("foo");
98 $c2 = new TestComponent("bar");
99
100 $renderer = $renderer
101 ->withAdditionalContext($c1)
102 ->withAdditionalContext($c2);
103
104 $loader
105 ->expects($this->once())
106 ->method("getRendererFor")
107 ->with($c1, [$c1, $c2]);
108
109 $renderer->_getRendererFor($c1);
110 }
111
112 public function test_render(): void
113 {
114 $c1 = new TestComponent("foo");
115 $renderer = $this->getDefaultRenderer();
116 $html = $renderer->render($c1);
117 $this->assertEquals("foo", $html);
118 }
119
120 public function test_render_async_no_js(): void
121 {
122 $c1 = new TestComponent("foo");
123 $renderer = $this->getDefaultRenderer(
125 $this->getTemplateFactory()->getTemplate("tpl.main.html", false, false)
126 )
127 );
128 $html = $renderer->renderAsync($c1);
129 $this->assertEquals("foo", $html);
130 }
131
132 public function test_render_async_with_js(): void
133 {
134 $c1 = new JSTestComponent("foo");
135 $renderer = $this->getDefaultRenderer(
136 new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
137 );
138 $html = $renderer->renderAsync($c1);
139 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
140 }
141
142 public function test_render_async_with_js_twice(): void
143 {
144 $c1 = new TestComponent("foo");
145 $c2 = new JSTestComponent("foo");
146 $renderer = $this->getDefaultRenderer(
147 new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
148 );
149 $html = $renderer->renderAsync($c2);
150 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
151 $html = $renderer->renderAsync($c1);
152 $this->assertEquals("foo", $html);
153 $html = $renderer->renderAsync($c2);
154 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
155 }
156
157 public function test_render_async_array(): void
158 {
159 $c1 = new TestComponent("foo");
160
161 $renderer = $this->getDefaultRenderer(
162 new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
163 );
164 $html = $renderer->renderAsync([$c1,$c1]);
165 $this->assertEquals('foofoo', $html);
166 }
167
171 public function test_passes_self_as_root_if_no_root_exist($render_type)
172 {
173 $this->component_renderer = $this->createMock(ComponentRenderer::class);
174 $component = $this->createMock(C\Component::class);
175
176 $renderer = new class ($this) extends DefaultRenderer {
177 public function __construct($self)
178 {
179 $this->self = $self;
180 }
181
182 protected function getRendererFor(ILIAS\UI\Component\Component $component): ComponentRenderer
183 {
184 return $this->self->component_renderer;
185 }
186
187 protected function getJSCodeForAsyncRenderingFor(C\Component $component)
188 {
189 return "";
190 }
191 };
192
193 $this->component_renderer->expects($this->once())
194 ->method("render")
195 ->with($component, $renderer);
196
197 $renderer->$render_type($component);
198 }
199
203 public function test_passes_other_on_as_root($render_type)
204 {
205 $this->component_renderer = $this->createMock(ComponentRenderer::class);
206 $component = $this->createMock(C\Component::class);
207 $root = $this->createMock(Renderer::class);
208
209 $renderer = new class ($this) extends DefaultRenderer {
210 public function __construct($self)
211 {
212 $this->self = $self;
213 }
214
215 protected function getRendererFor(ILIAS\UI\Component\Component $component): ComponentRenderer
216 {
217 return $this->self->component_renderer;
218 }
219
220 protected function getJSCodeForAsyncRenderingFor(C\Component $component)
221 {
222 return "";
223 }
224 };
225
226 $this->component_renderer->expects($this->once())
227 ->method("render")
228 ->with($component, $root);
229
230 $renderer->$render_type($component, $root);
231 }
232
233 public function render_type()
234 {
235 return [
236 ["render"],
237 ["renderAsync"]
238 ];
239 }
240
242 {
243 $component = $this->createMock(C\Component::class);
244 $root = $this->createMock(Renderer::class);
245
246 $renderer = new class ($this) extends DefaultRenderer {
247 public function __construct($self)
248 {
249 $this->self = $self;
250 }
251 };
252
253 $root->expects($this->exactly(2))
254 ->method("render")
255 ->with($component)
256 ->willReturn(".");
257
258 $res = $renderer->render([$component, $component], $root);
259 $this->assertEquals("..", $res);
260 }
261}
test_passes_other_on_as_root($render_type)
@dataProvider render_type
test_passes_self_as_root_if_no_root_exist($render_type)
@dataProvider render_type
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:299
getTemplateFactory()
Definition: Base.php:315
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:355
An entity that renders components to a string output.
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.