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