Skip to content

Commit 6a0df41

Browse files
committed
Full coverage for EncoderNodeChild.php
1 parent 51b58e2 commit 6a0df41

File tree

8 files changed

+100
-17
lines changed

8 files changed

+100
-17
lines changed

src/PE/Encoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ protected function _encode($object, EncoderNode $node, EncoderOptions $options,
354354
$children = $node->getChildren();
355355
foreach ($children as $childNodeName => $child) {
356356

357-
if (!EncoderNode::nodeExists($child->getNodeName())) {
358-
throw new EncoderException(sprintf('Cannot set the node name (%s) of a node child because it doesn\'t exist. Please add the requested node with "EncoderNode::addNode()". Current node name "%s" with class name "%s"', $child->getNodeName(), $node->getNodeName(), get_class($node)));
357+
if (!EncoderNode::nodeExists($child->getChildNodeName())) {
358+
throw new EncoderException(sprintf('Cannot set the node name (%s) of a node child because it doesn\'t exist. Please add the requested node with "EncoderNode::addNode()". Current node name "%s" with class name "%s"', $child->getChildNodeName(), $node->getNodeName(), get_class($node)));
359359
}
360360

361361
$childOptionPath = $optionNodeIndex . ':' . $childNodeName;

src/PE/nodes/EncoderNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ public function getChild($childName) {
435435
return $this->children->getChild($childName);
436436
}
437437
/**
438+
* @return EncoderNodeChild[]
439+
*
438440
* @see EncoderNodeChildren::getChildren()
439441
*/
440442
public function getChildren() {

src/PE/nodes/EncoderNodeChild.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,24 @@
66

77
class EncoderNodeChild extends EncoderNodeVariable {
88

9-
private $nodeName;
9+
private $childNodeName;
1010
private $isArray = true;
1111
private $setAfterChildren = true;
1212
private $setAfterAttributes = true;
1313

1414
function __construct($nodeName, $options = null) {
15-
1615
parent::__construct('', $options);
17-
18-
$this->setNodeName($nodeName);
16+
$this->setChildNodeName($nodeName);
1917
}
2018

21-
public function setNodeName($nodeName) {
22-
if ($nodeName === null || $nodeName === '') {
19+
public function setChildNodeName($childNodeName) {
20+
if ($childNodeName === null || $childNodeName === '') {
2321
throw new EncoderNodeChildException('Node name cannot be null or empty');
2422
}
25-
$this->nodeName = $nodeName;
23+
$this->childNodeName = $childNodeName;
2624
}
27-
public function getNodeName() {
28-
return $this->nodeName;
25+
public function getChildNodeName() {
26+
return $this->childNodeName;
2927
}
3028

3129
// @todo Figure out if this feature is still necessary. Because if you use a single node, can't we simply assume it isn't an array?
@@ -60,7 +58,7 @@ public function setAfterAttributes($bool = null) {
6058
public function addChildrenToObject($target, $values) {
6159
$methodName = $this->getSetterMethod();
6260
if ($methodName === null) {
63-
throw new EncoderNodeChildException(sprintf('Setter method (%s) for class "%s" does not exist', $this->getId(), get_class($target)));
61+
throw new EncoderNodeChildException(sprintf('Setter method (%s) for class "%s" does not exist', $this->getChildNodeName(), get_class($target)));
6462
}
6563
else if (method_exists($target, $methodName)) {
6664
foreach ($values as $value) {

src/PE/nodes/EncoderNodeChildren.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function __construct() {
1818
* @return false|EncoderNodeChild Return child object if succeeded. Returns false if it failed
1919
*/
2020
public function addChild(EncoderNodeChild $child) {
21-
$nodeName = $child->getNodeName();
21+
$nodeName = $child->getChildNodeName();
2222
if ($this->childExists($nodeName)) {
2323
return false;
2424
}

src/PE/nodes/EncoderNodeVariable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public function getName() {
266266
public function getSetterMethod() {
267267
if (($result = self::_cache(__FUNCTION__)) === false) {
268268
$result = parent::getSetterMethod();
269-
if ($result === null) {
269+
if ($result === null && $this->getName()) {
270270
$result = 'set' . $this->camelCased($this->getName());
271271
}
272272
self::_cache(__FUNCTION__, $result);
@@ -282,7 +282,7 @@ public function getSetterMethod() {
282282
public function getGetterMethod() {
283283
if (($result = self::_cache(__FUNCTION__)) === false) {
284284
$result = parent::getGetterMethod();
285-
if ($result === null) {
285+
if ($result === null && $this->getName()) {
286286
$result = 'get' . $this->camelCased($this->getName());
287287
}
288288
self::_cache(__FUNCTION__, $result);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace PE\Tests\Nodes;
4+
use PE\Nodes\EncoderNodeChild;
5+
use PE\Tests\Samples;
6+
7+
class EncoderNodeChildTest extends Samples {
8+
9+
const DEFAULT_NODE_NAME = 'children';
10+
const DEFAULT_CHILD_OPTIONS = null;
11+
12+
protected function setUp() {
13+
parent::setUp();
14+
$this->_peApp = $this->nodeChild();
15+
}
16+
17+
/**
18+
* @param string $nodeName
19+
* @param $options
20+
* @return EncoderNodeChild
21+
*/
22+
protected function nodeChild($nodeName = self::DEFAULT_NODE_NAME, $options = self::DEFAULT_CHILD_OPTIONS) {
23+
return new EncoderNodeChild($nodeName, $options);
24+
}
25+
26+
public function testConstructor() {
27+
$nodeChild = $this->nodeChild();
28+
$this->assertNotNull($nodeChild);
29+
$this->assertTrue($nodeChild instanceof EncoderNodeChild);
30+
$this->assertEquals(self::DEFAULT_NODE_NAME, $nodeChild->getChildNodeName());
31+
}
32+
33+
public function testSetChildNodeName() {
34+
$nodeChild = $this->nodeChild();
35+
$nodeChild->setChildNodeName('name');
36+
$this->assertEquals('name', $nodeChild->getChildNodeName());
37+
}
38+
39+
public function testSetChildNodeNameWithEmptyString() {
40+
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeChildException', 'Node name cannot be null or empty');
41+
$nodeChild = $this->nodeChild();
42+
$nodeChild->setChildNodeName('');
43+
}
44+
public function testSetChildNodeNameWithNull() {
45+
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeChildException', 'Node name cannot be null or empty');
46+
$nodeChild = $this->nodeChild();
47+
$nodeChild->setChildNodeName(null);
48+
}
49+
50+
public function testSetAfterChildren() {
51+
$nodeChild = $this->nodeChild();
52+
$this->assertTrue($nodeChild->setAfterChildren());
53+
$this->assertFalse($nodeChild->setAfterChildren(false));
54+
$this->assertTrue($nodeChild->setAfterChildren(true));
55+
}
56+
public function testSetAfterAttributes() {
57+
$nodeChild = $this->nodeChild();
58+
$this->assertTrue($nodeChild->setAfterAttributes());
59+
$this->assertFalse($nodeChild->setAfterAttributes(false));
60+
$this->assertTrue($nodeChild->setAfterAttributes(true));
61+
}
62+
63+
public function testAddChildrenToObject() {
64+
$farmNode = $this->addFarmNode();
65+
$farm = $this->getFarm(false, false);
66+
$house = $this->getBuildingHouse();
67+
$farmNode->addChildrenToObject('buildings', $farm, array(
68+
$house
69+
));
70+
$this->assertEquals(array($house), $farm->getBuildings());
71+
}
72+
public function testAddChildrenToObjectWithoutMethod() {
73+
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeChildException', 'Setter method (' . self::DEFAULT_NODE_NAME . ') for class "PE\Samples\General\Thing" does not exist');
74+
$nodeChild = $this->nodeChild();
75+
$nodeChild->addChildrenToObject($this->getThing(), array());
76+
}
77+
public function testAddChildrenToObjectWhenMethodDoesNotExist() {
78+
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeChildException', 'Trying to add children to "PE\Samples\General\Thing" with method "methodName", but this method does not exist');
79+
$nodeChild = $this->nodeChild();
80+
$nodeChild->setSetterMethod('methodName');
81+
$nodeChild->addChildrenToObject($this->getThing(), array());
82+
}
83+
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use PE\Nodes\EncoderNode;
77
use PE\Nodes\EncoderNodeChild;
88
use PE\Nodes\EncoderNodeVariable;
9-
use PE\Nodes\Farm\BuildingNode;
10-
use PE\Nodes\Farm\Buildings\HouseNode;
119
use PE\Tests\Samples;
1210

1311
class EncoderNodeTest extends Samples

tests/PE/Tests/variables/VariableTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function testParseOptionsDefaultSetExists() {
5454

5555
public function testSetSetterMethod() {
5656
$variable = $this->variable();
57+
$this->assertNull($variable->getSetterMethod());
5758
$variable->setSetterMethod('setterName');
5859
$this->assertEquals('setterName', $variable->getSetterMethod());
5960
$variable->setSetterMethod(null);
@@ -68,6 +69,7 @@ public function testSetSetterMethodNoValue() {
6869

6970
public function testSetGetterMethod() {
7071
$variable = $this->variable();
72+
$this->assertNull($variable->getGetterMethod());
7173
$variable->setGetterMethod('getterName');
7274
$this->assertEquals('getterName', $variable->getGetterMethod());
7375
$variable->setGetterMethod(null);

0 commit comments

Comments
 (0)