‪TYPO3CMS  10.4
ConstantMatcherTest.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;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ConstantMatcherTest extends UnitTestCase
31 {
35  public function hitsFromFixtureAreFound()
36  {
37  ‪$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
38  $fixtureFile = __DIR__ . '/Fixtures/ConstantMatcherFixture.php';
39  $statements = ‪$parser->parse(file_get_contents($fixtureFile));
40 
41  $traverser = new NodeTraverser();
42  $traverser->addVisitor(new NameResolver());
43  $traverser->addVisitor(new GeneratorClassesResolver());
44 
45  $configuration = [
46  'TYPO3_DLOG' => [
47  'restFiles' => [
48  'Breaking-82162-GlobalErrorConstantsRemoved.rst',
49  ],
50  ],
51  ];
52  $subject = new ConstantMatcher($configuration);
53  $traverser->addVisitor($subject);
54  $traverser->traverse($statements);
55  $expectedHitLineNumbers = [
56  28,
57  ];
58  $actualHitLineNumbers = [];
59  foreach ($subject->getMatches() as $hit) {
60  $actualHitLineNumbers[] = $hit['line'];
61  }
62  self::assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
63  }
64 
68  public function matchesReturnsExpectedRestFilesDataProvider()
69  {
70  return [
71  'a straight match' => [
72  [
73  'aConstant' => [
74  'restFiles' => [
75  'Foo-1.rst',
76  'Foo-2.rst',
77  ],
78  ],
79  ],
80  '<?php
81  $foo = aConstant;',
82  [
83  0 => [
84  'restFiles' => [
85  'Foo-1.rst',
86  'Foo-2.rst',
87  ],
88  ],
89  ],
90  ],
91  ];
92  }
93 
98  public function matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
99  {
100  ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
101  $statements = ‪$parser->parse($phpCode);
102 
103  $subject = new ConstantMatcher($configuration);
104 
105  $traverser = new NodeTraverser();
106  $traverser->addVisitor(new NameResolver());
107  $traverser->addVisitor($subject);
108  $traverser->traverse($statements);
109 
110  $result = $subject->getMatches();
111  self::assertSame($expected[0]['restFiles'], $result[0]['restFiles']);
112  }
113 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver
Definition: GeneratorClassesResolver.php:41
‪$parser
‪$parser
Definition: annotationChecker.php:108
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcherTest.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ConstantMatcher
Definition: ConstantMatcher.php:30