Skip to content

Commit

Permalink
Merge pull request #906 from Vinai/view-layout-reader-block
Browse files Browse the repository at this point in the history
Add tests for View\Layout\Reader\Block and slight refactoring (MAGETWO-32829)
  • Loading branch information
vpelipenko committed Jan 20, 2015
2 parents 1168042 + 8689706 commit 2b16af7
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Magento\Framework\View\Layout\Reader;

class BlockTest extends \PHPUnit_Framework_TestCase
{
const IDX_TYPE = 0;
const IDX_PARENT = 2;

/**
* @var Block
*/
private $block;

/**
* @var Context
*/
private $readerContext;

private $blockName = 'test.block';
private $childBlockName = 'test.child.block';

public function setUp()
{
$this->block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
'Magento\Framework\View\Layout\Reader\Block'
);

$this->readerContext = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
'Magento\Framework\View\Layout\Reader\Context'
);
}

public function testInterpretBlockDirective()
{
$pageXml = new \Magento\Framework\View\Layout\Element(__DIR__ . '/_files/_layout_update_block.xml', 0, true);
$parentElement = new \Magento\Framework\View\Layout\Element('<page></page>');

foreach ($pageXml->xpath('body/block') as $blockElement) {
$this->assertTrue(in_array($blockElement->getName(), $this->block->getSupportedNodes()));

$this->block->interpret($this->readerContext, $blockElement, $parentElement);
}

$structure = $this->readerContext->getScheduledStructure();
$this->assertArrayHasKey($this->blockName, $structure->getStructure());
$this->assertEquals('block', $structure->getStructure()[$this->blockName][self::IDX_TYPE]);

$resultElementData = $structure->getStructureElementData($this->blockName);

$this->assertEquals(
['group' => 'test.group', 'class' => 'Dummy\Class', 'template' => 'test.phtml', 'ttl' => 3],
$resultElementData['attributes']
);
$this->assertEquals(
['test_arg' => ['name' => 'test_arg', 'xsi:type' => 'string', 'value' => 'test-argument-value']],
$resultElementData['arguments']
);

$this->assertEquals('block', $structure->getStructure()[$this->childBlockName][self::IDX_TYPE]);
$this->assertEquals($this->blockName, $structure->getStructure()[$this->childBlockName][self::IDX_PARENT]);
}

/**
* @depends testInterpretBlockDirective
*/
public function testInterpretReferenceBlockDirective()
{
$pageXml = new \Magento\Framework\View\Layout\Element(
__DIR__ . '/_files/_layout_update_reference.xml', 0, true
);
$parentElement = new \Magento\Framework\View\Layout\Element('<page></page>');

foreach ($pageXml->xpath('body/*') as $element) {
$this->assertTrue(in_array($element->getName(), $this->block->getSupportedNodes()));

$this->block->interpret($this->readerContext, $element, $parentElement);
}

$structure = $this->readerContext->getScheduledStructure();
$this->assertArrayHasKey($this->blockName, $structure->getStructure());
$this->assertEquals('block', $structure->getStructure()[$this->blockName][self::IDX_TYPE]);

$resultElementData = $structure->getStructureElementData($this->blockName);

$this->assertEquals(
['test_arg' => ['name' => 'test_arg', 'xsi:type' => 'string', 'value' => 'test-argument-value']],
$resultElementData['arguments']
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<block class="Dummy\Class"
name="test.block"
group="test.group"
template="test.phtml"
ttl="3">
<arguments>
<argument name="test_arg" xsi:type="string">test-argument-value</argument>
</arguments>
<block class="Dummy\Class" name="test.child.block"/>
</block>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<block class="Dummy\Class" name="test.block"/>
<referenceBlock name="test.block">
<arguments>
<argument name="test_arg" xsi:type="string">test-argument-value</argument>
</arguments>
</referenceBlock>
</body>
</page>
5 changes: 3 additions & 2 deletions lib/internal/Magento/Framework/View/Layout/Reader/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public function interpret(Context $readerContext, Layout\Element $currentElement
default:
break;
}
return $this->readerPool->interpret($readerContext, $currentElement);
$this->readerPool->interpret($readerContext, $currentElement);
return $this;
}

/**
Expand Down Expand Up @@ -174,7 +175,7 @@ protected function scheduleReference(
* Update data for scheduled element
*
* @param Layout\Element $currentElement
* @param array $data
* @param array &$data
* @return array
*/
protected function updateScheduledData($currentElement, array &$data)
Expand Down

0 comments on commit 2b16af7

Please sign in to comment.