‪TYPO3CMS  ‪main
ArrayDimensionMatcherTest.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;
23 use PhpParser\PhpVersion;
24 use PHPUnit\Framework\Attributes\DataProvider;
25 use PHPUnit\Framework\Attributes\Test;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
29 final class ‪ArrayDimensionMatcherTest extends UnitTestCase
30 {
31  #[Test]
32  public function ‪hitsFromFixtureAreFound(): void
33  {
34  ‪$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2));
35  $fixtureFile = __DIR__ . '/Fixtures/ArrayDimensionMatcherFixture.php';
36  $statements = ‪$parser->parse(file_get_contents($fixtureFile));
37 
38  $traverser = new NodeTraverser();
39  $traverser->addVisitor(new NameResolver());
40 
41  $configuration = [
42  '$GLOBALS[\'TYPO3_CONF_VARS\'][\'FE\'][\'maxSessionDataSize\']' => [
43  'restFiles' => [
44  'Breaking-80700-DeprecatedFunctionalityRemoved.rst',
45  'Deprecation-70316-FrontendBasketWithRecs.rst',
46  ],
47  ],
48  ];
49  $subject = new ‪ArrayDimensionMatcher($configuration);
50  $traverser->addVisitor($subject);
51  $traverser->traverse($statements);
52  $expectedHitLineNumbers = [
53  28,
54  29,
55  ];
56  $actualHitLineNumbers = [];
57  foreach ($subject->getMatches() as $hit) {
58  $actualHitLineNumbers[] = $hit['line'];
59  }
60  self::assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
61  }
62 
63  public static function ‪matchesReturnsExpectedRestFilesDataProvider(): array
64  {
65  return [
66  'two candidates' => [
67  [
68  '$foo[\'bar\'][\'findMe\']' => [
69  'restFiles' => [
70  'Foo-1.rst',
71  'Foo-2.rst',
72  ],
73  ],
74  '$foo[\'findMe\']' => [
75  'restFiles' => [
76  'Bar-1.rst',
77  'Bar-2.rst',
78  ],
79  ],
80  ],
81  '<?php
82  $bar = $foo[\'findMe\'];',
83  [
84  0 => [
85  'restFiles' => [
86  'Foo-1.rst',
87  'Foo-2.rst',
88  'Bar-1.rst',
89  'Bar-2.rst',
90  ],
91  ],
92  ],
93  ],
94  'double linked .rst file is returned only once' => [
95  [
96  '$foo[\'bar\'][\'findMe\']' => [
97  'unusedArgumentNumbers' => [ 1 ],
98  'restFiles' => [
99  'aRest.rst',
100  ],
101  ],
102  '$foo[\'findMe\']' => [
103  'unusedArgumentNumbers' => [ 1 ],
104  'restFiles' => [
105  'aRest.rst',
106  ],
107  ],
108  ],
109  '<?php
110  $bar = $foo[\'findMe\'];',
111  [
112  0 => [
113  'restFiles' => [
114  'aRest.rst',
115  ],
116  ],
117  ],
118  ],
119  ];
120  }
121 
122  #[DataProvider('matchesReturnsExpectedRestFilesDataProvider')]
123  #[Test]
124  public function ‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected): void
125  {
126  ‪$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2));
127  $statements = ‪$parser->parse($phpCode);
128 
129  $subject = new ‪ArrayDimensionMatcher($configuration);
130 
131  $traverser = new NodeTraverser();
132  $traverser->addVisitor($subject);
133  $traverser->traverse($statements);
134 
135  $result = $subject->getMatches();
136  self::assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
137  }
138 }
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\ArrayDimensionMatcherTest\matchesReturnsExpectedRestFiles
‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
Definition: ArrayDimensionMatcherTest.php:124
‪$parser
‪$parser
Definition: annotationChecker.php:103
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcherTest.php:18
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\ArrayDimensionMatcherTest\hitsFromFixtureAreFound
‪hitsFromFixtureAreFound()
Definition: ArrayDimensionMatcherTest.php:32
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ArrayDimensionMatcher
Definition: ArrayDimensionMatcher.php:31
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\ArrayDimensionMatcherTest
Definition: ArrayDimensionMatcherTest.php:30
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\ArrayDimensionMatcherTest\matchesReturnsExpectedRestFilesDataProvider
‪static matchesReturnsExpectedRestFilesDataProvider()
Definition: ArrayDimensionMatcherTest.php:63