ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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;
33 
35 {
38 
39  public function testGetRendererSuccessfully(): void
40  {
41  // There should be a renderer for Glyph...
42  $dr = $this->getDefaultRenderer();
43  $r = $dr->_getRendererFor(new Glyph("up", "up"));
44  $this->assertInstanceOf(ComponentRenderer::class, $r);
45  }
46 
47  public function testGetRendererCaching(): void
48  {
49  $dr = $this->getDefaultRenderer();
50  $r1 = $dr->_getRendererFor(new Glyph("up", "up"));
51  $r2 = $dr->_getRendererFor(new Glyph("up", "up"));
52  $this->assertTrue($r1 === $r2, "Instances not equal");
53  }
54 
56  {
57  $this->resource_registry = parent::getResourceRegistry();
59  }
60 
61  public function testInvokesRegistry(): void
62  {
63  $dr = $this->getDefaultRenderer();
64  $component = new TestComponent("foo");
65 
66  $dr->render($component);
67 
68  $this->assertEquals(array("test.js"), $this->resource_registry->resources);
69  }
70 
71  public function testWithAdditionalContextClones(): void
72  {
73  $dr = $this->getDefaultRenderer();
74  $component = new TestComponent("foo");
75  $dr2 = $dr->withAdditionalContext($component);
76  $this->assertNotSame($dr, $dr2);
77  }
78 
79  public function testGetContexts(): void
80  {
81  $dr = $this->getDefaultRenderer();
82  $c1 = new TestComponent("foo");
83  $c2 = new TestComponent("bar");
84  $dr2 = $dr
85  ->withAdditionalContext($c1)
86  ->withAdditionalContext($c2);
87  $this->assertEquals([], $dr->_getContexts());
88  $this->assertEquals([$c1, $c2], $dr2->_getContexts());
89  }
90 
92  {
93  $loader = $this
94  ->getMockBuilder(Loader::class)
95  ->onlyMethods(["getRendererFor", "getRendererFactoryFor"])
96  ->getMock();
97 
98  $renderer = new TestDefaultRenderer($loader);
99 
100  $c1 = new TestComponent("foo");
101  $c2 = new TestComponent("bar");
102 
103  $renderer = $renderer
104  ->withAdditionalContext($c1)
105  ->withAdditionalContext($c2);
106 
107  $loader
108  ->expects($this->once())
109  ->method("getRendererFor")
110  ->with($c1, [$c1, $c2]);
111 
112  $renderer->_getRendererFor($c1);
113  }
114 
115  public function testRender(): void
116  {
117  $c1 = new TestComponent("foo");
118  $renderer = $this->getDefaultRenderer();
119  $html = $renderer->render($c1);
120  $this->assertEquals("foo", $html);
121  }
122 
123  public function testRenderAsyncNoJs(): void
124  {
125  $c1 = new TestComponent("foo");
126  $renderer = $this->getDefaultRenderer(
128  $this->getTemplateFactory()->getTemplate("tpl.main.html", false, false)
129  )
130  );
131  $html = $renderer->renderAsync($c1);
132  $this->assertEquals("foo", $html);
133  }
134 
135  public function testRenderAsyncWithJs(): void
136  {
137  $c1 = new JSTestComponent("foo");
138  $renderer = $this->getDefaultRenderer(
139  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
140  );
141  $html = $renderer->renderAsync($c1);
142  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
143  }
144 
145  public function testRenderAsyncWithJsTwice(): void
146  {
147  $c1 = new TestComponent("foo");
148  $c2 = new JSTestComponent("foo");
149  $renderer = $this->getDefaultRenderer(
150  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
151  );
152  $html = $renderer->renderAsync($c2);
153  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
154  $html = $renderer->renderAsync($c1);
155  $this->assertEquals("foo", $html);
156  $html = $renderer->renderAsync($c2);
157  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
158  }
159 
160  public function testRenderAsyncArray(): void
161  {
162  $c1 = new TestComponent("foo");
163 
164  $renderer = $this->getDefaultRenderer(
165  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
166  );
167  $html = $renderer->renderAsync([$c1,$c1]);
168  $this->assertEquals('foofoo', $html);
169  }
170 
174  public function testPassesSelfAsRootIfNoRootExist($render_type)
175  {
176  $this->component_renderer = $this->createMock(ComponentRenderer::class);
177  $component = $this->createMock(C\Component::class);
178 
179  $renderer = new class ($this) extends DefaultRenderer {
180  public function __construct(
181  protected DefaultRendererTest $self
182  ) {
183  }
184 
185  protected function getRendererFor(ILIAS\UI\Component\Component $component): ComponentRenderer
186  {
187  return $this->self->component_renderer;
188  }
189 
190  protected function getJSCodeForAsyncRenderingFor(C\Component $component)
191  {
192  return "";
193  }
194  };
195 
196  $this->component_renderer->expects($this->once())
197  ->method("render")
198  ->with($component, $renderer);
199 
200  $renderer->$render_type($component);
201  }
202 
206  public function testPassesOtherOnAsRoot($render_type)
207  {
208  $this->component_renderer = $this->createMock(ComponentRenderer::class);
209  $component = $this->createMock(C\Component::class);
210  $root = $this->createMock(Renderer::class);
211 
212  $renderer = new class ($this) extends DefaultRenderer {
213  public function __construct(
214  protected DefaultRendererTest $self
215  ) {
216  }
217 
218  protected function getRendererFor(ILIAS\UI\Component\Component $component): ComponentRenderer
219  {
220  return $this->self->component_renderer;
221  }
222 
223  protected function getJSCodeForAsyncRenderingFor(C\Component $component)
224  {
225  return "";
226  }
227  };
228 
229  $this->component_renderer->expects($this->once())
230  ->method("render")
231  ->with($component, $root);
232 
233  $renderer->$render_type($component, $root);
234  }
235 
236  public function getRenderType()
237  {
238  return [
239  ["render"],
240  ["renderAsync"]
241  ];
242  }
243 
245  {
246  $component = $this->createMock(C\Component::class);
247  $root = $this->createMock(Renderer::class);
248 
249  $renderer = $this->getDefaultRenderer();
250 
251  $root->expects($this->exactly(2))
252  ->method("render")
253  ->with($component)
254  ->willReturn(".");
255 
256  $res = $renderer->render([$component, $component], $root);
257  $this->assertEquals("..", $res);
258  }
259 }
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:377
$res
Definition: ltiservices.php:69
Class ChatMainBarProvider .
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...
ComponentRenderer $component_renderer
__construct(VocabulariesInterface $vocabularies)
Provides common functionality for UI tests.
Definition: Base.php:310
testPassesSelfAsRootIfNoRootExist($render_type)
getRenderType
LoggingRegistry $resource_registry
getTemplateFactory()
Definition: Base.php:327
testPassesOtherOnAsRoot($render_type)
getRenderType
$r