‪TYPO3CMS  ‪main
ArrayGlobalMatcherTest.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 ‪ArrayGlobalMatcherTest extends UnitTestCase
30 {
31  #[Test]
32  public function ‪hitsFromFixtureAreFound(): void
33  {
34  ‪$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2));
35  $fixtureFile = __DIR__ . '/Fixtures/ArrayGlobalMatcherFixture.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_DB\']' => [
43  'restFiles' => [
44  'Breaking-80929-TYPO3_DBMovedToExtension.rst',
45  ],
46  ],
47  ];
48  $subject = new ‪ArrayGlobalMatcher($configuration);
49  $traverser->addVisitor($subject);
50  $traverser->traverse($statements);
51  $expectedHitLineNumbers = [
52  28,
53  ];
54  $actualHitLineNumbers = [];
55  foreach ($subject->getMatches() as $hit) {
56  $actualHitLineNumbers[] = $hit['line'];
57  }
58  self::assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
59  }
60 
61  public static function ‪matchesReturnsExpectedRestFilesDataProvider(): array
62  {
63  return [
64  'one match' => [
65  [
66  '$GLOBALS[\'foo\']' => [
67  'restFiles' => [
68  'Foo-1.rst',
69  'Foo-2.rst',
70  ],
71  ],
72  ],
73  '<?php
74  $bar = $GLOBALS[\'foo\'] = \'bar\';',
75  [
76  0 => [
77  'restFiles' => [
78  'Foo-1.rst',
79  'Foo-2.rst',
80  ],
81  ],
82  ],
83  ],
84  ];
85  }
86 
87  #[DataProvider('matchesReturnsExpectedRestFilesDataProvider')]
88  #[Test]
89  public function ‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected): void
90  {
91  ‪$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2));
92  $statements = ‪$parser->parse($phpCode);
93 
94  $subject = new ‪ArrayGlobalMatcher($configuration);
95 
96  $traverser = new NodeTraverser();
97  $traverser->addVisitor($subject);
98  $traverser->traverse($statements);
99 
100  $result = $subject->getMatches();
101  self::assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
102  }
103 }
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\ArrayGlobalMatcherTest\matchesReturnsExpectedRestFilesDataProvider
‪static matchesReturnsExpectedRestFilesDataProvider()
Definition: ArrayGlobalMatcherTest.php:61
‪$parser
‪$parser
Definition: annotationChecker.php:103
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\ArrayGlobalMatcherTest\matchesReturnsExpectedRestFiles
‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
Definition: ArrayGlobalMatcherTest.php:89
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcherTest.php:18
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\ArrayGlobalMatcherTest
Definition: ArrayGlobalMatcherTest.php:30
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\ArrayGlobalMatcherTest\hitsFromFixtureAreFound
‪hitsFromFixtureAreFound()
Definition: ArrayGlobalMatcherTest.php:32
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ArrayGlobalMatcher
Definition: ArrayGlobalMatcher.php:31