TYPO3 CMS  TYPO3_8-7
RendererRegistryTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 class RendererRegistryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
28  protected function getTestRendererRegistry(array $createsRendererInstances = [])
29  {
30  $rendererRegistry = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\RendererRegistry::class)
31  ->setMethods(['createRendererInstance'])
32  ->getMock();
33 
34  if (!empty($createsRendererInstances)) {
35  $rendererRegistry->expects($this->any())
36  ->method('createRendererInstance')
37  ->will($this->returnValueMap($createsRendererInstances));
38  }
39 
40  return $rendererRegistry;
41  }
42 
47  {
48  $rendererClass = $this->getUniqueId('myRenderer');
49  $rendererObject = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
50  ->setMockClassName($rendererClass)
51  ->getMock();
52 
53  $rendererRegistry = $this->getTestRendererRegistry([[$rendererClass, $rendererObject]]);
54 
55  $rendererRegistry->registerRendererClass($rendererClass);
56  $this->assertContains($rendererObject, $rendererRegistry->getRendererInstances(), '', false, false);
57  }
58 
63  {
64  $this->expectException(\InvalidArgumentException::class);
65  $this->expectExceptionCode(1411840171);
66 
68  $rendererRegistry->registerRendererClass($this->getUniqueId());
69  }
70 
75  {
76  $this->expectException(\InvalidArgumentException::class);
77  $this->expectExceptionCode(1411840172);
78 
79  $className = __CLASS__;
81  $rendererRegistry->registerRendererClass($className);
82  }
83 
88  {
89  $rendererClass1 = $this->getUniqueId('myRenderer1');
90  $rendererObject1 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
91  ->setMockClassName($rendererClass1)
92  ->getMock();
93  $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
94 
95  $rendererClass2 = $this->getUniqueId('myRenderer2');
96  $rendererObject2 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
97  ->setMockClassName($rendererClass2)
98  ->getMock();
99  $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
100 
101  $rendererClass3 = $this->getUniqueId('myRenderer3');
102  $rendererObject3 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
103  ->setMockClassName($rendererClass3)
104  ->getMock();
105  $rendererObject3->expects($this->any())->method('getPriority')->will($this->returnValue(2));
106 
107  $createdRendererInstances = [
108  [$rendererClass1, $rendererObject1],
109  [$rendererClass2, $rendererObject2],
110  [$rendererClass3, $rendererObject3],
111  ];
112 
113  $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
114  $rendererRegistry->registerRendererClass($rendererClass1);
115  $rendererRegistry->registerRendererClass($rendererClass2);
116  $rendererRegistry->registerRendererClass($rendererClass3);
117 
118  $rendererInstances = $rendererRegistry->getRendererInstances();
119  $this->assertTrue($rendererInstances[0] instanceof $rendererClass2);
120  $this->assertTrue($rendererInstances[1] instanceof $rendererClass3);
121  $this->assertTrue($rendererInstances[2] instanceof $rendererClass1);
122  }
123 
128  {
129  $rendererClass1 = $this->getUniqueId('myRenderer1');
130  $rendererObject1 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
131  ->setMockClassName($rendererClass1)
132  ->getMock();
133  $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
134 
135  $rendererClass2 = $this->getUniqueId('myRenderer2');
136  $rendererObject2 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
137  ->setMockClassName($rendererClass2)
138  ->getMock();
139  $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(1));
140 
141  $createdRendererInstances = [
142  [$rendererClass1, $rendererObject1],
143  [$rendererClass2, $rendererObject2],
144  ];
145 
146  $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
147  $rendererRegistry->registerRendererClass($rendererClass1);
148  $rendererRegistry->registerRendererClass($rendererClass2);
149 
150  $rendererInstances = $rendererRegistry->getRendererInstances();
151  $this->assertContains($rendererObject1, $rendererInstances);
152  $this->assertContains($rendererObject2, $rendererInstances);
153  }
154 
159  {
160  $this->markTestSkipped('Test triggers an error. This is a known PHP bug (http://stackoverflow.com/questions/3235387/usort-array-was-modified-by-the-user-comparison-function)');
161 
162  $rendererClass1 = $this->getUniqueId('myVideoRenderer');
163  $rendererObject1 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
164  ->setMethods(['getPriority', 'canRender', 'render'])
165  ->setMockClassName($rendererClass1)
166  ->getMock();
167  $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
168  $rendererObject1->expects($this->once())->method('canRender')->will($this->returnValue(true));
169 
170  $rendererClass2 = $this->getUniqueId('myAudioRenderer');
171  $rendererObject2 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
172  ->setMethods(['getPriority', 'canRender', 'render'])
173  ->setMockClassName($rendererClass2)
174  ->getMock();
175  $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
176  $rendererObject2->expects($this->once())->method('canRender')->will($this->returnValue(false));
177 
178  $fileResourceMock = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
179 
180  $createdRendererInstances = [
181  [$rendererClass1, $rendererObject1],
182  [$rendererClass2, $rendererObject2],
183  ];
184 
185  $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
186  $rendererRegistry->registerRendererClass($rendererClass1);
187  $rendererRegistry->registerRendererClass($rendererClass2);
188 
189  $rendererRegistry->getRendererInstances();
190 
191  $renderer = $rendererRegistry->getRenderer($fileResourceMock);
192 
193  $this->assertTrue($renderer instanceof $rendererClass1);
194  }
195 
200  {
202  $rendererRegistry->registerRendererClass(\TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer::class);
203  $rendererRegistry->registerRendererClass(\TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer::class);
204 
205  $fileResourceMock = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
206  $fileResourceMock->expects($this->any())->method('getMimeType')->will($this->returnValue('video/mp4'));
207 
208  $rendererRegistry->getRendererInstances();
209 
210  $renderer = $rendererRegistry->getRenderer($fileResourceMock);
211 
212  $this->assertInstanceOf(\TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer::class, $renderer);
213  }
214 }
$rendererRegistry