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