|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Magento\Framework\View\Layout\Reader; |
| 4 | + |
| 5 | +class BlockTest extends \PHPUnit_Framework_TestCase |
| 6 | +{ |
| 7 | + const IDX_TYPE = 0; |
| 8 | + const IDX_PARENT = 2; |
| 9 | + |
| 10 | + /** |
| 11 | + * @var Block |
| 12 | + */ |
| 13 | + private $block; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var Context |
| 17 | + */ |
| 18 | + private $readerContext; |
| 19 | + |
| 20 | + private $blockName = 'test.block'; |
| 21 | + private $childBlockName = 'test.child.block'; |
| 22 | + |
| 23 | + public function setUp() |
| 24 | + { |
| 25 | + $this->block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 26 | + 'Magento\Framework\View\Layout\Reader\Block' |
| 27 | + ); |
| 28 | + |
| 29 | + $this->readerContext = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 30 | + 'Magento\Framework\View\Layout\Reader\Context' |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function testInterpretBlockDirective() |
| 35 | + { |
| 36 | + $pageXml = new \Magento\Framework\View\Layout\Element(__DIR__ . '/_files/_layout_update_block.xml', 0, true); |
| 37 | + $parentElement = new \Magento\Framework\View\Layout\Element('<page></page>'); |
| 38 | + |
| 39 | + foreach ($pageXml->xpath('body/block') as $blockElement) { |
| 40 | + $this->assertTrue(in_array($blockElement->getName(), $this->block->getSupportedNodes())); |
| 41 | + |
| 42 | + $this->block->interpret($this->readerContext, $blockElement, $parentElement); |
| 43 | + } |
| 44 | + |
| 45 | + $structure = $this->readerContext->getScheduledStructure(); |
| 46 | + $this->assertArrayHasKey($this->blockName, $structure->getStructure()); |
| 47 | + $this->assertEquals('block', $structure->getStructure()[$this->blockName][self::IDX_TYPE]); |
| 48 | + |
| 49 | + $resultElementData = $structure->getStructureElementData($this->blockName); |
| 50 | + |
| 51 | + $this->assertEquals( |
| 52 | + ['group' => 'test.group', 'class' => 'Dummy\Class', 'template' => 'test.phtml', 'ttl' => 3], |
| 53 | + $resultElementData['attributes'] |
| 54 | + ); |
| 55 | + $this->assertEquals( |
| 56 | + ['test_arg' => ['name' => 'test_arg', 'xsi:type' => 'string', 'value' => 'test-argument-value']], |
| 57 | + $resultElementData['arguments'] |
| 58 | + ); |
| 59 | + |
| 60 | + $this->assertEquals('block', $structure->getStructure()[$this->childBlockName][self::IDX_TYPE]); |
| 61 | + $this->assertEquals($this->blockName, $structure->getStructure()[$this->childBlockName][self::IDX_PARENT]); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @depends testInterpretBlockDirective |
| 66 | + */ |
| 67 | + public function testInterpretReferenceBlockDirective() |
| 68 | + { |
| 69 | + $pageXml = new \Magento\Framework\View\Layout\Element( |
| 70 | + __DIR__ . '/_files/_layout_update_reference.xml', 0, true |
| 71 | + ); |
| 72 | + $parentElement = new \Magento\Framework\View\Layout\Element('<page></page>'); |
| 73 | + |
| 74 | + foreach ($pageXml->xpath('body/*') as $element) { |
| 75 | + $this->assertTrue(in_array($element->getName(), $this->block->getSupportedNodes())); |
| 76 | + |
| 77 | + $this->block->interpret($this->readerContext, $element, $parentElement); |
| 78 | + } |
| 79 | + |
| 80 | + $structure = $this->readerContext->getScheduledStructure(); |
| 81 | + $this->assertArrayHasKey($this->blockName, $structure->getStructure()); |
| 82 | + $this->assertEquals('block', $structure->getStructure()[$this->blockName][self::IDX_TYPE]); |
| 83 | + |
| 84 | + $resultElementData = $structure->getStructureElementData($this->blockName); |
| 85 | + |
| 86 | + $this->assertEquals( |
| 87 | + ['test_arg' => ['name' => 'test_arg', 'xsi:type' => 'string', 'value' => 'test-argument-value']], |
| 88 | + $resultElementData['arguments'] |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments