‪TYPO3CMS  10.4
MethodArgumentDroppedMatcherTest.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 ‪MethodArgumentDroppedMatcherTest extends UnitTestCase
30 {
34  public function ‪hitsFromFixtureAreFound()
35  {
36  ‪$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
37  $fixtureFile = __DIR__ . '/Fixtures/MethodArgumentDroppedMatcherFixture.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\Core\Charset\CharsetConverter->euc_char_mapping' => [
45  'maximumNumberOfArguments' => 2,
46  'restFiles' => [
47  'Breaking-80700-DeprecatedFunctionalityRemoved.rst',
48  ],
49  ],
50  ];
51  $subject = new ‪MethodArgumentDroppedMatcher($configuration);
52  $traverser->addVisitor($subject);
53  $traverser->traverse($statements);
54  $expectedHitLineNumbers = [
55  28,
56  ];
57  $actualHitLineNumbers = [];
58  foreach ($subject->getMatches() as $hit) {
59  $actualHitLineNumbers[] = $hit['line'];
60  }
61  self::assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
62  }
63 
68  {
69  return [
70  'two rest candidates with same number of arguments' => [
71  [
72  'Foo->aMethod' => [
73  'maximumNumberOfArguments' => 0,
74  'restFiles' => [
75  'Foo-1.rst',
76  'Foo-2.rst',
77  ],
78  ],
79  'Bar->aMethod' => [
80  'maximumNumberOfArguments' => 0,
81  'restFiles' => [
82  'Bar-1.rst',
83  'Bar-2.rst',
84  ],
85  ],
86  ],
87  '<?php
88  $someVar->aMethod(\'foo\');',
89  [
90  0 => [
91  'restFiles' => [
92  'Foo-1.rst',
93  'Foo-2.rst',
94  'Bar-1.rst',
95  'Bar-2.rst',
96  ],
97  ],
98  ],
99  ],
100  'two candidates, only one hits because second candidate needs one argument' => [
101  [
102  'Foo->aMethod' => [
103  'maximumNumberOfArguments' => 2,
104  'restFiles' => [
105  'Foo-1.rst',
106  ],
107  ],
108  'Bar->aMethod' => [
109  'maximumNumberOfArguments' => 3,
110  'restFiles' => [
111  'Bar-1.rst',
112  ],
113  ],
114  ],
115  '<?php
116  $someVar->aMethod(\'arg1\', \'arg2\', \'arg3\');',
117  [
118  0 => [
119  'restFiles' => [
120  'Foo-1.rst',
121  ],
122  ],
123  ],
124  ],
125  'three candidates, first and second hits' => [
126  [
127  'Foo->aMethod' => [
128  'maximumNumberOfArguments' => 0,
129  'restFiles' => [
130  'Foo-1.rst',
131  ],
132  ],
133  'Bar->aMethod' => [
134  'maximumNumberOfArguments' => 1,
135  'restFiles' => [
136  'Bar-1.rst',
137  ],
138  ],
139  'FooBar->aMethod' => [
140  'maximumNumberOfArguments' => 2,
141  'restFiles' => [
142  'FooBar-1.rst',
143  ],
144  ],
145  ],
146  '<?php
147  $someVar->aMethod(\'arg1\', \'arg2\');',
148  [
149  0 => [
150  'restFiles' => [
151  'Foo-1.rst',
152  'Bar-1.rst',
153  ],
154  ],
155  ],
156  ],
157  'one candidate, does not hit, not enough arguments given' => [
158  [
159  'Foo->aMethod' => [
160  'maximumNumberOfArguments' => 1,
161  'restFiles' => [
162  'Foo-1.rst',
163  ],
164  ],
165  ],
166  '<?php
167  $someVar->aMethod();',
168  [], // no hit
169  ],
170  'no match, method call using argument unpacking' => [
171  [
172  'Foo->aMethod' => [
173  'maximumNumberOfArguments' => 2,
174  'restFiles' => [
175  'Foo-1.rst',
176  ],
177  ],
178  ],
179  '<?php
180  $args = [\'arg1\', \'arg2\', \'arg3\'];
181  $someVar->aMethod(...$args);',
182  [],
183  ],
184  'method call using argument unpacking with more than max number of args given arguments' => [
185  [
186  'Foo->aMethod' => [
187  'maximumNumberOfArguments' => 2,
188  'restFiles' => [
189  'Foo-1.rst',
190  ],
191  ],
192  ],
193  '<?php
194  $args1 = [\'arg1\', \'arg2\', \'arg3\'];
195  $args2 = [\'arg4\', \'arg5\', \'arg6\'];
196  $args3 = [\'arg7\', \'arg8\', \'arg9\'];
197  $someVar->aMethod(...$args1, ...$args2, ...$args3);',
198  [],
199  ],
200  'double linked .rst file is returned only once' => [
201  [
202  'Foo->aMethod' => [
203  'maximumNumberOfArguments' => 0,
204  'restFiles' => [
205  'aRest.rst',
206  ],
207  ],
208  'Bar->aMethod' => [
209  'maximumNumberOfArguments' => 0,
210  'restFiles' => [
211  'aRest.rst',
212  ],
213  ],
214  ],
215  '<?php
216  $someVar->aMethod(\'foo\');',
217  [
218  0 => [
219  'restFiles' => [
220  'aRest.rst',
221  ],
222  ],
223  ],
224  ],
225  ];
226  }
227 
235  public function ‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
236  {
237  ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
238  $statements = ‪$parser->parse($phpCode);
239 
240  $subject = new ‪MethodArgumentDroppedMatcher($configuration);
241 
242  $traverser = new NodeTraverser();
243  $traverser->addVisitor($subject);
244  $traverser->traverse($statements);
245 
246  $result = $subject->getMatches();
247  if (isset($expected[0], $result[0])) {
248  self::assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
249  } else {
250  self::assertEquals($expected, $result);
251  }
252  }
253 }
‪$parser
‪$parser
Definition: annotationChecker.php:108
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcherTest.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentDroppedMatcher
Definition: MethodArgumentDroppedMatcher.php:31
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentDroppedMatcherTest\matchesReturnsExpectedRestFilesDataProvider
‪array matchesReturnsExpectedRestFilesDataProvider()
Definition: MethodArgumentDroppedMatcherTest.php:67
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentDroppedMatcherTest\matchesReturnsExpectedRestFiles
‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
Definition: MethodArgumentDroppedMatcherTest.php:235
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentDroppedMatcherTest
Definition: MethodArgumentDroppedMatcherTest.php:30
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentDroppedMatcherTest\hitsFromFixtureAreFound
‪hitsFromFixtureAreFound()
Definition: MethodArgumentDroppedMatcherTest.php:34