17fa6b11517c514920c1655bf80b3007ff41e460
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / ExtensionScanner / Php / Matcher / ClassConstantMatcherTest.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\GeneratorClassesResolver;
22 use TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ClassConstantMatcher;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24
25 /**
26 * Test case
27 */
28 class ClassConstantMatcherTest extends UnitTestCase
29 {
30 /**
31 * @test
32 */
33 public function hitsFromFixtureAreFound()
34 {
35 $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
36 $fixtureFile = __DIR__ . '/Fixtures/ClassConstantMatcherFixture.php';
37 $statements = $parser->parse(file_get_contents($fixtureFile));
38
39 $traverser = new NodeTraverser();
40 $traverser->addVisitor(new NameResolver());
41 $traverser->addVisitor(new GeneratorClassesResolver());
42
43 $configuration = [
44 'TYPO3\CMS\Backend\Template\DocumentTemplate::STATUS_ICON_ERROR' => [
45 'restFiles' => [
46 'Breaking-80700-DeprecatedFunctionalityRemoved.rst',
47 ],
48 ],
49 ];
50 $subject = new ClassConstantMatcher($configuration);
51 $traverser->addVisitor($subject);
52 $traverser->traverse($statements);
53 $expectedHitLineNumbers = [
54 28,
55 29,
56 ];
57 $actualHitLineNumbers = [];
58 foreach ($subject->getMatches() as $hit) {
59 $actualHitLineNumbers[] = $hit['line'];
60 }
61 $this->assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
62 }
63
64 /**
65 * @return array
66 */
67 public function matchesReturnsExpectedRestFilesDataProvider()
68 {
69 return [
70 'a straight match' => [
71 [
72 'Foo\Bar::aClassConstant' => [
73 'restFiles' => [
74 'Foo-1.rst',
75 'Foo-2.rst',
76 ],
77 ],
78 ],
79 '<?php
80 $foo = \Foo\Bar::aClassConstant;',
81 [
82 0 => [
83 'restFiles' => [
84 'Foo-1.rst',
85 'Foo-2.rst',
86 ],
87 ],
88 ],
89 ],
90 ];
91 }
92
93 /**
94 * @test
95 * @dataProvider matchesReturnsExpectedRestFilesDataProvider
96 */
97 public function matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
98 {
99 $parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
100 $statements = $parser->parse($phpCode);
101
102 $subject = new ClassConstantMatcher($configuration);
103
104 $traverser = new NodeTraverser();
105 $traverser->addVisitor(new NameResolver());
106 $traverser->addVisitor($subject);
107 $traverser->traverse($statements);
108
109 $result = $subject->getMatches();
110 $this->assertSame($expected[0]['restFiles'], $result[0]['restFiles']);
111 }
112 }