[TASK] Create new processor registry
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Processing / ProcessorRegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Processing;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2014 Frans Saris <franssaris@gmail.com>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Test cases for ProcessorRegistry
32 */
33 class ProcessorRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
34
35 public function setUp() {
36 parent::setUp();
37 unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['fileProcessors']);
38 }
39
40 /**
41 * Initialize an FileProcessorRegistry and mock createFileProcessorInstance()
42 *
43 * @param array $createdFileProcessorInstances
44 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Resource\Processing\FileProcessorRegistry
45 */
46 protected function getTestFileProcessorRegistry(array $createdFileProcessorInstances = array()) {
47 $fileProcessorRegistry = $this->getMockBuilder('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorRegistry')
48 ->setMethods(array('createFileProcessorInstance'))
49 ->getMock();
50
51 if (count($createdFileProcessorInstances)) {
52 $fileProcessorRegistry->expects($this->any())
53 ->method('createFileProcessorInstance')
54 ->will($this->returnValueMap($createdFileProcessorInstances));
55 }
56
57 return $fileProcessorRegistry;
58 }
59
60 /**
61 * @test
62 */
63 public function registeredFileProcessorClassCanBeRetrieved() {
64 $processorClass = uniqid('myProcessor');
65 $processorObject = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass);
66
67 $fileProcessorRegistry = $this->getTestFileProcessorRegistry(array(array($processorClass, $processorObject)));
68
69 $fileProcessorRegistry->registerFileProcessorClass($processorClass);
70 $this->assertContains($processorObject, $fileProcessorRegistry->getFileProcessors(), '', FALSE, FALSE);
71 }
72
73 /**
74 * @test
75 */
76 public function registeredLocalProcessorClassCanBeRetrieved() {
77 $processorClass = uniqid('myProcessor');
78 $driverType = 'Local';
79 $processorObject = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass);
80 $processorObject->expects($this->any())->method('getDriverRestrictions')->will($this->returnValue(array($driverType)));
81
82 $fileProcessorRegistry = $this->getTestFileProcessorRegistry(array(array($processorClass, $processorObject)));
83
84 $fileProcessorRegistry->registerFileProcessorClass($processorClass);
85 $this->assertContains($processorObject, $fileProcessorRegistry->getFileProcessorsWithDriverSupport($driverType), '', FALSE, FALSE);
86 }
87
88 /**
89 * @test
90 */
91 public function registeredLocalProcessorClassDoesNotGetRetrievedWhenAskingForOtherDriverType() {
92 $processorClass = uniqid('myRemoteProcessor');
93 $processorObject = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass);
94 $processorObject->expects($this->any())->method('getDriverRestrictions')->will($this->returnValue(array('Remote')));
95
96 $fileProcessorRegistry = $this->getTestFileProcessorRegistry(array(array($processorClass, $processorObject)));
97
98 $fileProcessorRegistry->registerFileProcessorClass($processorClass);
99 $this->assertNotContains($processorObject, $fileProcessorRegistry->getFileProcessorsWithDriverSupport('Local'), '', FALSE, FALSE);
100 }
101
102 /**
103 * @test
104 * @expectedException \InvalidArgumentException
105 * @expectedExceptionCode 1393614709
106 */
107 public function registerFileProcessorClassThrowsExceptionIfClassDoesNotExist() {
108 $fileProcessorRegistry = $this->getTestFileProcessorRegistry();
109 $fileProcessorRegistry->registerFileProcessorClass(uniqid());
110 }
111
112 /**
113 * @test
114 * @expectedException \InvalidArgumentException
115 * @expectedExceptionCode 1393614710
116 */
117 public function registerFileProcessorClassThrowsExceptionIfClassDoesNotImplementRightInterface() {
118 $className = __CLASS__;
119 $fileProcessorRegistry = $this->getTestFileProcessorRegistry();
120 $fileProcessorRegistry->registerFileProcessorClass($className);
121 }
122
123 /**
124 * @test
125 */
126 public function processorRegistryIsInitializedWithPreconfiguredFileProcessors() {
127 $processorClass = uniqid('myRemoteProcessor');
128 $processorObject = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass);
129
130 // set TYPO3_CONF_VARS
131 $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['fileProcessors'] = array(
132 $processorClass
133 );
134
135 $fileProcessorRegistry = $this->getTestFileProcessorRegistry(array(array($processorClass, $processorObject)));
136 $this->assertContains($processorObject, $fileProcessorRegistry->getFileProcessors(), '', FALSE, FALSE);
137 }
138
139 /**
140 * @test
141 */
142 public function registerFileProcessorClassWithHighestPriorityIsFirstInResult() {
143 $processorClass1 = uniqid('myProcessor1');
144 $processorObject1 = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass1);
145 $processorObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
146
147 $processorClass2 = uniqid('myProcessor2');
148 $processorObject2 = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass2);
149 $processorObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
150
151 $processorClass3 = uniqid('myProcessor3');
152 $processorObject3 = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass3);
153 $processorObject3->expects($this->any())->method('getPriority')->will($this->returnValue(2));
154
155 $createdFileProcessorInstances = array(
156 array($processorClass1, $processorObject1),
157 array($processorClass2, $processorObject2),
158 array($processorClass3, $processorObject3),
159 );
160
161 $fileProcessorRegistry = $this->getTestFileProcessorRegistry($createdFileProcessorInstances);
162 $fileProcessorRegistry->registerFileProcessorClass($processorClass1);
163 $fileProcessorRegistry->registerFileProcessorClass($processorClass2);
164 $fileProcessorRegistry->registerFileProcessorClass($processorClass3);
165
166 $processors = $fileProcessorRegistry->getFileProcessors();
167 $this->assertTrue($processors[0] instanceof $processorClass2);
168 $this->assertTrue($processors[1] instanceof $processorClass3);
169 $this->assertTrue($processors[2] instanceof $processorClass1);
170 }
171
172 /**
173 * @test
174 */
175 public function registeredFileProcessorClassWithSamePriorityAreReturnedInSameOrderAsTheyWereAdded() {
176 $processorClass1 = uniqid('myProcessor1');
177 $processorObject1 = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass1);
178 $processorObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
179
180 $processorClass2 = uniqid('myProcessor2');
181 $processorObject2 = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Processing\\FileProcessorInterface', array(), array(), $processorClass2);
182 $processorObject2->expects($this->any())->method('getPriority')->will($this->returnValue(1));
183
184 $createdFileProcessorInstances = array(
185 array($processorClass1, $processorObject1),
186 array($processorClass2, $processorObject2),
187 );
188
189 $fileProcessorRegistry = $this->getTestFileProcessorRegistry($createdFileProcessorInstances);
190 $fileProcessorRegistry->registerFileProcessorClass($processorClass1);
191 $fileProcessorRegistry->registerFileProcessorClass($processorClass2);
192
193 $processors = $fileProcessorRegistry->getFileProcessors();
194 $this->assertTrue($processors[0] instanceof $processorClass1);
195 $this->assertTrue($processors[1] instanceof $processorClass2);
196 }
197 }
198