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