TYPO3 CMS  TYPO3_7-6
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 
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->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, [], [], $rendererClass);
50 
51  $rendererRegistry = $this->getTestRendererRegistry([[$rendererClass, $rendererObject]]);
52 
53  $rendererRegistry->registerRendererClass($rendererClass);
54  $this->assertContains($rendererObject, $rendererRegistry->getRendererInstances(), '', false, false);
55  }
56 
63  {
65  $rendererRegistry->registerRendererClass($this->getUniqueId());
66  }
67 
74  {
75  $className = __CLASS__;
77  $rendererRegistry->registerRendererClass($className);
78  }
79 
84  {
85  $rendererClass1 = $this->getUniqueId('myRenderer1');
86  $rendererObject1 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, [], [], $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, [], [], $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, [], [], $rendererClass3);
95  $rendererObject3->expects($this->any())->method('getPriority')->will($this->returnValue(2));
96 
97  $createdRendererInstances = [
98  [$rendererClass1, $rendererObject1],
99  [$rendererClass2, $rendererObject2],
100  [$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 
118  {
119  $rendererClass1 = $this->getUniqueId('myRenderer1');
120  $rendererObject1 = $this->getMock(\TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface::class, [], [], $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, [], [], $rendererClass2);
125  $rendererObject2->expects($this->any())->method('getPriority')->will($this->returnValue(1));
126 
127  $createdRendererInstances = [
128  [$rendererClass1, $rendererObject1],
129  [$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 
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, ['getPriority', 'canRender', 'render'], [], $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, ['getPriority', 'canRender', 'render'], [], $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, [], [], '', false);
159 
160  $createdRendererInstances = [
161  [$rendererClass1, $rendererObject1],
162  [$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 
180  {
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, [], [], '', 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 }
$rendererRegistry