Skip to content

Commit b6d98f1

Browse files
committed
Added a few more unit tests for EncoderNode.php
1 parent 10ed3e6 commit b6d98f1

File tree

3 files changed

+161
-19
lines changed

3 files changed

+161
-19
lines changed

src/PE/nodes/EncoderNode.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class EncoderNode {
2525

2626
private $_nodeIsObjectCache;
2727

28-
private $nodeOptions;
29-
3028
/**
3129
* @var EncoderNodeChildren
3230
*/
@@ -56,12 +54,11 @@ function __construct($nodeName, $nodeNameSingle, $classPrepend, $nodeTypeName =
5654
$this->_nodeIsObjectCache = array();
5755

5856
$this->children = new EncoderNodeChildren();
59-
$this->nodeOptions = new Variable();
6057
$this->variables = new EncoderNodeVariableCollection();
6158

6259
$this->setNodeName($nodeName, $nodeNameSingle);
6360

64-
$this->needsObject(true);
61+
$this->setNeedsObject(true);
6562
$this->classPrepend = $classPrepend;
6663
$this->typeName = $nodeTypeName;
6764
}
@@ -294,11 +291,22 @@ public static function clean() {
294291
}
295292

296293

297-
294+
/**
295+
* In name-spaced projects it would be a lot of work to supply the full class names all the time.
296+
* This variable allows for a shortcut that will prepend the repeated part of a class name.
297+
*
298+
* @return string Class name it will prepend
299+
*/
298300
public function classPrepend() {
299301
return $this->classPrepend;
300302
}
301303

304+
/**
305+
* Returns the
306+
*
307+
* @return string
308+
* @see classPrepend() This will get prepended this this value
309+
*/
302310
public function getNodeObjectName() {
303311
return $this->_getNodeIsolatedClassName();
304312
}
@@ -327,11 +335,6 @@ public function nodeIsObject($object) {
327335
$this->_nodeIsObjectCache[$className] = $nodeIsObject;
328336
return $nodeIsObject;
329337
}
330-
public function objectIsNodeObject($object) {
331-
$objectClass = new ReflectionClass($object);
332-
$objectClassShortName = $objectClass->getShortName();
333-
return $objectClassShortName === $this->getNodeObjectName();
334-
}
335338

336339
protected function setNodeName($nodeName, $nodeNameSingle) {
337340
$this->nodeName = $nodeName;
@@ -348,17 +351,13 @@ public function getNodeTypeName() {
348351
return $this->typeName;
349352
}
350353

351-
public function addOptions($options) {
352-
$this->nodeOptions->parseOptions($options);
353-
}
354-
355354
public function getDefaultType() {
356355
return EncoderNode::DEFAULT_TYPE;
357356
}
358-
public function needsObject($bool = null) {
359-
if ($bool !== null && is_bool($bool)) {
360-
$this->needsClass = $bool;
361-
}
357+
protected function setNeedsObject($bool) {
358+
$this->needsClass = $bool;
359+
}
360+
public function needsObject() {
362361
return $this->needsClass;
363362
}
364363

tests/PE/Tests/Samples.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,17 @@ public function addFarmNode() {
118118
}
119119

120120
public function addBuildingNode() {
121+
$node = new BuildingNode();
121122
EncoderNode::addNode(new BuildingNode());
123+
return $node;
124+
}
125+
public function getBuildingHouse() {
126+
return new House();
122127
}
123128
public function addBuildingHouseNode() {
124-
EncoderNode::addNodeType(new HouseNode());
129+
$node = new HouseNode();
130+
EncoderNode::addNodeType($node);
131+
return $node;
125132
}
126133
public function addBuildingGreenhouseNode() {
127134
EncoderNode::addNodeType(new GreenhouseNode());

tests/PE/Tests/nodes/EncodeNodeTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public function testStaticAddNode() {
5656
$nodeTypes = EncoderNode::getNodeTypes(self::DEFAULT_NODE_NAME);
5757
$this->assertArrayHasKey(EncoderNode::DEFAULT_TYPE, $nodeTypes);
5858
$this->assertEquals($nodeTypes[EncoderNode::DEFAULT_TYPE], $node);
59+
60+
$types = EncoderNode::getNodeTypes();
61+
$this->assertCount(1, $types);
62+
$this->assertEquals('default', $node->getNodeTypeName());
5963
}
6064

6165
public function testStaticAddNodeWithNonStringNodeName() {
@@ -94,6 +98,9 @@ public function testStaticGetNodeByObject() {
9498
$things = $this->getThings();
9599
$this->assertEquals($thingsNode, EncoderNode::getNodeByObject($things));
96100
$this->assertNull(EncoderNode::getNodeByObject($this->getThing()));
101+
102+
// from cache
103+
$this->assertEquals($thingsNode, EncoderNode::getNodeByObject($things));
97104
}
98105

99106
public function testStaticGetNodes() {
@@ -117,6 +124,19 @@ public function testStaticNodeExists() {
117124
$this->assertTrue(EncoderNode::nodeExists(self::DEFAULT_NODE_NAME_SINGLE));
118125
$this->assertFalse(EncoderNode::nodeExists('unknown'));
119126
}
127+
128+
public function testStaticAddNodeType() {
129+
$this->addBuildingNode();
130+
$this->addBuildingHouseNode();
131+
132+
$types = EncoderNode::getNodeTypes();
133+
$this->assertCount(2, $types);
134+
$this->assertArrayHasKey('buildings:default', $types);
135+
$this->assertArrayHasKey('buildings:house', $types);
136+
137+
$this->assertEquals('PE\\Nodes\\Farm\\BuildingNode', get_class($types['buildings:default']));
138+
$this->assertEquals('PE\\Nodes\\Farm\\Buildings\HouseNode', get_class($types['buildings:house']));
139+
}
120140
public function testStaticAddNodeTypeTwice() {
121141
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeException', 'Node type with name "buildings" and node type name "house" already exists');
122142
$this->addBuildingNode();
@@ -127,4 +147,120 @@ public function testStaticAddNodeTypeWithoutNodeType() {
127147
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeException', 'The node type you\'re trying to add seems to be a regular node because it has a no type name. Make sure you try to add an EncoderNode with a type name');
128148
EncoderNode::addNodeType($this->nodeType());
129149
}
150+
151+
public function testStaticGetNodeTypeByObject() {
152+
$this->addBuildingNode();
153+
$houseNode = $this->addBuildingHouseNode();
154+
$house = $this->getBuildingHouse();
155+
$this->assertEquals($houseNode, EncoderNode::getNodeTypeByObject($house));
156+
$this->assertNull(EncoderNode::getNodeTypeByObject($this->getThing()));
157+
158+
// from cache
159+
$this->assertEquals($houseNode, EncoderNode::getNodeTypeByObject($house));
160+
}
161+
162+
public function testStaticGetNodeTypesAll() {
163+
$this->addBuildingNode();
164+
$this->addBuildingHouseNode();
165+
166+
$types = EncoderNode::getNodeTypes();
167+
$this->assertCount(2, $types);
168+
$this->assertArrayHasKey('buildings:default', $types);
169+
$this->assertArrayHasKey('buildings:house', $types);
170+
171+
$this->assertEquals('PE\\Nodes\\Farm\\BuildingNode', get_class($types['buildings:default']));
172+
$this->assertEquals('PE\\Nodes\\Farm\\Buildings\HouseNode', get_class($types['buildings:house']));
173+
}
174+
175+
public function testStaticGetNodeTypesOfType() {
176+
$this->addHouseNodes();
177+
178+
$types = EncoderNode::getNodeTypes('buildings');
179+
$this->assertCount(2, $types);
180+
$this->assertArrayHasKey('default', $types);
181+
$this->assertArrayHasKey('house', $types);
182+
183+
$this->assertEquals('PE\\Nodes\\Farm\\BuildingNode', get_class($types['default']));
184+
$this->assertEquals('PE\\Nodes\\Farm\\Buildings\HouseNode', get_class($types['house']));
185+
}
186+
187+
public function testStaticGetNodeType() {
188+
$this->addBuildingNode();
189+
$this->addBuildingHouseNode();
190+
191+
$this->assertEquals('PE\\Nodes\\Farm\\Buildings\HouseNode', get_class(EncoderNode::getNodeType('buildings', 'house')));
192+
$this->assertNull(EncoderNode::getNodeType('buildings', 'unknown'));
193+
$this->assertNull(EncoderNode::getNodeType('unknown', 'unknown'));
194+
}
195+
196+
public function testStaticNodeTypeExists() {
197+
$this->addBuildingNode();
198+
$this->addBuildingHouseNode();
199+
200+
$this->assertTrue(EncoderNode::nodeTypeExists('buildings', 'house'));
201+
$this->assertFalse(EncoderNode::nodeTypeExists('buildings', 'unknown'));
202+
$this->assertFalse(EncoderNode::nodeTypeExists('unknown', 'unknown'));
203+
}
204+
205+
public function testStaticClean() {
206+
$this->addBuildingNode();
207+
$this->addBuildingHouseNode();
208+
209+
$this->assertCount(2, EncoderNode::getNodeTypes());
210+
$this->assertCount(2, EncoderNode::getNodes());
211+
212+
EncoderNode::clean();
213+
214+
$this->assertEmpty(EncoderNode::getNodeTypes());
215+
$this->assertEmpty(EncoderNode::getNodes());
216+
}
217+
218+
219+
220+
221+
public function testClassPrepend() {
222+
$buildingNode = $this->addBuildingNode();
223+
224+
$this->assertEquals('\\PE\\Samples\\Farm', $buildingNode->classPrepend());
225+
}
226+
227+
public function testGetNodeObjectName() {
228+
$buildingNode = $this->addBuildingNode();
229+
230+
$this->assertEquals('Building', $buildingNode->getNodeObjectName());
231+
232+
// get it from cache
233+
$this->assertEquals('Building', $buildingNode->getNodeObjectName());
234+
}
235+
236+
public function testNodeIsObject() {
237+
$this->addBuildingNode();
238+
$houseNode = $this->addBuildingHouseNode();
239+
$house = $this->getBuildingHouse();
240+
241+
$this->assertFalse($houseNode->nodeIsObject($this->getThing()));
242+
$this->assertTrue($houseNode->nodeIsObject($house));
243+
244+
// get from cache
245+
$this->assertTrue($houseNode->nodeIsObject($house));
246+
}
247+
248+
public function testGetNodeName() {
249+
$this->addBuildingNode();
250+
$houseNode = $this->addBuildingHouseNode();
251+
252+
$this->assertEquals('buildings', $houseNode->getNodeName());
253+
}
254+
public function testGetNodeNameSingle() {
255+
$this->addBuildingNode();
256+
$houseNode = $this->addBuildingHouseNode();
257+
258+
$this->assertEquals('building', $houseNode->getNodeNameSingle());
259+
}
260+
public function testGetNodeTypeName() {
261+
$this->addBuildingNode();
262+
$houseNode = $this->addBuildingHouseNode();
263+
264+
$this->assertEquals('house', $houseNode->getNodeTypeName());
265+
}
130266
}

0 commit comments

Comments
 (0)