‪TYPO3CMS  10.4
MethodCallStaticMatcherTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use PhpParser\NodeTraverser;
21 use PhpParser\NodeVisitor\NameResolver;
22 use PhpParser\ParserFactory;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪MethodCallStaticMatcherTest extends UnitTestCase
31 {
35  public function ‪hitsFromFixtureAreFound()
36  {
37  ‪$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
38  $fixtureFile = __DIR__ . '/Fixtures/MethodCallStaticMatcherFixture.php';
39  $statements = ‪$parser->parse(file_get_contents($fixtureFile));
40 
41  $traverser = new NodeTraverser();
42  $traverser->addVisitor(new NameResolver());
43  $traverser->addVisitor(new ‪GeneratorClassesResolver());
44 
45  $configuration = [
46  'TYPO3\CMS\Backend\Utility\BackendUtility::getAjaxUrl' => [
47  'numberOfMandatoryArguments' => 1,
48  'maximumNumberOfArguments' => 2,
49  'restFiles' => [
50  'Breaking-80700-DeprecatedFunctionalityRemoved.rst',
51  'Deprecation-75340-MethodsRelatedToGeneratingTraditionalBackendAJAXURLs.rst',
52  ],
53  ],
54  ];
55  $subject = new ‪MethodCallStaticMatcher($configuration);
56  $traverser->addVisitor($subject);
57  $traverser->traverse($statements);
58  $expectedHitLineNumbers = [
59  30,
60  32,
61  ];
62  $actualHitLineNumbers = [];
63  foreach ($subject->getMatches() as $hit) {
64  $actualHitLineNumbers[] = $hit['line'];
65  }
66  self::assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
67  }
68 
73  {
74  return [
75  'two rest candidates with same number of arguments' => [
76  [
77  'Foo::aMethod' => [
78  'numberOfMandatoryArguments' => 0,
79  'maximumNumberOfArguments' => 2,
80  'restFiles' => [
81  'Foo-1.rst',
82  'Foo-2.rst',
83  ],
84  ],
85  'Bar::aMethod' => [
86  'numberOfMandatoryArguments' => 0,
87  'maximumNumberOfArguments' => 2,
88  'restFiles' => [
89  'Bar-1.rst',
90  'Bar-2.rst',
91  ],
92  ],
93  ],
94  '<?php
95  $someVar::aMethod();',
96  [
97  0 => [
98  'restFiles' => [
99  'Foo-1.rst',
100  'Foo-2.rst',
101  'Bar-1.rst',
102  'Bar-2.rst',
103  ],
104  ],
105  ],
106  ],
107  'two candidates, only one hits because second candidate needs one argument' => [
108  [
109  'Foo::aMethod' => [
110  'numberOfMandatoryArguments' => 0,
111  'maximumNumberOfArguments' => 2,
112  'restFiles' => [
113  'Foo-1.rst',
114  ],
115  ],
116  'Bar::aMethod' => [
117  'numberOfMandatoryArguments' => 1,
118  'maximumNumberOfArguments' => 2,
119  'restFiles' => [
120  'Bar-1.rst',
121  ],
122  ],
123  ],
124  '<?php
125  $someVar::aMethod();',
126  [
127  0 => [
128  'restFiles' => [
129  'Foo-1.rst',
130  ],
131  ],
132  ],
133  ],
134  'three candidates, first and second hits' => [
135  [
136  'Foo::aMethod' => [
137  'numberOfMandatoryArguments' => 2,
138  'maximumNumberOfArguments' => 4,
139  'restFiles' => [
140  'Foo-1.rst',
141  ],
142  ],
143  'Bar::aMethod' => [
144  'numberOfMandatoryArguments' => 1,
145  'maximumNumberOfArguments' => 4,
146  'restFiles' => [
147  'Bar-1.rst',
148  ],
149  ],
150  'FooBar::aMethod' => [
151  'numberOfMandatoryArguments' => 3,
152  'maximumNumberOfArguments' => 4,
153  'restFiles' => [
154  'FooBar-1.rst',
155  ],
156  ],
157  ],
158  '<?php
159  $someVar::aMethod(\'arg1\', \'arg2\');',
160  [
161  0 => [
162  'restFiles' => [
163  'Foo-1.rst',
164  'Bar-1.rst',
165  ],
166  ],
167  ],
168  ],
169  'one candidate, does not hit, not enough arguments given' => [
170  [
171  'Foo::aMethod' => [
172  'numberOfMandatoryArguments' => 1,
173  'maximumNumberOfArguments' => 2,
174  'restFiles' => [
175  'Foo-1.rst',
176  ],
177  ],
178  ],
179  '<?php
180  $someVar::aMethod();',
181  [], // no hit
182  ],
183  'too many arguments given' => [
184  [
185  'Foo::aMethod' => [
186  'numberOfMandatoryArguments' => 1,
187  'maximumNumberOfArguments' => 1,
188  'restFiles' => [
189  'Foo-1.rst',
190  ],
191  ],
192  ],
193  '<?php
194  $someVar::aMethod($foo, $bar);',
195  [], // no hit
196  ],
197  'method call using argument unpacking' => [
198  [
199  'Foo::aMethod' => [
200  'numberOfMandatoryArguments' => 2,
201  'maximumNumberOfArguments' => 2,
202  'restFiles' => [
203  'Foo-1.rst',
204  ],
205  ],
206  ],
207  '<?php
208  $args = [\'arg1\', \'arg2\', \'arg3\'];
209  $someVar::aMethod(...$args);',
210  [
211  0 => [
212  'restFiles' => [
213  'Foo-1.rst',
214  ],
215  ],
216  ],
217  ],
218  'method call using argument unpacking with more than max number of args given arguments' => [
219  [
220  'Foo::aMethod' => [
221  'numberOfMandatoryArguments' => 2,
222  'maximumNumberOfArguments' => 2,
223  'restFiles' => [
224  'Foo-1.rst',
225  ],
226  ],
227  ],
228  '<?php
229  $args1 = [\'arg1\', \'arg2\', \'arg3\'];
230  $args2 = [\'arg4\', \'arg5\', \'arg6\'];
231  $args3 = [\'arg7\', \'arg8\', \'arg9\'];
232  $someVar::aMethod(...$args1, ...$args2, ...$args3);',
233  [
234  0 => [
235  'restFiles' => [
236  'Foo-1.rst',
237  ],
238  ],
239  ],
240  ],
241  // something wrong here
242  'double linked .rst file is returned only once' => [
243  [
244  'Foo::aMethod' => [
245  'numberOfMandatoryArguments' => 0,
246  'maximumNumberOfArguments' => 2,
247  'restFiles' => [
248  'aRest.rst',
249  ],
250  ],
251  'Bar::aMethod' => [
252  'numberOfMandatoryArguments' => 0,
253  'maximumNumberOfArguments' => 2,
254  'restFiles' => [
255  'aRest.rst',
256  ],
257  ],
258  ],
259  '<?php
260  $someVar::aMethod(\'foo\');',
261  [
262  0 => [
263  'restFiles' => [
264  'aRest.rst',
265  ],
266  ],
267  ],
268  ],
269  ];
270  }
271 
276  public function ‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
277  {
278  ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
279  $statements = ‪$parser->parse($phpCode);
280 
281  $subject = new ‪MethodCallStaticMatcher($configuration);
282 
283  $traverser = new NodeTraverser();
284  $traverser->addVisitor($subject);
285  $traverser->traverse($statements);
286 
287  $result = $subject->getMatches();
288  if (isset($expected[0], $result[0])) {
289  self::assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
290  } else {
291  self::assertEquals($expected, $result);
292  }
293  }
294 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver
Definition: GeneratorClassesResolver.php:41
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodCallStaticMatcher
Definition: MethodCallStaticMatcher.php:38
‪$parser
‪$parser
Definition: annotationChecker.php:108
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcherTest.php:18
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodCallStaticMatcherTest
Definition: MethodCallStaticMatcherTest.php:31
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodCallStaticMatcherTest\matchesReturnsExpectedRestFiles
‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
Definition: MethodCallStaticMatcherTest.php:276
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodCallStaticMatcherTest\hitsFromFixtureAreFound
‪hitsFromFixtureAreFound()
Definition: MethodCallStaticMatcherTest.php:35
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodCallStaticMatcherTest\matchesReturnsExpectedRestFilesDataProvider
‪array matchesReturnsExpectedRestFilesDataProvider()
Definition: MethodCallStaticMatcherTest.php:72