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