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