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