[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Rendering / RendererRegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Rendering;
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
17 /**
18 * Test cases for RendererRegistry
19 */
20 class RendererRegistryTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
21 {
22 /**
23 * Initialize a RendererRegistry and mock createRendererInstance()
24 *
25 * @param array $createsRendererInstances
26 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
27 */
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
43 /**
44 * @test
45 */
46 public function registeredFileRenderClassCanBeRetrieved()
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
59 /**
60 * @test
61 */
62 public function registerRendererClassThrowsExceptionIfClassDoesNotExist()
63 {
64 $this->expectException(\InvalidArgumentException::class);
65 $this->expectExceptionCode(1411840171);
66
67 $rendererRegistry = $this->getTestRendererRegistry();
68 $rendererRegistry->registerRendererClass($this->getUniqueId());
69 }
70
71 /**
72 * @test
73 */
74 public function registerRendererClassThrowsExceptionIfClassDoesNotImplementRightInterface()
75 {
76 $this->expectException(\InvalidArgumentException::class);
77 $this->expectExceptionCode(1411840172);
78
79 $className = __CLASS__;
80 $rendererRegistry = $this->getTestRendererRegistry();
81 $rendererRegistry->registerRendererClass($className);
82 }
83
84 /**
85 * @test
86 */
87 public function registerRendererClassWithHighestPriorityIsFirstInResult()
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
124 /**
125 * @test
126 */
127 public function registeredFileRendererClassWithSamePriorityAreAllReturned()
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
155 /**
156 * @test
157 */
158 public function getRendererReturnsCorrectInstance()
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
196 /**
197 * @test
198 */
199 public function getRendererReturnsCorrectInstance2()
200 {
201 $rendererRegistry = \TYPO3\CMS\Core\Resource\Rendering\RendererRegistry::getInstance();
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 }