‪TYPO3CMS  9.5
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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21 
25 class ‪RendererRegistryTest extends UnitTestCase
26 {
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 
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 
68  {
69  $this->expectException(\InvalidArgumentException::class);
70  $this->expectExceptionCode(1411840171);
71 
73  ‪$rendererRegistry->registerRendererClass($this->getUniqueId());
74  }
75 
80  {
81  $this->expectException(\InvalidArgumentException::class);
82  $this->expectExceptionCode(1411840172);
83 
84  $className = __CLASS__;
86  ‪$rendererRegistry->registerRendererClass($className);
87  }
88 
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 
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 
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 
203  {
204  $this->resetSingletonInstances = true;
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 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registeredFileRenderClassCanBeRetrieved
‪registeredFileRenderClassCanBeRetrieved()
Definition: RendererRegistryTest.php:51
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registeredFileRendererClassWithSamePriorityAreAllReturned
‪registeredFileRendererClassWithSamePriorityAreAllReturned()
Definition: RendererRegistryTest.php:132
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer
Definition: VideoTagRenderer.php:25
‪TYPO3
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\getTestRendererRegistry
‪PHPUnit_Framework_MockObject_MockObject RendererRegistry getTestRendererRegistry(array $createsRendererInstances=[])
Definition: RendererRegistryTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registerRendererClassThrowsExceptionIfClassDoesNotExist
‪registerRendererClassThrowsExceptionIfClassDoesNotExist()
Definition: RendererRegistryTest.php:67
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\getRendererReturnsCorrectInstance2
‪getRendererReturnsCorrectInstance2()
Definition: RendererRegistryTest.php:202
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering
Definition: AudioTagRendererTest.php:3
‪TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer
Definition: AudioTagRenderer.php:25
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registerRendererClassThrowsExceptionIfClassDoesNotImplementRightInterface
‪registerRendererClassThrowsExceptionIfClassDoesNotImplementRightInterface()
Definition: RendererRegistryTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registerRendererClassWithHighestPriorityIsFirstInResult
‪registerRendererClassWithHighestPriorityIsFirstInResult()
Definition: RendererRegistryTest.php:92
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getInstance
‪static RendererRegistry getInstance()
Definition: RendererRegistry.php:42
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\getRendererReturnsCorrectInstance
‪getRendererReturnsCorrectInstance()
Definition: RendererRegistryTest.php:163
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest
Definition: RendererRegistryTest.php:26
‪$rendererRegistry
‪$rendererRegistry
Definition: ext_localconf.php:120
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
Definition: RendererRegistry.php:24