‪TYPO3CMS  11.5
MethodArgumentRequiredStaticMatcherTest.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 MethodArgumentRequiredStaticMatcherTest extends UnitTestCase
30 {
34  public function hitsFromFixtureAreFound(): void
35  {
36  ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
37  $fixtureFile = __DIR__ . '/Fixtures/MethodArgumentRequiredStaticMatcherFixture.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\Core\Utility\ExtensionManagementUtility::addNavigationComponent' => [
45  'numberOfMandatoryArguments' => 3,
46  'maximumNumberOfArguments' => 3,
47  'restFiles' => [
48  'Breaking-82899-MoreRestrictingChecksForAPIMethodsInExtensionManagementUtility.rst',
49  ],
50  ],
51  ];
52  $subject = new MethodArgumentRequiredStaticMatcher($configuration);
53  $traverser->addVisitor($subject);
54  $traverser->traverse($statements);
55  $expectedHitLineNumbers = [
56  30,
57  32,
58  34,
59  ];
60  $actualHitLineNumbers = [];
61  foreach ($subject->getMatches() as $hit) {
62  $actualHitLineNumbers[] = $hit['line'];
63  }
64  self::assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
65  }
66 
70  public function matchesReturnsExpectedRestFilesDataProvider(): array
71  {
72  return [
73  'two rest candidates with same number of arguments' => [
74  [
75  'Foo::aMethod' => [
76  'numberOfMandatoryArguments' => 1,
77  'maximumNumberOfArguments' => 1,
78  'restFiles' => [
79  'Foo-1.rst',
80  'Foo-2.rst',
81  ],
82  ],
83  'Bar::aMethod' => [
84  'numberOfMandatoryArguments' => 1,
85  'maximumNumberOfArguments' => 1,
86  'restFiles' => [
87  'Bar-1.rst',
88  'Bar-2.rst',
89  ],
90  ],
91  ],
92  '<?php
93  $someVar::aMethod();',
94  [
95  0 => [
96  'restFiles' => [
97  'Foo-1.rst',
98  'Foo-2.rst',
99  'Bar-1.rst',
100  'Bar-2.rst',
101  ],
102  ],
103  ],
104  ],
105  'three candidates, first and second hits' => [
106  [
107  'Foo::aMethod' => [
108  'numberOfMandatoryArguments' => 3,
109  'maximumNumberOfArguments' => 3,
110  'restFiles' => [
111  'Foo-1.rst',
112  ],
113  ],
114  'Bar::aMethod' => [
115  'numberOfMandatoryArguments' => 3,
116  'maximumNumberOfArguments' => 3,
117  'restFiles' => [
118  'Bar-1.rst',
119  ],
120  ],
121  'FooBar::aMethod' => [
122  'numberOfMandatoryArguments' => 2,
123  'maximumNumberOfArguments' => 3,
124  'restFiles' => [
125  'FooBar-1.rst',
126  ],
127  ],
128  ],
129  '<?php
130  $someVar::aMethod(\'foo\', \'bar\');',
131  [
132  0 => [
133  'restFiles' => [
134  'Foo-1.rst',
135  'Bar-1.rst',
136  ],
137  ],
138  ],
139  ],
140  'one candidate, does not hit, enough arguments given' => [
141  [
142  'Foo::aMethod' => [
143  'numberOfMandatoryArguments' => 1,
144  'maximumNumberOfArguments' => 1,
145  'restFiles' => [
146  'Foo-1.rst',
147  ],
148  ],
149  ],
150  '<?php
151  $someVar::aMethod(\'foo\');',
152  [], // no hit
153  ],
154  'no match, method call using argument unpacking' => [
155  [
156  'Foo::aMethod' => [
157  'numberOfMandatoryArguments' => 1,
158  'maximumNumberOfArguments' => 1,
159  'restFiles' => [
160  'Foo-1.rst',
161  ],
162  ],
163  ],
164  '<?php
165  $args = [\'arg1\', \'arg2\', \'arg3\'];
166  $someVar::aMethod(...$args);',
167  [],
168  ],
169  'double linked .rst file is returned only once' => [
170  [
171  'Foo::aMethod' => [
172  'numberOfMandatoryArguments' => 1,
173  'maximumNumberOfArguments' => 1,
174  'restFiles' => [
175  'aRest.rst',
176  ],
177  ],
178  'Bar::aMethod' => [
179  'numberOfMandatoryArguments' => 1,
180  'maximumNumberOfArguments' => 1,
181  'restFiles' => [
182  'aRest.rst',
183  ],
184  ],
185  ],
186  '<?php
187  $someVar::aMethod();',
188  [
189  0 => [
190  'restFiles' => [
191  'aRest.rst',
192  ],
193  ],
194  ],
195  ],
196  ];
197  }
198 
206  public function matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected): void
207  {
208  ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
209  $statements = ‪$parser->parse($phpCode);
210 
211  $subject = new MethodArgumentRequiredStaticMatcher($configuration);
212 
213  $traverser = new NodeTraverser();
214  $traverser->addVisitor($subject);
215  $traverser->traverse($statements);
216 
217  $result = $subject->getMatches();
218  if (isset($expected[0], $result[0])) {
219  self::assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
220  } else {
221  self::assertEquals($expected, $result);
222  }
223  }
224 }
‪$parser
‪$parser
Definition: annotationChecker.php:110
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcherTest.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentRequiredStaticMatcher
Definition: MethodArgumentRequiredStaticMatcher.php:31