|
3 | 3 | namespace Phug\Test\Component; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\TestCase; |
| 6 | +use Phug\Compiler\Event\NodeEvent; |
6 | 7 | use Phug\CompilerEvent; |
7 | 8 | use Phug\Component\ComponentExtension; |
8 | 9 | use Phug\Formatter\Element\KeywordElement; |
9 | 10 | use Phug\Formatter\Element\TextElement; |
| 11 | +use Phug\Parser\Node\ElementNode; |
| 12 | +use Phug\Parser\Node\KeywordNode; |
| 13 | +use Phug\Parser\Node\MixinCallNode; |
| 14 | +use Phug\Parser\Node\TextNode; |
10 | 15 | use Phug\Phug; |
11 | 16 | use Phug\Renderer; |
| 17 | +use Phug\Util\Partial\ValueTrait; |
12 | 18 | use XhtmlFormatter\Formatter; |
13 | 19 |
|
14 | 20 | /** |
@@ -214,4 +220,42 @@ public function testSlotKeyword() |
214 | 220 | 'end' => '<?php } ?>', |
215 | 221 | ], $slot('foobar', $keyword)); |
216 | 222 | } |
| 223 | + |
| 224 | + protected function getValue($object) |
| 225 | + { |
| 226 | + $this->assertTrue(method_exists($object, 'getValue'), (is_object($object) ? get_class($object) : gettype($object)).' unexpected.'); |
| 227 | + |
| 228 | + /** @var ValueTrait $node */ |
| 229 | + $node = $object; |
| 230 | + |
| 231 | + return $node->getValue(); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * @covers ::handleNodeEvent |
| 236 | + */ |
| 237 | + public function testHandleNodeEvent() |
| 238 | + { |
| 239 | + $extension = new ComponentExtension(new Renderer()); |
| 240 | + $children = [ |
| 241 | + (new KeywordNode)->setName('foo')->setValue('bar'), |
| 242 | + (new KeywordNode)->setName('slot')->setValue('header'), |
| 243 | + new TextNode(), |
| 244 | + ]; |
| 245 | + $tag = new ElementNode(null, null, null, null, $children); |
| 246 | + $extension->handleNodeEvent(new NodeEvent($tag)); |
| 247 | + |
| 248 | + $this->assertSame($children, $tag->getChildren()); |
| 249 | + |
| 250 | + $call = new MixinCallNode(null, null, null, null, $children); |
| 251 | + $extension->handleNodeEvent(new NodeEvent($call)); |
| 252 | + |
| 253 | + $this->assertCount(5, $call->getChildren()); |
| 254 | + $this->assertSame('$pug_component_slot = null', $this->getValue($call->getChildAt(0))); |
| 255 | + $this->assertSame('if ($pug_component_slot === "__main__")', $this->getValue($call->getChildAt(1)->getChildAt(0))); |
| 256 | + $this->assertSame($children[0], $call->getChildAt(1)->getChildAt(1)); |
| 257 | + $this->assertSame($children[1], $call->getChildAt(2)); |
| 258 | + $this->assertSame('if ($pug_component_slot === "__main__")', $this->getValue($call->getChildAt(3)->getChildAt(0))); |
| 259 | + $this->assertSame($children[2], $call->getChildAt(3)->getChildAt(1)); |
| 260 | + } |
217 | 261 | } |
0 commit comments