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