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