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