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