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