‪TYPO3CMS  ‪main
MethodArgumentUnusedMatcherTest.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 ‪MethodArgumentUnusedMatcherTest extends UnitTestCase
30 {
31  #[Test]
32  public function ‪hitsFromFixtureAreFound(): void
33  {
34  ‪$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2));
35  $fixtureFile = __DIR__ . '/Fixtures/MethodArgumentUnusedMatcherFixture.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\Html\RteHtmlParser->RTE_transform' => [
43  'unusedArgumentNumbers' => [ 2 ],
44  'restFiles' => [
45  'Breaking-80700-DeprecatedFunctionalityRemoved.rst',
46  'Deprecation-79341-MethodsRelatedToRichtextConfiguration.rst',
47  ],
48  ],
49  ];
50  $subject = new ‪MethodArgumentUnusedMatcher($configuration);
51  $traverser->addVisitor($subject);
52  $traverser->traverse($statements);
53  $expectedHitLineNumbers = [
54  28,
55  29,
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  'unusedArgumentNumbers' => [ 1 ],
71  'restFiles' => [
72  'Foo-1.rst',
73  'Foo-2.rst',
74  ],
75  ],
76  'Bar->aMethod' => [
77  'unusedArgumentNumbers' => [ 1 ],
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, multiple matches' => [
98  [
99  'Foo->aMethod' => [
100  'unusedArgumentNumbers' => [ 1, 2 ],
101  'restFiles' => [
102  'Foo-1.rst',
103  ],
104  ],
105  'Bar->aMethod' => [
106  'unusedArgumentNumbers' => [ 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  'Bar-1.rst',
119  ],
120  ],
121  ],
122  ],
123  'one candidate, no hit, not enough arguments' => [
124  [
125  'Foo->aMethod' => [
126  'unusedArgumentNumbers' => [ 2, 3 ],
127  'restFiles' => [
128  'Foo-1.rst',
129  ],
130  ],
131  ],
132  '<?php
133  $someVar->aMethod(\'arg1\');',
134  [], // no hit
135  ],
136  'one candidate, no hit, given as null is ok' => [
137  [
138  'Foo->aMethod' => [
139  'unusedArgumentNumbers' => [ 2, 3 ],
140  'restFiles' => [
141  'Foo-1.rst',
142  ],
143  ],
144  ],
145  '<?php
146  $someVar->aMethod(\'arg1\', null, null);',
147  [], // no hit
148  ],
149  'one match, third argument still given not null' => [
150  [
151  'Foo->aMethod' => [
152  'unusedArgumentNumbers' => [ 2, 3 ],
153  'restFiles' => [
154  'aRest.rst',
155  ],
156  ],
157  ],
158  '<?php
159  $someVar->aMethod(\'arg1\', null, \'arg3\');',
160  [
161  0 => [
162  'restFiles' => [
163  'aRest.rst',
164  ],
165  ],
166  ],
167  ],
168  'no match, scanning ignored as soon as argument unpacking is used' => [
169  [
170  'Foo->aMethod' => [
171  'unusedArgumentNumbers' => [ 1, 3 ],
172  'restFiles' => [
173  'Foo-1.rst',
174  ],
175  ],
176  ],
177  '<?php
178  $args1 = [\'arg1\', \'arg2\', \'arg3\'];
179  $args2 = [\'arg4\', \'arg5\', \'arg6\'];
180  $args3 = [\'arg7\', \'arg8\', \'arg9\'];
181  $someVar->aMethod(...$args1, ...$args2, ...$args3);',
182  [],
183  ],
184  'double linked .rst file is returned only once' => [
185  [
186  'Foo->aMethod' => [
187  'unusedArgumentNumbers' => [ 1 ],
188  'restFiles' => [
189  'aRest.rst',
190  ],
191  ],
192  'Bar->aMethod' => [
193  'unusedArgumentNumbers' => [ 1 ],
194  'restFiles' => [
195  'aRest.rst',
196  ],
197  ],
198  ],
199  '<?php
200  $someVar->aMethod(\'foo\');',
201  [
202  0 => [
203  'restFiles' => [
204  'aRest.rst',
205  ],
206  ],
207  ],
208  ],
209  ];
210  }
211 
212  #[DataProvider('matchesReturnsExpectedRestFilesDataProvider')]
213  #[Test]
214  public function ‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected): void
215  {
216  ‪$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2));
217  $statements = ‪$parser->parse($phpCode);
218 
219  $subject = new ‪MethodArgumentUnusedMatcher($configuration);
220 
221  $traverser = new NodeTraverser();
222  $traverser->addVisitor($subject);
223  $traverser->traverse($statements);
224 
225  $result = $subject->getMatches();
226  if (isset($expected[0], $result[0])) {
227  self::assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
228  } else {
229  self::assertEquals($expected, $result);
230  }
231  }
232 }
‪$parser
‪$parser
Definition: annotationChecker.php:103
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcherTest
Definition: MethodArgumentUnusedMatcherTest.php:30
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcherTest\hitsFromFixtureAreFound
‪hitsFromFixtureAreFound()
Definition: MethodArgumentUnusedMatcherTest.php:32
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcherTest.php:18
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcherTest\matchesReturnsExpectedRestFilesDataProvider
‪static matchesReturnsExpectedRestFilesDataProvider()
Definition: MethodArgumentUnusedMatcherTest.php:64
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcher
Definition: MethodArgumentUnusedMatcher.php:33
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcherTest\matchesReturnsExpectedRestFiles
‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
Definition: MethodArgumentUnusedMatcherTest.php:214