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