7643bfad13d6d3665d49c06d470a44c0c2a47659
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / ExtensionScanner / Php / Matcher / ArrayDimensionMatcherTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher;
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;
21 use TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ArrayDimensionMatcher;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Test case
26 */
27 class ArrayDimensionMatcherTest extends UnitTestCase
28 {
29 /**
30 * Subject is not notice free, disable E_NOTICES
31 */
32 protected static $suppressNotices = true;
33
34 /**
35 * @test
36 */
37 public function hitsFromFixtureAreFound()
38 {
39 $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
40 $fixtureFile = __DIR__ . '/Fixtures/ArrayDimensionMatcherFixture.php';
41 $statements = $parser->parse(file_get_contents($fixtureFile));
42
43 $traverser = new NodeTraverser();
44 $traverser->addVisitor(new NameResolver());
45
46 $configuration = [
47 '$GLOBALS[\'TYPO3_CONF_VARS\'][\'FE\'][\'maxSessionDataSize\']' => [
48 'restFiles' => [
49 'Breaking-80700-DeprecatedFunctionalityRemoved.rst',
50 'Deprecation-70316-FrontendBasketWithRecs.rst',
51 ],
52 ],
53 ];
54 $subject = new ArrayDimensionMatcher($configuration);
55 $traverser->addVisitor($subject);
56 $traverser->traverse($statements);
57 $expectedHitLineNumbers = [
58 26,
59 27,
60 ];
61 $actualHitLineNumbers = [];
62 foreach ($subject->getMatches() as $hit) {
63 $actualHitLineNumbers[] = $hit['line'];
64 }
65 $this->assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
66 }
67
68 /**
69 * @return array
70 */
71 public function matchesReturnsExpectedRestFilesDataProvider()
72 {
73 return [
74 'two candidates' => [
75 [
76 '$foo[\'bar\'][\'findMe\']' => [
77 'restFiles' => [
78 'Foo-1.rst',
79 'Foo-2.rst',
80 ],
81 ],
82 '$foo[\'findMe\']' => [
83 'restFiles' => [
84 'Bar-1.rst',
85 'Bar-2.rst',
86 ],
87 ],
88 ],
89 '<?php
90 $bar = $foo[\'findMe\'];',
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 'double linked .rst file is returned only once' => [
103 [
104 '$foo[\'bar\'][\'findMe\']' => [
105 'unusedArgumentNumbers' => [ 1 ],
106 'restFiles' => [
107 'aRest.rst',
108 ],
109 ],
110 '$foo[\'findMe\']' => [
111 'unusedArgumentNumbers' => [ 1 ],
112 'restFiles' => [
113 'aRest.rst',
114 ],
115 ],
116 ],
117 '<?php
118 $bar = $foo[\'findMe\'];',
119 [
120 0 => [
121 'restFiles' => [
122 'aRest.rst',
123 ],
124 ],
125 ],
126 ],
127 ];
128 }
129
130 /**
131 * @test
132 * @dataProvider matchesReturnsExpectedRestFilesDataProvider
133 */
134 public function matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
135 {
136 $parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
137 $statements = $parser->parse($phpCode);
138
139 $subject = new ArrayDimensionMatcher($configuration);
140
141 $traverser = new NodeTraverser();
142 $traverser->addVisitor($subject);
143 $traverser->traverse($statements);
144
145 $result = $subject->getMatches();
146 $this->assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
147 }
148 }