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