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