ILIAS  release_7 Revision v7.30-3-g800a261c036
DefaultRendererTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5require_once(__DIR__ . "/TestComponent.php");
6require_once(__DIR__ . "/../Base.php");
7
8use \ILIAS\UI\Component as C;
9use \ILIAS\UI\Implementation\DefaultRenderer;
10use \ILIAS\UI\Implementation\Glyph\Renderer as GlyphRenderer;
11use \ILIAS\UI\Renderer;
12use \ILIAS\UI\Implementation\Render\ComponentRenderer;
13
15{
17 {
18 // There should be a renderer for Glyph...
19 $dr = $this->getDefaultRenderer();
20 $r = $dr->_getRendererFor(new \ILIAS\UI\Implementation\Component\Symbol\Glyph\Glyph("up", "up"));
21 $this->assertInstanceOf(\ILIAS\UI\Implementation\Render\ComponentRenderer::class, $r);
22 }
23
24 public function test_getRenderer_caching()
25 {
26 $dr = $this->getDefaultRenderer();
27 $r1 = $dr->_getRendererFor(new \ILIAS\UI\Implementation\Component\Symbol\Glyph\Glyph("up", "up"));
28 $r2 = $dr->_getRendererFor(new \ILIAS\UI\Implementation\Component\Symbol\Glyph\Glyph("up", "up"));
29 $this->assertTrue($r1 === $r2, "Instances not equal");
30 }
31
32 public function getResourceRegistry()
33 {
34 $this->resource_registry = parent::getResourceRegistry();
35 return $this->resource_registry;
36 }
37
38 public function test_invokesRegistry()
39 {
40 $dr = $this->getDefaultRenderer();
41 $component = new \ILIAS\UI\Component\Test\TestComponent("foo");
42
43 $dr->render($component);
44
45 $this->assertEquals(array("test.js"), $this->resource_registry->resources);
46 }
47
49 {
50 $dr = $this->getDefaultRenderer();
51 $component = new \ILIAS\UI\Component\Test\TestComponent("foo");
52 $dr2 = $dr->withAdditionalContext($component);
53 $this->assertNotSame($dr, $dr2);
54 }
55
56 public function test_getContexts()
57 {
58 $dr = $this->getDefaultRenderer();
59 $c1 = new \ILIAS\UI\Component\Test\TestComponent("foo");
60 $c2 = new \ILIAS\UI\Component\Test\TestComponent("bar");
61 $dr2 = $dr
62 ->withAdditionalContext($c1)
63 ->withAdditionalContext($c2);
64 $this->assertEquals([], $dr->_getContexts());
65 $this->assertEquals([$c1, $c2], $dr2->_getContexts());
66 }
67
69 {
70 $loader = $this
71 ->getMockBuilder(\ILIAS\UI\Implementation\Render\Loader::class)
72 ->setMethods(["getRendererFor", "getRendererFactoryFor"])
73 ->getMock();
74
75 $renderer = new TestDefaultRenderer($loader);
76
77 $c1 = new \ILIAS\UI\Component\Test\TestComponent("foo");
78 $c2 = new \ILIAS\UI\Component\Test\TestComponent("bar");
79
80 $renderer = $renderer
81 ->withAdditionalContext($c1)
82 ->withAdditionalContext($c2);
83
84 $loader
85 ->expects($this->once())
86 ->method("getRendererFor")
87 ->with($c1, [$c1, $c2]);
88
89 $renderer->_getRendererFor($c1);
90 }
91
92 public function test_render()
93 {
94 $c1 = new \ILIAS\UI\Component\Test\TestComponent("foo");
95 $renderer = $this->getDefaultRenderer();
96 $html = $renderer->render($c1);
97 $this->assertEquals("foo", $html);
98 }
99
100 public function test_render_async_no_js()
101 {
102 $c1 = new \ILIAS\UI\Component\Test\TestComponent("foo");
103 $renderer = $this->getDefaultRenderer(
104 new \ILIAS\UI\Implementation\Render\ilJavaScriptBinding(
105 $this->getTemplateFactory()->getTemplate("tpl.main.html", false, false)
106 )
107 );
108 $html = $renderer->renderAsync($c1);
109 $this->assertEquals("foo", $html);
110 }
111
113 {
114 $c1 = new \ILIAS\UI\Component\Test\JSTestComponent("foo");
115 $renderer = $this->getDefaultRenderer(
116 new \ILIAS\UI\Implementation\Render\ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
117 );
118 $html = $renderer->renderAsync($c1);
119 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
120 }
121
123 {
124 $c1 = new \ILIAS\UI\Component\Test\TestComponent("foo");
125 $c2 = new \ILIAS\UI\Component\Test\JSTestComponent("foo");
126 $renderer = $this->getDefaultRenderer(
127 new \ILIAS\UI\Implementation\Render\ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
128 );
129 $html = $renderer->renderAsync($c2);
130 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
131 $html = $renderer->renderAsync($c1);
132 $this->assertEquals("foo", $html);
133 $html = $renderer->renderAsync($c2);
134 $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
135 }
136
137 public function test_render_async_array()
138 {
139 $c1 = new \ILIAS\UI\Component\Test\TestComponent("foo");
140
141 $renderer = $this->getDefaultRenderer(
142 new \ILIAS\UI\Implementation\Render\ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
143 );
144 $html = $renderer->renderAsync([$c1,$c1]);
145 $this->assertEquals('foofoo', $html);
146 }
147
151 public function test_passes_self_as_root_if_no_root_exist($render_type)
152 {
153 $this->component_renderer = $this->createMock(ComponentRenderer::class);
154 $component = $this->createMock(C\Component::class);
155
156 $renderer = new class($this) extends DefaultRenderer {
157 public function __construct($self)
158 {
159 $this->self = $self;
160 }
161
162 protected function getRendererFor(C\Component $component)
163 {
164 return $this->self->component_renderer;
165 }
166
167 protected function getJSCodeForAsyncRenderingFor(C\Component $component)
168 {
169 return "";
170 }
171 };
172
173 $this->component_renderer->expects($this->once())
174 ->method("render")
175 ->with($component, $renderer);
176
177 $renderer->$render_type($component);
178 }
179
183 public function test_passes_other_on_as_root($render_type)
184 {
185 $this->component_renderer = $this->createMock(ComponentRenderer::class);
186 $component = $this->createMock(C\Component::class);
187 $root = $this->createMock(Renderer::class);
188
189 $renderer = new class($this) extends DefaultRenderer {
190 public function __construct($self)
191 {
192 $this->self = $self;
193 }
194
195 protected function getRendererFor(C\Component $component)
196 {
197 return $this->self->component_renderer;
198 }
199
200 protected function getJSCodeForAsyncRenderingFor(C\Component $component)
201 {
202 return "";
203 }
204 };
205
206 $this->component_renderer->expects($this->once())
207 ->method("render")
208 ->with($component, $root);
209
210 $renderer->$render_type($component, $root);
211 }
212
213 public function render_type()
214 {
215 return [
216 ["render"],
217 ["renderAsync"]
218 ];
219 }
220
222 {
223 $component = $this->createMock(C\Component::class);
224 $root = $this->createMock(Renderer::class);
225
226 $renderer = new class($this) extends DefaultRenderer {
227 public function __construct($self)
228 {
229 $this->self = $self;
230 }
231 };
232
233 $root->expects($this->exactly(2))
234 ->method("render")
235 ->with($component)
236 ->willReturn(".");
237
238 $res = $renderer->render([$component, $component], $root);
239 $this->assertEquals("..", $res);
240 }
241}
An exception for terminatinating execution or to throw for unit testing.
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 ...
Provides common functionality for UI tests.
Definition: Base.php:263
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311
getTemplateFactory()
Definition: Base.php:279
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.
foreach($_POST as $key=> $value) $res