[TASK] Shorten the extend class in all unit tests
[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 use TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer;
18 use TYPO3\CMS\Core\Resource\Rendering\RendererRegistry;
19 use TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class RendererRegistryTest extends UnitTestCase
26 {
27 /**
28 * Initialize a RendererRegistry and mock createRendererInstance()
29 *
30 * @param array $createsRendererInstances
31 * @return \PHPUnit_Framework_MockObject_MockObject|RendererRegistry
32 */
33 protected function getTestRendererRegistry(array $createsRendererInstances = [])
34 {
35 $rendererRegistry = $this->getMockBuilder(RendererRegistry::class)
36 ->setMethods(['createRendererInstance'])
37 ->getMock();
38
39 if (!empty($createsRendererInstances)) {
40 $rendererRegistry->expects($this->any())
41 ->method('createRendererInstance')
42 ->will($this->returnValueMap($createsRendererInstances));
43 }
44
45 return $rendererRegistry;
46 }
47
48 /**
49 * @test
50 */
51 public function registeredFileRenderClassCanBeRetrieved()
52 {
53 $rendererClass = $this->getUniqueId('myRenderer');
54 $rendererObject = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
55 ->setMockClassName($rendererClass)
56 ->getMock();
57
58 $rendererRegistry = $this->getTestRendererRegistry([[$rendererClass, $rendererObject]]);
59
60 $rendererRegistry->registerRendererClass($rendererClass);
61 $this->assertContains($rendererObject, $rendererRegistry->getRendererInstances(), '', false, false);
62 }
63
64 /**
65 * @test
66 */
67 public function registerRendererClassThrowsExceptionIfClassDoesNotExist()
68 {
69 $this->expectException(\InvalidArgumentException::class);
70 $this->expectExceptionCode(1411840171);
71
72 $rendererRegistry = $this->getTestRendererRegistry();
73 $rendererRegistry->registerRendererClass($this->getUniqueId());
74 }
75
76 /**
77 * @test
78 */
79 public function registerRendererClassThrowsExceptionIfClassDoesNotImplementRightInterface()
80 {
81 $this->expectException(\InvalidArgumentException::class);
82 $this->expectExceptionCode(1411840172);
83
84 $className = __CLASS__;
85 $rendererRegistry = $this->getTestRendererRegistry();
86 $rendererRegistry->registerRendererClass($className);
87 }
88
89 /**
90 * @test
91 */
92 public function registerRendererClassWithHighestPriorityIsFirstInResult()
93 {
94 $rendererClass1 = $this->getUniqueId('myRenderer1');
95 $rendererObject1 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
96 ->setMockClassName($rendererClass1)
97 ->getMock();
98 $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
99
100 $rendererClass2 = $this->getUniqueId('myRenderer2');
101 $rendererObject2 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
102 ->setMockClassName($rendererClass2)
103 ->getMock();
104 $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
105
106 $rendererClass3 = $this->getUniqueId('myRenderer3');
107 $rendererObject3 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
108 ->setMockClassName($rendererClass3)
109 ->getMock();
110 $rendererObject3->expects($this->any())->method('getPriority')->will($this->returnValue(2));
111
112 $createdRendererInstances = [
113 [$rendererClass1, $rendererObject1],
114 [$rendererClass2, $rendererObject2],
115 [$rendererClass3, $rendererObject3],
116 ];
117
118 $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
119 $rendererRegistry->registerRendererClass($rendererClass1);
120 $rendererRegistry->registerRendererClass($rendererClass2);
121 $rendererRegistry->registerRendererClass($rendererClass3);
122
123 $rendererInstances = $rendererRegistry->getRendererInstances();
124 $this->assertTrue($rendererInstances[0] instanceof $rendererClass2);
125 $this->assertTrue($rendererInstances[1] instanceof $rendererClass3);
126 $this->assertTrue($rendererInstances[2] instanceof $rendererClass1);
127 }
128
129 /**
130 * @test
131 */
132 public function registeredFileRendererClassWithSamePriorityAreAllReturned()
133 {
134 $rendererClass1 = $this->getUniqueId('myRenderer1');
135 $rendererObject1 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
136 ->setMockClassName($rendererClass1)
137 ->getMock();
138 $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
139
140 $rendererClass2 = $this->getUniqueId('myRenderer2');
141 $rendererObject2 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
142 ->setMockClassName($rendererClass2)
143 ->getMock();
144 $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(1));
145
146 $createdRendererInstances = [
147 [$rendererClass1, $rendererObject1],
148 [$rendererClass2, $rendererObject2],
149 ];
150
151 $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
152 $rendererRegistry->registerRendererClass($rendererClass1);
153 $rendererRegistry->registerRendererClass($rendererClass2);
154
155 $rendererInstances = $rendererRegistry->getRendererInstances();
156 $this->assertContains($rendererObject1, $rendererInstances);
157 $this->assertContains($rendererObject2, $rendererInstances);
158 }
159
160 /**
161 * @test
162 */
163 public function getRendererReturnsCorrectInstance()
164 {
165 $rendererClass1 = $this->getUniqueId('myVideoRenderer');
166 $rendererObject1 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
167 ->setMethods(['getPriority', 'canRender', 'render'])
168 ->setMockClassName($rendererClass1)
169 ->getMock();
170 $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
171 $rendererObject1->expects($this->once())->method('canRender')->will($this->returnValue(true));
172
173 $rendererClass2 = $this->getUniqueId('myAudioRenderer');
174 $rendererObject2 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class)
175 ->setMethods(['getPriority', 'canRender', 'render'])
176 ->setMockClassName($rendererClass2)
177 ->getMock();
178 $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
179 $rendererObject2->expects($this->once())->method('canRender')->will($this->returnValue(false));
180
181 $fileResourceMock = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
182
183 $createdRendererInstances = [
184 [$rendererClass1, $rendererObject1],
185 [$rendererClass2, $rendererObject2],
186 ];
187
188 $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
189 $rendererRegistry->registerRendererClass($rendererClass1);
190 $rendererRegistry->registerRendererClass($rendererClass2);
191
192 $rendererRegistry->getRendererInstances();
193
194 $renderer = $rendererRegistry->getRenderer($fileResourceMock);
195
196 $this->assertTrue($renderer instanceof $rendererClass1);
197 }
198
199 /**
200 * @test
201 */
202 public function getRendererReturnsCorrectInstance2()
203 {
204 $this->resetSingletonInstances = true;
205 $rendererRegistry = RendererRegistry::getInstance();
206 $rendererRegistry->registerRendererClass(AudioTagRenderer::class);
207 $rendererRegistry->registerRendererClass(VideoTagRenderer::class);
208
209 $fileResourceMock = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
210 $fileResourceMock->expects($this->any())->method('getMimeType')->will($this->returnValue('video/mp4'));
211
212 $rendererRegistry->getRendererInstances();
213
214 $renderer = $rendererRegistry->getRenderer($fileResourceMock);
215
216 $this->assertInstanceOf(VideoTagRenderer::class, $renderer);
217 }
218 }