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