Skip to content

Add tests for View\Layout\Reader\Block and slight refactoring #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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::class
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace \Magento\Framework\View\Layout\Reader\Block::class with "\Magento\Framework\View\Layout\Reader\Block"
All same usage in test will be refactored automatically after php 5.4 become deprecated.

);

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

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to not testing ScheduledStructure in this test. This is private representation of layout and should not used outside

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the scheduled structure is used by the class under test, View\Layout\Reader\Block, and this is an integration test, it seems to me to probably be the best way to check things are working as they are intended.

Beyond mocking the Layout\ScheduledStructure\Helper and the View\Layout\ScheduledStructure and setting expectations I don't see any other way.
And that would still make the tests specific to the implementation by the ScheduledStructure of course.

If you don't mind, please suggest a better, alternative. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I did not see in the code depending on the class. Your test is fully acceptable in this case.

$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']
);
$expectedAction = [
'setTestAction',
['test_action_param' => [
'name' => 'test_action_param', 'xsi:type' => 'string', 'value' => 'test-action-value']
]
];
$this->assertEquals(
[$expectedAction],
$resultElementData['actions']
);

$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']
);
$expectedAction = [
'setTestAction',
['test_action_param' => [
'name' => 'test_action_param', 'xsi:type' => 'string', 'value' => 'test-action-value']
]
];
$this->assertEquals(
[$expectedAction],
$resultElementData['actions']
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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>
<action method="setTestAction">
<argument name="test_action_param" xsi:type="string">test-action-value</argument>
</action>
<block class="Dummy\Class" name="test.child.block"/>
</block>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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>
<action method="setTestAction">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove action node from layout. It's deprecated. see: Layout Test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, thats good to know, thanks.
Can you point me to some information or an example what those methods will be replaced with?

<argument name="test_action_param" xsi:type="string">test-action-value</argument>
</action>
</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 @@ -120,7 +120,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 @@ -173,7 +174,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