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