Skip to content

Commit 1df3941

Browse files
committed
Add unit tests for handleNodeEvent()
1 parent 314ac2d commit 1df3941

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

tests/Phug/Component/ComponentExtensionTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
namespace Phug\Test\Component;
44

55
use PHPUnit\Framework\TestCase;
6+
use Phug\Compiler\Event\NodeEvent;
67
use Phug\CompilerEvent;
78
use Phug\Component\ComponentExtension;
89
use Phug\Formatter\Element\KeywordElement;
910
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;
1015
use Phug\Phug;
1116
use Phug\Renderer;
17+
use Phug\Util\Partial\ValueTrait;
1218
use XhtmlFormatter\Formatter;
1319

1420
/**
@@ -214,4 +220,42 @@ public function testSlotKeyword()
214220
'end' => '<?php } ?>',
215221
], $slot('foobar', $keyword));
216222
}
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+
}
217261
}

0 commit comments

Comments
 (0)