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