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