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