‪TYPO3CMS  ‪main
GeneratorClassesResolverTest.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\Node\Expr\BinaryOp\Concat;
21 use PhpParser\Node\Expr\ClassConstFetch;
22 use PhpParser\Node\Expr\New_;
23 use PhpParser\Node\Expr\StaticCall;
24 use PhpParser\Node\Name\FullyQualified;
25 use PhpParser\NodeTraverser;
26 use PhpParser\ParserFactory;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪GeneratorClassesResolverTest extends UnitTestCase
32 {
37  {
38  $phpCode = <<<'EOC'
39 <?php
40 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Does\\Not\\Exist');
41 EOC;
42  ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
43  $statements = ‪$parser->parse($phpCode);
44  $traverser = new NodeTraverser();
45  $traverser->addVisitor(new ‪GeneratorClassesResolver());
46  $statements = $traverser->traverse($statements);
47  $node = $statements[0]->expr ?? null;
48  $argValue = $node->args[0]->value ?? null;
49  self::assertInstanceOf(StaticCall::class, $node);
50  self::assertInstanceOf(ClassConstFetch::class, $argValue);
51  self::assertInstanceOf(FullyQualified::class, $argValue->class);
52  self::assertEquals(['TYPO3', 'CMS', 'Does', 'Not', 'Exist'], $argValue->class->parts);
53  self::assertInstanceOf(New_::class, $node->getAttribute(‪AbstractCoreMatcher::NODE_RESOLVED_AS));
54  }
55 
60  {
61  $phpCode = <<<'EOC'
62 <?php
63 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Does\\Not\\' . $foo);
64 EOC;
65  ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
66  $statements = ‪$parser->parse($phpCode);
67  $traverser = new NodeTraverser();
68  $traverser->addVisitor(new ‪GeneratorClassesResolver());
69  $statements = $traverser->traverse($statements);
70  $argValue = $statements[0]->expr->args[0]->value ?? null;
71  // the fixture source above is a binary concatenation
72  self::assertInstanceOf(Concat::class, $argValue);
73  self::assertNotInstanceOf(FullyQualified::class, $argValue->class ?? null);
74  }
75 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver
Definition: GeneratorClassesResolver.php:42
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\NODE_RESOLVED_AS
‪const NODE_RESOLVED_AS
Definition: AbstractCoreMatcher.php:35
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher
Definition: AbstractCoreMatcher.php:34
‪$parser
‪$parser
Definition: annotationChecker.php:108
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\GeneratorClassesResolverTest\visitorDoesNotTransformDynamicallyCreatesFullyQualifiedNameFromStringArgumentInMakeInstance
‪visitorDoesNotTransformDynamicallyCreatesFullyQualifiedNameFromStringArgumentInMakeInstance()
Definition: GeneratorClassesResolverTest.php:59
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php
Definition: CodeStatisticsTest.php:18
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\GeneratorClassesResolverTest
Definition: GeneratorClassesResolverTest.php:32
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\GeneratorClassesResolverTest\visitorCreatesFullyQualifiedNameFromStringArgumentInMakeInstance
‪visitorCreatesFullyQualifiedNameFromStringArgumentInMakeInstance()
Definition: GeneratorClassesResolverTest.php:36