groups leyout refactor

This commit is contained in:
idradm 2015-06-15 14:58:48 +02:00
parent cae634dfb1
commit b9d4925f52
3 changed files with 80 additions and 19 deletions

View file

@ -178,14 +178,6 @@ class Node {
: null;
}
protected function getXmlAttributeFromSupported( \SimpleXMLElement $xmlNode, $attribute, $supportedAttributes ) {
$attr = $this->getXmlAttribute( $xmlNode, $attribute );
if ( isset($attr) && in_array( $attr, $supportedAttributes ) ) {
return $attr;
}
return static::DEFAULT_TAG_NAME;
}
protected function getRawInfoboxData( $key ) {
return isset( $this->infoboxData[ $key ] ) ? $this->infoboxData[ $key ]
: null;

View file

@ -13,8 +13,7 @@ class NodeGroup extends Node {
public function getData() {
if ( !isset( $this->data ) ) {
$this->data = [ 'value' => $this->getDataForChildren(),
'layout' => $this->getXmlAttributeFromSupported( $this->xmlNode,
self::DATA_LAYOUT_ATTR_NAME, $this->supportedGroupLayouts) ];
'layout' => $this->getLayout() ];
}
return $this->data;
@ -23,9 +22,8 @@ class NodeGroup extends Node {
public function getRenderData() {
return [
'type' => $this->getType(),
'data' => [ 'value' => $this->getRenderDataForChildren(),
'layout' => $this->getXmlAttributeFromSupported( $this->xmlNode,
self::DATA_LAYOUT_ATTR_NAME, $this->supportedGroupLayouts) ],
'data' => [ 'value' => $this->getRenderDataForChildren(),
'layout' => $this->getLayout() ],
];
}
@ -43,4 +41,14 @@ class NodeGroup extends Node {
public function getSource() {
return $this->getSourceForChildren();
}
/**
* @return string
*/
protected function getLayout() {
$layout = $this->getXmlAttribute( $this->xmlNode, self::DATA_LAYOUT_ATTR_NAME );
return ( isset( $layout ) && in_array( $layout, $this->supportedGroupLayouts ) ) ? $layout
: self::DEFAULT_TAG_NAME;
}
}

View file

@ -1,7 +1,68 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: adamr
* Date: 15/06/15
* Time: 14:38
*/
class NodeGroupTest extends WikiaBaseTest {
protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
parent::setUp();
}
/**
* @covers NodeGroup::getData
* @dataProvider groupNodeTestProvider
* @param $markup
* @param $params
* @param $expected
*/
public function testNodeGroup( $markup, $params, $expected ) {
$node = \Wikia\PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, $params );
$this->assertEquals( $expected, $node->getData() );
}
public function groupNodeTestProvider() {
return [
[ '<group><data source="elem1"><label>l1</label><default>def1</default></data><data source="elem2">
<label>l2</label><default>def2</default></data><data source="elem3"><label>l2</label></data></group>',
[ 'elem1' => 1, 'elem2' => 2 ],
[ 'value' =>
[
[ 'type' => 'data', 'isEmpty' => false, 'data' => [ 'label' => 'l1', 'value' => 1 ],
'source' => [ 'elem1' ] ],
[ 'type' => 'data', 'isEmpty' => false, 'data' => [ 'label' => 'l2', 'value' => 2 ],
'source' => [ 'elem2' ] ],
[ 'type' => 'data', 'isEmpty' => true, 'data' => [ 'label' => 'l2', 'value' => null ],
'source' => [ 'elem3' ] ]
],
'layout' => 'default'
] ],
[ '<group layout="horizontal"><data source="elem1"><label>l1</label><default>def1</default></data><data source="elem2">
<label>l2</label><default>def2</default></data><data source="elem3"><label>l2</label></data></group>',
[ 'elem1' => 1, 'elem2' => 2 ],
[ 'value' =>
[
[ 'type' => 'data', 'isEmpty' => false, 'data' => [ 'label' => 'l1', 'value' => 1 ],
'source' => [ 'elem1' ] ],
[ 'type' => 'data', 'isEmpty' => false, 'data' => [ 'label' => 'l2', 'value' => 2 ],
'source' => [ 'elem2' ] ],
[ 'type' => 'data', 'isEmpty' => true, 'data' => [ 'label' => 'l2', 'value' => null ],
'source' => [ 'elem3' ] ],
],
'layout' => 'horizontal'
] ],
[ '<group layout="loool"><data source="elem1"><label>l1</label><default>def1</default></data><data source="elem2">
<label>l2</label><default>def2</default></data><data source="elem3"><label>l2</label></data></group>',
[ 'elem1' => 1, 'elem2' => 2 ],
[ 'value' =>
[
[ 'type' => 'data', 'isEmpty' => false, 'data' => [ 'label' => 'l1', 'value' => 1 ],
'source' => [ 'elem1' ] ],
[ 'type' => 'data', 'isEmpty' => false, 'data' => [ 'label' => 'l2', 'value' => 2 ],
'source' => [ 'elem2' ] ],
[ 'type' => 'data', 'isEmpty' => true, 'data' => [ 'label' => 'l2', 'value' => null ],
'source' => [ 'elem3' ] ],
],
'layout' => 'default'
] ],
];
}
}