[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / ClassNamingUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
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
17 /**
18 * Testcase for class \TYPO3\CMS\Core\Utility\ClassNamingUtility
19 */
20 class ClassNamingUtilityTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
21 {
22 /**
23 * DataProvider for translateModelNameToRepositoryName
24 * and translateRepositoryNameToModelName
25 * and translateModelNameToValidatorName
26 *
27 * @return array
28 */
29 public function repositoryAndModelClassNames()
30 {
31 return [
32 [
33 'Tx_BlogExample_Domain_Repository_BlogRepository',
34 'Tx_BlogExample_Domain_Model_Blog',
35 'Tx_BlogExample_Domain_Validator_BlogValidator'
36 ],
37 [
38 ' _Domain_Repository_Content_PageRepository',
39 ' _Domain_Model_Content_Page',
40 ' _Domain_Validator_Content_PageValidator'
41 ],
42 [
43 'Tx_RepositoryExample_Domain_Repository_SomeModelRepository',
44 'Tx_RepositoryExample_Domain_Model_SomeModel',
45 'Tx_RepositoryExample_Domain_Validator_SomeModelValidator'
46 ],
47 [
48 'Tx_RepositoryExample_Domain_Repository_RepositoryRepository',
49 'Tx_RepositoryExample_Domain_Model_Repository',
50 'Tx_RepositoryExample_Domain_Validator_RepositoryValidator'
51 ],
52 [
53 'Tx_Repository_Domain_Repository_RepositoryRepository',
54 'Tx_Repository_Domain_Model_Repository',
55 'Tx_Repository_Domain_Validator_RepositoryValidator'
56 ],
57 [
58 'Tx_ModelCollection_Domain_Repository_ModelRepository',
59 'Tx_ModelCollection_Domain_Model_Model',
60 'Tx_ModelCollection_Domain_Validator_ModelValidator'
61 ],
62 [
63 'Tx_Model_Domain_Repository_ModelRepository',
64 'Tx_Model_Domain_Model_Model',
65 'Tx_Model_Domain_Validator_ModelValidator'
66 ],
67 [
68 'VENDOR\\EXT\\Domain\\Repository\\BlogRepository',
69 'VENDOR\\EXT\\Domain\\Model\\Blog',
70 'VENDOR\\EXT\\Domain\\Validator\\BlogValidator'
71 ],
72 [
73 'VENDOR\\EXT\\Domain\\Repository\\_PageRepository',
74 'VENDOR\\EXT\\Domain\\Model\\_Page',
75 'VENDOR\\EXT\\Domain\\Validator\\_PageValidator'
76 ],
77 [
78 'VENDOR\\Repository\\Domain\\Repository\\SomeModelRepository',
79 'VENDOR\\Repository\\Domain\\Model\\SomeModel',
80 'VENDOR\\Repository\\Domain\\Validator\\SomeModelValidator'
81 ],
82 [
83 'VENDOR\\EXT\\Domain\\Repository\\RepositoryRepository',
84 'VENDOR\\EXT\\Domain\\Model\\Repository',
85 'VENDOR\\EXT\\Domain\\Validator\\RepositoryValidator'
86 ],
87 [
88 'VENDOR\\Repository\\Domain\\Repository\\RepositoryRepository',
89 'VENDOR\\Repository\\Domain\\Model\\Repository',
90 'VENDOR\\Repository\\Domain\\Validator\\RepositoryValidator'
91 ],
92 [
93 'VENDOR\\ModelCollection\\Domain\\Repository\\ModelRepository',
94 'VENDOR\\ModelCollection\\Domain\\Model\\Model',
95 'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator'
96 ],
97 [
98 'VENDOR\\Model\\Domain\\Repository\\ModelRepository',
99 'VENDOR\\Model\\Domain\\Model\\Model',
100 'VENDOR\\Model\\Domain\\Validator\\ModelValidator'
101 ],
102 ];
103 }
104
105 /**
106 * @dataProvider repositoryAndModelClassNames
107 * @param string $expectedRepositoryName
108 * @param string $modelName
109 * @param string $dummyValidatorName not needed here - just a dummy to be able to cleanly use the same dataprovider
110 * @test
111 */
112 public function translateModelNameToRepositoryName($expectedRepositoryName, $modelName, $dummyValidatorName)
113 {
114 $translatedRepositoryName = \TYPO3\CMS\Core\Utility\ClassNamingUtility::translateModelNameToRepositoryName($modelName);
115 $this->assertSame($expectedRepositoryName, $translatedRepositoryName);
116 }
117
118 /**
119 * @dataProvider repositoryAndModelClassNames
120 * @param string $repositoryName
121 * @param string $expectedModelName
122 * @param string $dummyValidatorName not needed here - just a dummy to be able to use the same dataprovider
123 * @test
124 */
125 public function translateRepositoryNameToModelName($repositoryName, $expectedModelName, $dummyValidatorName)
126 {
127 $translatedModelName = \TYPO3\CMS\Core\Utility\ClassNamingUtility::translateRepositoryNameToModelName($repositoryName);
128 $this->assertSame($expectedModelName, $translatedModelName);
129 }
130
131 /**
132 * @dataProvider repositoryAndModelClassNames
133 * @param string $repositoryName
134 * @param string $modelName
135 * @param string $expectedValidatorName
136 * @test
137 */
138 public function translateModelNameToValidatorName($repositoryName, $modelName, $expectedValidatorName)
139 {
140 $translatedModelName = \TYPO3\CMS\Core\Utility\ClassNamingUtility::translateModelNameToValidatorName($modelName);
141 $this->assertSame($expectedValidatorName, $translatedModelName);
142 }
143
144 /**
145 * DataProvider for explodeObjectControllerName
146 *
147 * @return array
148 */
149 public function controllerObjectNamesAndMatches()
150 {
151 return [
152 [
153 'TYPO3\\CMS\\Ext\\Controller\\FooController',
154 [
155 'vendorName' => 'TYPO3\\CMS',
156 'extensionName' => 'Ext',
157 'subpackageKey' => '',
158 'controllerName' => 'Foo',
159 ]
160 ],
161 [
162 'TYPO3\\CMS\\Ext\\Command\\FooCommandController',
163 [
164 'vendorName' => 'TYPO3\\CMS',
165 'extensionName' => 'Ext',
166 'subpackageKey' => '',
167 'controllerName' => 'FooCommand',
168 ]
169 ],
170 [
171 \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController::class,
172 [
173 'vendorName' => 'TYPO3\\CMS',
174 'extensionName' => 'Fluid',
175 'subpackageKey' => 'ViewHelpers\\Widget',
176 'controllerName' => 'Paginate',
177 ]
178 ],
179 [
180 'VENDOR\\Ext\\Controller\\FooController',
181 [
182 'vendorName' => 'VENDOR',
183 'extensionName' => 'Ext',
184 'subpackageKey' => '',
185 'controllerName' => 'Foo',
186 ]
187 ],
188 [
189 'VENDOR\\Ext\\Command\\FooCommandController',
190 [
191 'vendorName' => 'VENDOR',
192 'extensionName' => 'Ext',
193 'subpackageKey' => '',
194 'controllerName' => 'FooCommand',
195 ]
196 ],
197 [
198 'VENDOR\\Ext\\ViewHelpers\\Widget\\Controller\\FooController',
199 [
200 'vendorName' => 'VENDOR',
201 'extensionName' => 'Ext',
202 'subpackageKey' => 'ViewHelpers\\Widget',
203 'controllerName' => 'Foo',
204 ]
205 ],
206 // Oldschool
207 [
208 'Tx_Ext_Controller_FooController',
209 [
210 'vendorName' => null,
211 'extensionName' => 'Ext',
212 'subpackageKey' => '',
213 'controllerName' => 'Foo',
214 ]
215 ],
216 [
217 'Tx_Ext_Command_FooCommandController',
218 [
219 'vendorName' => null,
220 'extensionName' => 'Ext',
221 'subpackageKey' => '',
222 'controllerName' => 'FooCommand',
223 ]
224 ],
225 [
226 'Tx_Fluid_ViewHelpers_Widget_Controller_PaginateController',
227 [
228 'vendorName' => null,
229 'extensionName' => 'Fluid',
230 'subpackageKey' => 'ViewHelpers_Widget',
231 'controllerName' => 'Paginate',
232 ]
233 ],
234 ];
235 }
236
237 /**
238 * @dataProvider controllerObjectNamesAndMatches
239 *
240 * @param string $controllerObjectName
241 * @param array $expectedMatches
242 * @test
243 */
244 public function explodeObjectControllerName($controllerObjectName, $expectedMatches)
245 {
246 $matches = \TYPO3\CMS\Core\Utility\ClassNamingUtility::explodeObjectControllerName($controllerObjectName);
247
248 $actualMatches = [
249 'vendorName' => $matches['vendorName'],
250 'extensionName' => $matches['extensionName'],
251 'subpackageKey' => $matches['subpackageKey'],
252 'controllerName' => $matches['controllerName'],
253 ];
254
255 $this->assertSame($expectedMatches, $actualMatches);
256 }
257 }