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