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