‪TYPO3CMS  10.4
RendererRegistryTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪RendererRegistryTest extends UnitTestCase
30 {
37  protected function ‪getTestRendererRegistry(array $createsRendererInstances = [])
38  {
39  ‪$rendererRegistry = $this->getMockBuilder(RendererRegistry::class)
40  ->setMethods(['createRendererInstance'])
41  ->getMock();
42 
43  if (!empty($createsRendererInstances)) {
44  ‪$rendererRegistry->expects(self::any())
45  ->method('createRendererInstance')
46  ->willReturnMap($createsRendererInstances);
47  }
48 
49  return ‪$rendererRegistry;
50  }
51 
56  {
57  $rendererClass = ‪StringUtility::getUniqueId('myRenderer');
58  $rendererObject = $this->getMockBuilder(FileRendererInterface::class)
59  ->setMockClassName($rendererClass)
60  ->getMock();
61 
62  ‪$rendererRegistry = $this->‪getTestRendererRegistry([[$rendererClass, $rendererObject]]);
63 
64  ‪$rendererRegistry->registerRendererClass($rendererClass);
65  self::assertContains($rendererObject, ‪$rendererRegistry->getRendererInstances());
66  }
67 
72  {
73  $this->expectException(\InvalidArgumentException::class);
74  $this->expectExceptionCode(1411840171);
75 
77  ‪$rendererRegistry->registerRendererClass(‪StringUtility::getUniqueId());
78  }
79 
84  {
85  $this->expectException(\InvalidArgumentException::class);
86  $this->expectExceptionCode(1411840172);
87 
88  $className = __CLASS__;
90  ‪$rendererRegistry->registerRendererClass($className);
91  }
92 
97  {
98  $rendererClass1 = ‪StringUtility::getUniqueId('myRenderer1');
99  $rendererObject1 = $this->getMockBuilder(FileRendererInterface::class)
100  ->setMockClassName($rendererClass1)
101  ->getMock();
102  $rendererObject1->expects(self::any())->method('getPriority')->willReturn(1);
103 
104  $rendererClass2 = ‪StringUtility::getUniqueId('myRenderer2');
105  $rendererObject2 = $this->getMockBuilder(FileRendererInterface::class)
106  ->setMockClassName($rendererClass2)
107  ->getMock();
108  $rendererObject2->expects(self::any())->method('getPriority')->willReturn(10);
109 
110  $rendererClass3 = ‪StringUtility::getUniqueId('myRenderer3');
111  $rendererObject3 = $this->getMockBuilder(FileRendererInterface::class)
112  ->setMockClassName($rendererClass3)
113  ->getMock();
114  $rendererObject3->expects(self::any())->method('getPriority')->willReturn(2);
115 
116  $createdRendererInstances = [
117  [$rendererClass1, $rendererObject1],
118  [$rendererClass2, $rendererObject2],
119  [$rendererClass3, $rendererObject3],
120  ];
121 
122  ‪$rendererRegistry = $this->‪getTestRendererRegistry($createdRendererInstances);
123  ‪$rendererRegistry->registerRendererClass($rendererClass1);
124  ‪$rendererRegistry->registerRendererClass($rendererClass2);
125  ‪$rendererRegistry->registerRendererClass($rendererClass3);
126 
127  $rendererInstances = ‪$rendererRegistry->getRendererInstances();
128  self::assertTrue($rendererInstances[0] instanceof $rendererClass2);
129  self::assertTrue($rendererInstances[1] instanceof $rendererClass3);
130  self::assertTrue($rendererInstances[2] instanceof $rendererClass1);
131  }
132 
137  {
138  $rendererClass1 = ‪StringUtility::getUniqueId('myRenderer1');
139  $rendererObject1 = $this->getMockBuilder(FileRendererInterface::class)
140  ->setMockClassName($rendererClass1)
141  ->getMock();
142  $rendererObject1->expects(self::any())->method('getPriority')->willReturn(1);
143 
144  $rendererClass2 = ‪StringUtility::getUniqueId('myRenderer2');
145  $rendererObject2 = $this->getMockBuilder(FileRendererInterface::class)
146  ->setMockClassName($rendererClass2)
147  ->getMock();
148  $rendererObject2->expects(self::any())->method('getPriority')->willReturn(1);
149 
150  $createdRendererInstances = [
151  [$rendererClass1, $rendererObject1],
152  [$rendererClass2, $rendererObject2],
153  ];
154 
155  ‪$rendererRegistry = $this->‪getTestRendererRegistry($createdRendererInstances);
156  ‪$rendererRegistry->registerRendererClass($rendererClass1);
157  ‪$rendererRegistry->registerRendererClass($rendererClass2);
158 
159  $rendererInstances = ‪$rendererRegistry->getRendererInstances();
160  self::assertContains($rendererObject1, $rendererInstances);
161  self::assertContains($rendererObject2, $rendererInstances);
162  }
163 
168  {
169  $rendererClass1 = ‪StringUtility::getUniqueId('myVideoRenderer');
170  $rendererObject1 = $this->getMockBuilder(FileRendererInterface::class)
171  ->setMethods(['getPriority', 'canRender', 'render'])
172  ->setMockClassName($rendererClass1)
173  ->getMock();
174  $rendererObject1->expects(self::any())->method('getPriority')->willReturn(1);
175  $rendererObject1->expects(self::once())->method('canRender')->willReturn(true);
176 
177  $rendererClass2 = ‪StringUtility::getUniqueId('myAudioRenderer');
178  $rendererObject2 = $this->getMockBuilder(FileRendererInterface::class)
179  ->setMethods(['getPriority', 'canRender', 'render'])
180  ->setMockClassName($rendererClass2)
181  ->getMock();
182  $rendererObject2->expects(self::any())->method('getPriority')->willReturn(10);
183  $rendererObject2->expects(self::once())->method('canRender')->willReturn(false);
184 
185  $fileResourceMock = $this->createMock(File::class);
186 
187  $createdRendererInstances = [
188  [$rendererClass1, $rendererObject1],
189  [$rendererClass2, $rendererObject2],
190  ];
191 
192  ‪$rendererRegistry = $this->‪getTestRendererRegistry($createdRendererInstances);
193  ‪$rendererRegistry->registerRendererClass($rendererClass1);
194  ‪$rendererRegistry->registerRendererClass($rendererClass2);
195 
196  ‪$rendererRegistry->getRendererInstances();
197 
198  $renderer = ‪$rendererRegistry->getRenderer($fileResourceMock);
199 
200  self::assertTrue($renderer instanceof $rendererClass1);
201  }
202 
207  {
208  $this->resetSingletonInstances = true;
210  ‪$rendererRegistry->registerRendererClass(AudioTagRenderer::class);
211  ‪$rendererRegistry->registerRendererClass(VideoTagRenderer::class);
212 
213  $fileResourceMock = $this->createMock(File::class);
214  $fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('video/mp4');
215 
216  ‪$rendererRegistry->getRendererInstances();
217 
218  $renderer = ‪$rendererRegistry->getRenderer($fileResourceMock);
219 
220  self::assertInstanceOf(VideoTagRenderer::class, $renderer);
221  }
222 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registeredFileRenderClassCanBeRetrieved
‪registeredFileRenderClassCanBeRetrieved()
Definition: RendererRegistryTest.php:55
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registeredFileRendererClassWithSamePriorityAreAllReturned
‪registeredFileRendererClassWithSamePriorityAreAllReturned()
Definition: RendererRegistryTest.php:136
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer
Definition: VideoTagRenderer.php:26
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registerRendererClassThrowsExceptionIfClassDoesNotExist
‪registerRendererClassThrowsExceptionIfClassDoesNotExist()
Definition: RendererRegistryTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\getRendererReturnsCorrectInstance2
‪getRendererReturnsCorrectInstance2()
Definition: RendererRegistryTest.php:206
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering
Definition: AudioTagRendererTest.php:18
‪TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer
Definition: AudioTagRenderer.php:26
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registerRendererClassThrowsExceptionIfClassDoesNotImplementRightInterface
‪registerRendererClassThrowsExceptionIfClassDoesNotImplementRightInterface()
Definition: RendererRegistryTest.php:83
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\registerRendererClassWithHighestPriorityIsFirstInResult
‪registerRendererClassWithHighestPriorityIsFirstInResult()
Definition: RendererRegistryTest.php:96
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\getTestRendererRegistry
‪PHPUnit Framework MockObject MockObject RendererRegistry getTestRendererRegistry(array $createsRendererInstances=[])
Definition: RendererRegistryTest.php:37
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getInstance
‪static RendererRegistry getInstance()
Definition: RendererRegistry.php:44
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest\getRendererReturnsCorrectInstance
‪getRendererReturnsCorrectInstance()
Definition: RendererRegistryTest.php:167
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:25
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\RendererRegistryTest
Definition: RendererRegistryTest.php:30
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪$rendererRegistry
‪$rendererRegistry
Definition: ext_localconf.php:19
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
Definition: RendererRegistry.php:26