ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
DefaultRendererTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/TestComponent.php");
22 require_once(__DIR__ . "/../Base.php");
23 
30 use ILIAS\UI\Component as C;
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 ($component_renderer) extends DefaultRenderer {
102  public array $context_history = [];
103 
104  public function __construct(
105  protected ComponentRenderer $component_renderer
106  ) {
107  }
108 
109  protected function getRendererFor(Component $component): ComponentRenderer
110  {
112  }
113 
114  protected function pushContext(Component $component): void
115  {
116  parent::pushContext($component);
117  $this->context_history[] = $this->getContexts();
118  }
119 
120  protected function popContext(): void
121  {
122  parent::popContext();
123  $this->context_history[] = $this->getContexts();
124  }
125  };
126 
127  $expected_context_history = [
128  [$component1],
129  [$component1, $component2],
130  [$component1, $component2, $component3],
131  [$component1, $component2],
132  [$component1],
133  []
134  ];
135 
136  $expected_html =
137  $component1->text . $glue .
138  $component2->text . $glue .
139  $component3->text;
140 
141  $html = $renderer->render($component1);
142 
143  $this->assertEquals($expected_context_history, $renderer->context_history);
144  $this->assertEquals($expected_html, $html);
145  }
146 
147  public function testRender(): void
148  {
149  $c1 = new TestComponent("foo");
150  $renderer = $this->getDefaultRenderer();
151  $html = $renderer->render($c1);
152  $this->assertEquals("foo", $html);
153  }
154 
155  public function testRenderAsyncNoJs(): void
156  {
157  $c1 = new TestComponent("foo");
158  $renderer = $this->getDefaultRenderer(
160  $this->getTemplateFactory()->getTemplate("tpl.main.html", false, false)
161  )
162  );
163  $html = $renderer->renderAsync($c1);
164  $this->assertEquals("foo", $html);
165  }
166 
167  public function testRenderAsyncWithJs(): void
168  {
169  $c1 = new JSTestComponent("foo");
170  $renderer = $this->getDefaultRenderer(
171  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
172  );
173  $html = $renderer->renderAsync($c1);
174  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
175  }
176 
177  public function testRenderAsyncWithJsTwice(): void
178  {
179  $c1 = new TestComponent("foo");
180  $c2 = new JSTestComponent("foo");
181  $renderer = $this->getDefaultRenderer(
182  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
183  );
184  $html = $renderer->renderAsync($c2);
185  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
186  $html = $renderer->renderAsync($c1);
187  $this->assertEquals("foo", $html);
188  $html = $renderer->renderAsync($c2);
189  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
190  }
191 
192  public function testRenderAsyncArray(): void
193  {
194  $c1 = new TestComponent("foo");
195 
196  $renderer = $this->getDefaultRenderer(
197  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
198  );
199  $html = $renderer->renderAsync([$c1,$c1]);
200  $this->assertEquals('foofoo', $html);
201  }
202 
206  public function testPassesSelfAsRootIfNoRootExist($render_type)
207  {
208  $this->component_renderer = $this->createMock(ComponentRenderer::class);
209  $component = $this->createMock(C\Component::class);
210 
211  $loader = $this->createMock(Loader::class);
212  $loader->method('getRendererFor')->willReturn($this->component_renderer);
213 
214  $renderer = new TestDefaultRenderer($loader, $this->getJavaScriptBinding());
215 
216  $this->component_renderer->expects($this->once())
217  ->method("render")
218  ->with($component, $renderer);
219 
220  $renderer->$render_type($component);
221  }
222 
226  public function testPassesOtherOnAsRoot($render_type)
227  {
228  $this->component_renderer = $this->createMock(ComponentRenderer::class);
229  $component = $this->createMock(C\Component::class);
230  $root = $this->createMock(Renderer::class);
231 
232  $loader = $this->createMock(Loader::class);
233  $loader->method('getRendererFor')->willReturn($this->component_renderer);
234 
235  $renderer = new TestDefaultRenderer($loader, $this->getJavaScriptBinding());
236 
237  $this->component_renderer->expects($this->once())
238  ->method("render")
239  ->with($component, $root);
240 
241  $renderer->$render_type($component, $root);
242  }
243 
244  public static function getRenderType()
245  {
246  return [
247  ["render"],
248  ["renderAsync"]
249  ];
250  }
251 
253  {
254  $component = $this->createMock(C\Component::class);
255  $root = $this->createMock(Renderer::class);
256 
257  $renderer = $this->getDefaultRenderer();
258 
259  $root->expects($this->exactly(2))
260  ->method("render")
261  ->with($component)
262  ->willReturn(".");
263 
264  $res = $renderer->render([$component, $component], $root);
265  $this->assertEquals("..", $res);
266  }
267 }
render($component, ?Renderer $root=null)
Render given component.If an array of components is passed, this method returns a concatenated output...
$res
Definition: ltiservices.php:69
$renderer
An entity that renders components to a string output.
Wraps global ilTemplate to provide JavaScriptBinding.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testRenderingChainAndContextStack()
Simulates the rendering chain and tests if the contexts are properly stacked.
ComponentRenderer $component_renderer
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testPassesSelfAsRootIfNoRootExist($render_type)
getRenderType
LoggingRegistry $resource_registry
testPassesOtherOnAsRoot($render_type)
getRenderType
__construct(Container $dic, ilPlugin $plugin)
$r