[TASK] Make MethodArgumentDroppedStaticMatcherTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / ExtensionScanner / Php / Matcher / MethodArgumentDroppedStaticMatcherTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher;
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
18 use PhpParser\NodeTraverser;
19 use PhpParser\NodeVisitor\NameResolver;
20 use PhpParser\ParserFactory;
21 use TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentDroppedStaticMatcher;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Test case
26 */
27 class MethodArgumentDroppedStaticMatcherTest extends UnitTestCase
28 {
29 /**
30 * @test
31 */
32 public function hitsFromFixtureAreFound()
33 {
34 $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
35 $fixtureFile = __DIR__ . '/Fixtures/MethodArgumentDroppedStaticMatcherFixture.php';
36 $statements = $parser->parse(file_get_contents($fixtureFile));
37
38 $traverser = new NodeTraverser();
39 $traverser->addVisitor(new NameResolver());
40
41 $configuration = [
42 'TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName' => [
43 'maximumNumberOfArguments' => 1,
44 'restFiles' => [
45 'Breaking-80700-DeprecatedFunctionalityRemoved.rst',
46 'Deprecation-73516-VariousGeneralUtilityMethods.rst'
47 ],
48 ],
49 ];
50 $subject = new MethodArgumentDroppedStaticMatcher($configuration);
51 $traverser->addVisitor($subject);
52 $traverser->traverse($statements);
53 $expectedHitLineNumbers = [
54 28,
55 30,
56 32,
57 ];
58 $actualHitLineNumbers = [];
59 foreach ($subject->getMatches() as $hit) {
60 $actualHitLineNumbers[] = $hit['line'];
61 }
62 $this->assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
63 }
64
65 /**
66 * @return array
67 */
68 public function matchesReturnsExpectedRestFilesDataProvider(): array
69 {
70 return [
71 'two rest candidates with same number of arguments' => [
72 [
73 'Foo::aMethod' => [
74 'maximumNumberOfArguments' => 0,
75 'restFiles' => [
76 'Foo-1.rst',
77 'Foo-2.rst',
78 ],
79 ],
80 'Bar::aMethod' => [
81 'maximumNumberOfArguments' => 0,
82 'restFiles' => [
83 'Bar-1.rst',
84 'Bar-2.rst',
85 ],
86 ],
87 ],
88 '<?php
89 $someVar::aMethod(\'foo\');',
90 [
91 0 => [
92 'restFiles' => [
93 'Foo-1.rst',
94 'Foo-2.rst',
95 'Bar-1.rst',
96 'Bar-2.rst',
97 ],
98 ],
99 ],
100 ],
101 'two candidates, only one hits because second candidate needs one argument' => [
102 [
103 'Foo::aMethod' => [
104 'maximumNumberOfArguments' => 2,
105 'restFiles' => [
106 'Foo-1.rst',
107 ],
108 ],
109 'Bar::aMethod' => [
110 'maximumNumberOfArguments' => 3,
111 'restFiles' => [
112 'Bar-1.rst',
113 ],
114 ],
115 ],
116 '<?php
117 $someVar::aMethod(\'arg1\', \'arg2\', \'arg3\');',
118 [
119 0 => [
120 'restFiles' => [
121 'Foo-1.rst',
122 ],
123 ],
124 ],
125 ],
126 'three candidates, first and second hits' => [
127 [
128 'Foo::aMethod' => [
129 'maximumNumberOfArguments' => 0,
130 'restFiles' => [
131 'Foo-1.rst',
132 ],
133 ],
134 'Bar::aMethod' => [
135 'maximumNumberOfArguments' => 1,
136 'restFiles' => [
137 'Bar-1.rst',
138 ],
139 ],
140 'FooBar::aMethod' => [
141 'maximumNumberOfArguments' => 2,
142 'restFiles' => [
143 'FooBar-1.rst',
144 ],
145 ],
146 ],
147 '<?php
148 $someVar::aMethod(\'arg1\', \'arg2\');',
149 [
150 0 => [
151 'restFiles' => [
152 'Foo-1.rst',
153 'Bar-1.rst',
154 ],
155 ],
156 ],
157 ],
158 'one candidate, does not hit, not enough arguments given' => [
159 [
160 'Foo::aMethod' => [
161 'maximumNumberOfArguments' => 1,
162 'restFiles' => [
163 'Foo-1.rst',
164 ],
165 ],
166 ],
167 '<?php
168 $someVar::aMethod();',
169 [], // no hit
170 ],
171 'no match, method call using argument unpacking' => [
172 [
173 'Foo::aMethod' => [
174 'maximumNumberOfArguments' => 2,
175 'restFiles' => [
176 'Foo-1.rst',
177 ],
178 ],
179 ],
180 '<?php
181 $args = [\'arg1\', \'arg2\', \'arg3\'];
182 $someVar::aMethod(...$args);',
183 [],
184 ],
185 'method call using argument unpacking with more than max number of args given arguments' => [
186 [
187 'Foo::aMethod' => [
188 'maximumNumberOfArguments' => 2,
189 'restFiles' => [
190 'Foo-1.rst',
191 ],
192 ],
193 ],
194 '<?php
195 $args1 = [\'arg1\', \'arg2\', \'arg3\'];
196 $args2 = [\'arg4\', \'arg5\', \'arg6\'];
197 $args3 = [\'arg7\', \'arg8\', \'arg9\'];
198 $someVar::aMethod(...$args1, ...$args2, ...$args3);',
199 [],
200 ],
201 'double linked .rst file is returned only once' => [
202 [
203 'Foo::aMethod' => [
204 'maximumNumberOfArguments' => 0,
205 'restFiles' => [
206 'aRest.rst',
207 ],
208 ],
209 'Bar::aMethod' => [
210 'maximumNumberOfArguments' => 0,
211 'restFiles' => [
212 'aRest.rst',
213 ],
214 ],
215 ],
216 '<?php
217 $someVar::aMethod(\'foo\');',
218 [
219 0 => [
220 'restFiles' => [
221 'aRest.rst',
222 ],
223 ],
224 ],
225 ],
226 ];
227 }
228
229 /**
230 * @test
231 * @dataProvider matchesReturnsExpectedRestFilesDataProvider
232 * @param array $configuration
233 * @param string $phpCode
234 * @param array $expected
235 */
236 public function matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
237 {
238 $parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
239 $statements = $parser->parse($phpCode);
240
241 $subject = new MethodArgumentDroppedStaticMatcher($configuration);
242
243 $traverser = new NodeTraverser();
244 $traverser->addVisitor($subject);
245 $traverser->traverse($statements);
246
247 $result = $subject->getMatches();
248 if (isset($expected[0], $result[0])) {
249 $this->assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
250 } else {
251 $this->assertEquals($expected, $result);
252 }
253 }
254 }