[TASK] Removes extra empty lines
[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\CMS\Core\Tests\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 = array())
29 {
30 $rendererRegistry = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Rendering\RendererRegistry::class)
31 ->setMethods(array('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->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, array(), array(), $rendererClass);
50
51 $rendererRegistry = $this->getTestRendererRegistry(array(array($rendererClass, $rendererObject)));
52
53 $rendererRegistry->registerRendererClass($rendererClass);
54 $this->assertContains($rendererObject, $rendererRegistry->getRendererInstances(), '', false, false);
55 }
56
57 /**
58 * @test
59 * @expectedException \InvalidArgumentException
60 * @expectedExceptionCode 1411840171
61 */
62 public function registerRendererClassThrowsExceptionIfClassDoesNotExist()
63 {
64 $rendererRegistry = $this->getTestRendererRegistry();
65 $rendererRegistry->registerRendererClass($this->getUniqueId());
66 }
67
68 /**
69 * @test
70 * @expectedException \InvalidArgumentException
71 * @expectedExceptionCode 1411840172
72 */
73 public function registerRendererClassThrowsExceptionIfClassDoesNotImplementRightInterface()
74 {
75 $className = __CLASS__;
76 $rendererRegistry = $this->getTestRendererRegistry();
77 $rendererRegistry->registerRendererClass($className);
78 }
79
80 /**
81 * @test
82 */
83 public function registerRendererClassWithHighestPriorityIsFirstInResult()
84 {
85 $rendererClass1 = $this->getUniqueId('myRenderer1');
86 $rendererObject1 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, array(), array(), $rendererClass1);
87 $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
88
89 $rendererClass2 = $this->getUniqueId('myRenderer2');
90 $rendererObject2 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, array(), array(), $rendererClass2);
91 $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
92
93 $rendererClass3 = $this->getUniqueId('myRenderer3');
94 $rendererObject3 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, array(), array(), $rendererClass3);
95 $rendererObject3->expects($this->any())->method('getPriority')->will($this->returnValue(2));
96
97 $createdRendererInstances = array(
98 array($rendererClass1, $rendererObject1),
99 array($rendererClass2, $rendererObject2),
100 array($rendererClass3, $rendererObject3),
101 );
102
103 $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
104 $rendererRegistry->registerRendererClass($rendererClass1);
105 $rendererRegistry->registerRendererClass($rendererClass2);
106 $rendererRegistry->registerRendererClass($rendererClass3);
107
108 $rendererInstances = $rendererRegistry->getRendererInstances();
109 $this->assertTrue($rendererInstances[0] instanceof $rendererClass2);
110 $this->assertTrue($rendererInstances[1] instanceof $rendererClass3);
111 $this->assertTrue($rendererInstances[2] instanceof $rendererClass1);
112 }
113
114 /**
115 * @test
116 */
117 public function registeredFileRendererClassWithSamePriorityAreAllReturned()
118 {
119 $rendererClass1 = $this->getUniqueId('myRenderer1');
120 $rendererObject1 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, array(), array(), $rendererClass1);
121 $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
122
123 $rendererClass2 = $this->getUniqueId('myRenderer2');
124 $rendererObject2 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, array(), array(), $rendererClass2);
125 $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(1));
126
127 $createdRendererInstances = array(
128 array($rendererClass1, $rendererObject1),
129 array($rendererClass2, $rendererObject2),
130 );
131
132 $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
133 $rendererRegistry->registerRendererClass($rendererClass1);
134 $rendererRegistry->registerRendererClass($rendererClass2);
135
136 $rendererInstances = $rendererRegistry->getRendererInstances();
137 $this->assertContains($rendererObject1, $rendererInstances);
138 $this->assertContains($rendererObject2, $rendererInstances);
139 }
140
141 /**
142 * @test
143 */
144 public function getRendererReturnsCorrectInstance()
145 {
146 $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)');
147
148 $rendererClass1 = $this->getUniqueId('myVideoRenderer');
149 $rendererObject1 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, array('getPriority', 'canRender', 'render'), array(), $rendererClass1);
150 $rendererObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
151 $rendererObject1->expects($this->once())->method('canRender')->will($this->returnValue(true));
152
153 $rendererClass2 = $this->getUniqueId('myAudioRenderer');
154 $rendererObject2 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, array('getPriority', 'canRender', 'render'), array(), $rendererClass2);
155 $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
156 $rendererObject2->expects($this->once())->method('canRender')->will($this->returnValue(false));
157
158 $fileResourceMock = $this->getMock(\TYPO3\CMS\Core\Resource\File::class, array(), array(), '', false);
159
160 $createdRendererInstances = array(
161 array($rendererClass1, $rendererObject1),
162 array($rendererClass2, $rendererObject2),
163 );
164
165 $rendererRegistry = $this->getTestRendererRegistry($createdRendererInstances);
166 $rendererRegistry->registerRendererClass($rendererClass1);
167 $rendererRegistry->registerRendererClass($rendererClass2);
168
169 $rendererRegistry->getRendererInstances();
170
171 $renderer = $rendererRegistry->getRenderer($fileResourceMock);
172
173 $this->assertTrue($renderer instanceof $rendererClass1);
174 }
175
176 /**
177 * @test
178 */
179 public function getRendererReturnsCorrectInstance2()
180 {
181 $rendererRegistry = \TYPO3\CMS\Core\Resource\Rendering\RendererRegistry::getInstance();
182 $rendererRegistry->registerRendererClass(\TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer::class);
183 $rendererRegistry->registerRendererClass(\TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer::class);
184
185 $fileResourceMock = $this->getMock(\TYPO3\CMS\Core\Resource\File::class, array(), array(), '', false);
186 $fileResourceMock->expects($this->any())->method('getMimeType')->will($this->returnValue('video/mp4'));
187
188 $rendererRegistry->getRendererInstances();
189
190 $renderer = $rendererRegistry->getRenderer($fileResourceMock);
191
192 $this->assertInstanceOf(\TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer::class, $renderer);
193 }
194 }