-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
); | ||
|
||
$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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the scheduled structure is used by the class under test, Beyond mocking the If you don't mind, please suggest a better, alternative. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove action node from layout. It's deprecated. see: Layout Test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, thats good to know, thanks. |
||
<argument name="test_action_param" xsi:type="string">test-action-value</argument> | ||
</action> | ||
</referenceBlock> | ||
</body> | ||
</page> |
There was a problem hiding this comment.
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.