‪TYPO3CMS  ‪main
IncludeTreeSyntaxScannerVisitorTest.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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
27 use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSyntaxScannerVisitor;
29 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
30 
31 final class ‪IncludeTreeSyntaxScannerVisitorTest extends FunctionalTestCase
32 {
37  private function ‪removeLineFromErrors(array ‪$errors): array
38  {
39  foreach (‪$errors as &$error) {
40  unset($error['line']);
41  }
42  return ‪$errors;
43  }
44 
45  public static function ‪visitDataProvider(): iterable
46  {
47  $node = new ‪FileInclude();
48  $node->setLineStream((new ‪LosslessTokenizer())->tokenize('
49  foo {
50  bar = barValue
51  }
52  '));
53  yield 'no errors detected' => [
54  $node,
55  [],
56  ];
57 
58  $node = new ‪FileInclude();
59  $node->setLineStream((new ‪LosslessTokenizer())->tokenize('
60  foo {
61  bar = barValue
62  '));
63  yield 'brace missing' => [
64  $node,
65  [
66  [
67  'type' => 'brace.missing',
68  'include' => $node,
69  'lineNumber' => 3,
70  ],
71  ],
72  ];
73 
74  $node = new ‪FileInclude();
75  $node->setLineStream((new ‪LosslessTokenizer())->tokenize('
76  }
77  '));
78  yield 'brace excess' => [
79  $node,
80  [
81  [
82  'type' => 'brace.excess',
83  'include' => $node,
84  'lineNumber' => 1,
85  ],
86  ],
87  ];
88 
89  $node = new ‪FileInclude();
90  $node->setLineStream((new ‪LosslessTokenizer())->tokenize('
91  foo {
92  bar = barValue
93  }
94  }
95 
96  foo2 {
97  bar2 = bar2Value
98  '));
99  yield 'brace excess and brace missing' => [
100  $node,
101  [
102  [
103  'type' => 'brace.excess',
104  'include' => $node,
105  'lineNumber' => 4,
106  ],
107  [
108  'type' => 'brace.missing',
109  'include' => $node,
110  'lineNumber' => 8,
111  ],
112  ],
113  ];
114 
115  $node = new ‪FileInclude();
116  $node->setLineStream((new ‪LosslessTokenizer())->tokenize('
117  foo <
118  '));
119  yield 'invalid line' => [
120  $node,
121  [
122  [
123  'type' => 'line.invalid',
124  'include' => $node,
125  'lineNumber' => 1,
126  ],
127  ],
128  ];
129  }
130 
131  #[DataProvider('visitDataProvider')]
132  #[Test]
133  public function ‪visit(‪IncludeInterface $node, array $expectedErrors): void
134  {
135  $subject = new IncludeTreeSyntaxScannerVisitor();
136  $subject->visit($node, 0);
137  self::assertEquals($expectedErrors, $this->‪removeLineFromErrors($subject->getErrors()));
138  }
139 
140  #[Test]
141  public function ‪visitFindsEmptyImports()
142  {
143  $this->importCSVDataSet(__DIR__ . '/../Fixtures/IncludeTreeSyntaxScannerVisitor/RootTemplate.csv');
144  $rootline = [
145  [
146  'uid' => 1,
147  'pid' => 0,
148  'is_siteroot' => 0,
149  ],
150  ];
151  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
152  $subject = $this->get(SysTemplateTreeBuilder::class);
153  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LosslessTokenizer());
154  $traverser = new ‪IncludeTreeTraverser();
155  $visitor = new IncludeTreeSyntaxScannerVisitor();
156  $traverser->traverse($includeTree, [$visitor]);
157  $erroneousLineNumbers = array_column($visitor->getErrors(), 'lineNumber');
158  $expectedLineNumbers = [0, 2, 4, 6, 9, 12, 13, 15];
159  self::assertSame($expectedLineNumbers, $erroneousLineNumbers);
160  }
161 }
‪TYPO3\CMS\Core\TypoScript\IncludeTree\SysTemplateRepository
Definition: SysTemplateRepository.php:38
‪TYPO3\CMS\Core\TypoScript\Tokenizer\LosslessTokenizer
Definition: LosslessTokenizer.php:61
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Traverser\IncludeTreeTraverser
Definition: IncludeTreeTraverser.php:30
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\Visitor\IncludeTreeSyntaxScannerVisitorTest\removeLineFromErrors
‪removeLineFromErrors(array $errors)
Definition: IncludeTreeSyntaxScannerVisitorTest.php:37
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\Visitor\IncludeTreeSyntaxScannerVisitorTest
Definition: IncludeTreeSyntaxScannerVisitorTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\Visitor\IncludeTreeSyntaxScannerVisitorTest\visit
‪visit(IncludeInterface $node, array $expectedErrors)
Definition: IncludeTreeSyntaxScannerVisitorTest.php:133
‪TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\IncludeInterface
Definition: IncludeInterface.php:39
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\Visitor
Definition: IncludeTreeSetupConditionConstantSubstitutionVisitorTest.php:18
‪TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\FileInclude
Definition: FileInclude.php:28
‪$errors
‪$errors
Definition: annotationChecker.php:116
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\Visitor\IncludeTreeSyntaxScannerVisitorTest\visitDataProvider
‪static visitDataProvider()
Definition: IncludeTreeSyntaxScannerVisitorTest.php:45
‪TYPO3\CMS\Core\TypoScript\IncludeTree\SysTemplateTreeBuilder
Definition: SysTemplateTreeBuilder.php:74
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\Visitor\IncludeTreeSyntaxScannerVisitorTest\visitFindsEmptyImports
‪visitFindsEmptyImports()
Definition: IncludeTreeSyntaxScannerVisitorTest.php:141