/
IncludeTreeSyntaxScannerVisitorTest.php
163 lines (150 loc) · 4.91 KB
/
IncludeTreeSyntaxScannerVisitorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\Visitor;
use TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\FileInclude;
use TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\IncludeInterface;
use TYPO3\CMS\Core\TypoScript\IncludeTree\SysTemplateRepository;
use TYPO3\CMS\Core\TypoScript\IncludeTree\SysTemplateTreeBuilder;
use TYPO3\CMS\Core\TypoScript\IncludeTree\Traverser\IncludeTreeTraverser;
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSyntaxScannerVisitor;
use TYPO3\CMS\Core\TypoScript\Tokenizer\LosslessTokenizer;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
final class IncludeTreeSyntaxScannerVisitorTest extends FunctionalTestCase
{
/**
* Helper method to remove line. This is more convenient
* to compare, we simply rely on lineNumber.
*/
private function removeLineFromErrors(array $errors): array
{
foreach ($errors as &$error) {
unset($error['line']);
}
return $errors;
}
public static function visitDataProvider(): iterable
{
$node = new FileInclude();
$node->setLineStream((new LosslessTokenizer())->tokenize('
foo {
bar = barValue
}
'));
yield 'no errors detected' => [
$node,
[],
];
$node = new FileInclude();
$node->setLineStream((new LosslessTokenizer())->tokenize('
foo {
bar = barValue
'));
yield 'brace missing' => [
$node,
[
[
'type' => 'brace.missing',
'include' => $node,
'lineNumber' => 3,
],
],
];
$node = new FileInclude();
$node->setLineStream((new LosslessTokenizer())->tokenize('
}
'));
yield 'brace excess' => [
$node,
[
[
'type' => 'brace.excess',
'include' => $node,
'lineNumber' => 1,
],
],
];
$node = new FileInclude();
$node->setLineStream((new LosslessTokenizer())->tokenize('
foo {
bar = barValue
}
}
foo2 {
bar2 = bar2Value
'));
yield 'brace excess and brace missing' => [
$node,
[
[
'type' => 'brace.excess',
'include' => $node,
'lineNumber' => 4,
],
[
'type' => 'brace.missing',
'include' => $node,
'lineNumber' => 8,
],
],
];
$node = new FileInclude();
$node->setLineStream((new LosslessTokenizer())->tokenize('
foo <
'));
yield 'invalid line' => [
$node,
[
[
'type' => 'line.invalid',
'include' => $node,
'lineNumber' => 1,
],
],
];
}
/**
* @test
* @dataProvider visitDataProvider
*/
public function visit(IncludeInterface $node, array $expectedErrors): void
{
$subject = new IncludeTreeSyntaxScannerVisitor();
$subject->visit($node, 0);
self::assertEquals($expectedErrors, $this->removeLineFromErrors($subject->getErrors()));
}
/**
* @test
*/
public function visitFindsEmptyImports()
{
$this->importCSVDataSet(__DIR__ . '/../Fixtures/IncludeTreeSyntaxScannerVisitor/RootTemplate.csv');
$rootline = [
[
'uid' => 1,
'pid' => 0,
'is_siteroot' => 0,
],
];
$sysTemplateRepository = $this->get(SysTemplateRepository::class);
$subject = $this->get(SysTemplateTreeBuilder::class);
$includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new LosslessTokenizer());
$traverser = new IncludeTreeTraverser();
$visitor = new IncludeTreeSyntaxScannerVisitor();
$traverser->traverse($includeTree, [$visitor]);
$erroneousLineNumbers = array_column($visitor->getErrors(), 'lineNumber');
$expectedLineNumbers = [0, 2, 4, 6, 9, 12, 13, 15];
self::assertSame($expectedLineNumbers, $erroneousLineNumbers);
}
}