[!!!][TASK] Remove support for non namespaced classes in Extbase
[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 'VENDOR\\EXT\\Domain\\Repository\\BlogRepository',
39 'VENDOR\\EXT\\Domain\\Model\\Blog',
40 'VENDOR\\EXT\\Domain\\Validator\\BlogValidator'
41 ],
42 [
43 'VENDOR\\EXT\\Domain\\Repository\\_PageRepository',
44 'VENDOR\\EXT\\Domain\\Model\\_Page',
45 'VENDOR\\EXT\\Domain\\Validator\\_PageValidator'
46 ],
47 [
48 'VENDOR\\Repository\\Domain\\Repository\\SomeModelRepository',
49 'VENDOR\\Repository\\Domain\\Model\\SomeModel',
50 'VENDOR\\Repository\\Domain\\Validator\\SomeModelValidator'
51 ],
52 [
53 'VENDOR\\EXT\\Domain\\Repository\\RepositoryRepository',
54 'VENDOR\\EXT\\Domain\\Model\\Repository',
55 'VENDOR\\EXT\\Domain\\Validator\\RepositoryValidator'
56 ],
57 [
58 'VENDOR\\Repository\\Domain\\Repository\\RepositoryRepository',
59 'VENDOR\\Repository\\Domain\\Model\\Repository',
60 'VENDOR\\Repository\\Domain\\Validator\\RepositoryValidator'
61 ],
62 [
63 'VENDOR\\ModelCollection\\Domain\\Repository\\ModelRepository',
64 'VENDOR\\ModelCollection\\Domain\\Model\\Model',
65 'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator'
66 ],
67 [
68 'VENDOR\\Model\\Domain\\Repository\\ModelRepository',
69 'VENDOR\\Model\\Domain\\Model\\Model',
70 'VENDOR\\Model\\Domain\\Validator\\ModelValidator'
71 ],
72 ];
73 }
74
75 /**
76 * @dataProvider repositoryAndModelClassNames
77 * @param string $expectedRepositoryName
78 * @param string $modelName
79 * @param string $dummyValidatorName not needed here - just a dummy to be able to cleanly use the same dataprovider
80 * @test
81 */
82 public function translateModelNameToRepositoryName($expectedRepositoryName, $modelName, $dummyValidatorName)
83 {
84 $translatedRepositoryName = ClassNamingUtility::translateModelNameToRepositoryName($modelName);
85 $this->assertSame($expectedRepositoryName, $translatedRepositoryName);
86 }
87
88 /**
89 * @dataProvider repositoryAndModelClassNames
90 * @param string $repositoryName
91 * @param string $expectedModelName
92 * @param string $dummyValidatorName not needed here - just a dummy to be able to use the same dataprovider
93 * @test
94 */
95 public function translateRepositoryNameToModelName($repositoryName, $expectedModelName, $dummyValidatorName)
96 {
97 $translatedModelName = ClassNamingUtility::translateRepositoryNameToModelName($repositoryName);
98 $this->assertSame($expectedModelName, $translatedModelName);
99 }
100
101 /**
102 * @dataProvider repositoryAndModelClassNames
103 * @param string $repositoryName
104 * @param string $modelName
105 * @param string $expectedValidatorName
106 * @test
107 */
108 public function translateModelNameToValidatorName($repositoryName, $modelName, $expectedValidatorName)
109 {
110 $translatedModelName = ClassNamingUtility::translateModelNameToValidatorName($modelName);
111 $this->assertSame($expectedValidatorName, $translatedModelName);
112 }
113
114 /**
115 * DataProvider for explodeObjectControllerName
116 *
117 * @return array
118 */
119 public function controllerObjectNamesAndMatches(): array
120 {
121 return [
122 [
123 'TYPO3\\CMS\\Ext\\Controller\\FooController',
124 [
125 'vendorName' => 'TYPO3\\CMS',
126 'extensionName' => 'Ext',
127 'subpackageKey' => '',
128 'controllerName' => 'Foo',
129 ]
130 ],
131 [
132 'TYPO3\\CMS\\Ext\\Command\\FooCommandController',
133 [
134 'vendorName' => 'TYPO3\\CMS',
135 'extensionName' => 'Ext',
136 'subpackageKey' => '',
137 'controllerName' => 'FooCommand',
138 ]
139 ],
140 [
141 PaginateController::class,
142 [
143 'vendorName' => 'TYPO3\\CMS',
144 'extensionName' => 'Fluid',
145 'subpackageKey' => 'ViewHelpers\\Widget',
146 'controllerName' => 'Paginate',
147 ]
148 ],
149 [
150 'VENDOR\\Ext\\Controller\\FooController',
151 [
152 'vendorName' => 'VENDOR',
153 'extensionName' => 'Ext',
154 'subpackageKey' => '',
155 'controllerName' => 'Foo',
156 ]
157 ],
158 [
159 'VENDOR\\Ext\\Command\\FooCommandController',
160 [
161 'vendorName' => 'VENDOR',
162 'extensionName' => 'Ext',
163 'subpackageKey' => '',
164 'controllerName' => 'FooCommand',
165 ]
166 ],
167 [
168 'VENDOR\\Ext\\ViewHelpers\\Widget\\Controller\\FooController',
169 [
170 'vendorName' => 'VENDOR',
171 'extensionName' => 'Ext',
172 'subpackageKey' => 'ViewHelpers\\Widget',
173 'controllerName' => 'Foo',
174 ]
175 ],
176 ];
177 }
178
179 /**
180 * @dataProvider controllerObjectNamesAndMatches
181 *
182 * @param string $controllerObjectName
183 * @param array $expectedMatches
184 * @test
185 */
186 public function explodeObjectControllerName($controllerObjectName, $expectedMatches)
187 {
188 $actualMatches = ClassNamingUtility::explodeObjectControllerName($controllerObjectName);
189 $this->assertSame($expectedMatches, $actualMatches);
190 }
191 }