1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace a9f \FractorTypoScript ;
6
+
7
+ use a9f \Fractor \DependencyInjection \ContainerContainerBuilder ;
8
+ use a9f \Fractor \Exception \ShouldNotHappenException ;
9
+ use a9f \FractorTypoScript \Tests \Fixture \StatementCollectingVisitor ;
10
+ use Helmich \TypoScriptParser \Parser \Parser ;
11
+ use PHPUnit \Framework \Attributes \Test ;
12
+ use PHPUnit \Framework \TestCase ;
13
+ use Psr \Container \ContainerInterface ;
14
+
15
+ final class TypoScriptStatementsIteratorTest extends TestCase
16
+ {
17
+ private ?ContainerInterface $ currentContainer = null ;
18
+
19
+ protected function setUp (): void
20
+ {
21
+ parent ::setUp ();
22
+
23
+ $ this ->currentContainer = (new ContainerContainerBuilder ())
24
+ ->createDependencyInjectionContainer (__DIR__ . '/config/fractor.php ' , [
25
+ __DIR__ . '/config/config.php ' ,
26
+ ]);
27
+ }
28
+
29
+ #[Test]
30
+ public function visitorsAreCalledForAllStatements (): void
31
+ {
32
+ $ parser = $ this ->getService (Parser::class);
33
+ $ nodes = $ parser ->parseString (<<<TS
34
+ page = PAGE
35
+ page.10 = TEXT
36
+ page.10.value = Hello World!
37
+ TS );
38
+
39
+ $ calls = [];
40
+ $ subject = new TypoScriptStatementsIterator ([new StatementCollectingVisitor ('statements ' , $ calls )]);
41
+ $ subject ->traverseDocument ($ nodes );
42
+
43
+ self ::assertSame ([
44
+ 'statements:beforeTraversal:3 ' ,
45
+ 'statements:enterNode:Helmich\TypoScriptParser\Parser\AST\Operator\Assignment:l-1 ' ,
46
+ 'statements:leaveNode:Helmich\TypoScriptParser\Parser\AST\Operator\Assignment:l-1 ' ,
47
+ 'statements:enterNode:Helmich\TypoScriptParser\Parser\AST\Operator\ObjectCreation:l-2 ' ,
48
+ 'statements:leaveNode:Helmich\TypoScriptParser\Parser\AST\Operator\ObjectCreation:l-2 ' ,
49
+ 'statements:enterNode:Helmich\TypoScriptParser\Parser\AST\Operator\Assignment:l-3 ' ,
50
+ 'statements:leaveNode:Helmich\TypoScriptParser\Parser\AST\Operator\Assignment:l-3 ' ,
51
+ 'statements:afterTraversal:3 ' ,
52
+ ], $ calls );
53
+ }
54
+
55
+ /**
56
+ * @template T of object
57
+ * @phpstan-param class-string<T> $type
58
+ * @phpstan-return T
59
+ */
60
+ protected function getService (string $ type ): object
61
+ {
62
+ if ($ this ->currentContainer === null ) {
63
+ throw new ShouldNotHappenException ('Container is not initalized ' );
64
+ }
65
+
66
+ return $ this ->currentContainer ->get ($ type )
67
+ ?? throw new ShouldNotHappenException (sprintf ('Service "%s" was not found ' , $ type ));
68
+ }
69
+ }
0 commit comments