Skip to content

Commit 51b58e2

Browse files
committed
Added more unit tests to EncoderNode.php
And I've added more documentation to some methods
1 parent dac61ac commit 51b58e2

File tree

7 files changed

+331
-108
lines changed

7 files changed

+331
-108
lines changed

src/PE/nodes/EncoderNode.php

Lines changed: 126 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function __construct($nodeName, $nodeNameSingle, $classPrepend, $nodeTypeName =
8787
* This method is essential for using this library because it will instruct it what to do and how to do it.
8888
*
8989
* @param EncoderNode $node
90-
* @return bool Will return true if the node had been successfully added
90+
* @return EncoderNode Will return the EncoderNode if the node had been successfully added
9191
*/
9292
public static function addNode(EncoderNode $node) {
9393
$nodeName = $node->getNodeName();
@@ -182,7 +182,7 @@ public static function nodeExists($nodeName) {
182182
* node. You can compare it with inheriting functions from a parent class.
183183
*
184184
* @param EncoderNode $nodeType
185-
* @return true Returns true if the node type was successfully added
185+
* @return EncoderNode Returns the EncoderNode if the node type was successfully added
186186
*/
187187
public static function addNodeType(EncoderNode $nodeType) {
188188
if ($nodeType->getNodeTypeName() === null) {
@@ -466,70 +466,112 @@ public function addChildrenToObject($childName, $target, $values) {
466466

467467

468468

469-
public function loadPlugin($pluginName) {
470-
throw new EncoderNodeException('Must be overwritten by subclasses');
469+
/**
470+
* @param EncoderNodeVariable $variable
471+
* @return EncoderNodeVariable
472+
*
473+
* @see EncoderNodeVariable::addNodeVariable()
474+
*/
475+
public function addVariable(EncoderNodeVariable $variable) {
476+
return $this->variables->addNodeVariable($variable);
471477
}
472478

473479
/**
474-
* Retrieve a node type based on its node type name
480+
* @param $variable
481+
* @return EncoderNodeVariable
475482
*
476-
* @param string $nodeTypeName
477-
* @return EncoderNode|null Returns the EncoderNode object for the requested type. Null if if no type is found
483+
* @see EncoderNodeVariable::getVariable()
478484
*/
479-
public function getType($nodeTypeName) {
480-
return EncoderNode::getNodeType($this->getNodeName(), $nodeTypeName);
485+
public function getVariable($variable) {
486+
return $this->variables->getVariable($variable);
481487
}
482488

489+
/**
490+
* @param string $id
491+
* @return null|EncoderNodeVariable
492+
*
493+
* @see EncoderNodeVariable::getVariableById()
494+
*/
495+
public function getVariableById($id) {
496+
return $this->variables->getVariableById($id);
497+
}
483498

484-
protected function _loadObject($object) {
485-
return null;
499+
/**
500+
* @param string $type
501+
* @return EncoderNodeVariable[]
502+
*
503+
* @see EncoderNodeVariable::getVariablesSetterActionByType()
504+
*/
505+
public function getVariablesSetterActionByType($type) {
506+
return $this->variables->getVariablesSetterActionByType($type);
486507
}
487508

488509
/**
489-
* @return string Returns the full class name that will be required for this EncoderNode
510+
* @param string $type
511+
* @return EncoderNodeVariable[]
512+
*
513+
* @see EncoderNodeVariable::getVariablesGetterActionByType()
490514
*/
491-
protected function _objectClassName() {
492-
return $this->classPrepend() . '\\' . $this->_objectFileName();
515+
public function getVariablesGetterActionByType($type) {
516+
return $this->variables->getVariablesGetterActionByType($type);
493517
}
494518

495519
/**
496-
* @return string Returns the file name of the class required for this EncoderNode
520+
* @return EncoderNodeVariable[]
521+
*
522+
* @see EncoderNodeVariable::getAlwaysExecutedVariables()
497523
*/
498-
protected function _objectFileName() {
499-
return $this->_getNodeIsolatedClassName();
524+
public function getAlwaysExecutedVariables() {
525+
return $this->variables->getAlwaysExecutedVariables();
500526
}
501527

502528
/**
503-
* Will load a certain file
529+
* @param bool $order
530+
* @return EncoderNodeVariable[]
504531
*
505-
* @param string|null $object Name of the object. Leave empty if you want to trigger the "_objectFileName" method
506-
* @return mixed
532+
* @see EncoderNodeVariable::getAlwaysExecutedVariables()
533+
*/
534+
public function getVariables($order = true) {
535+
return $this->variables->getVariables($order);
536+
}
537+
538+
/**
539+
* @param string $id
540+
* @return bool
507541
*
508-
* @see _objectFileName()
542+
* @see EncoderNodeVariable::variableExists()
509543
*/
510-
public function loadObject($object = null) {
511-
if ($object === null && $object = $this->_objectFileName()) {
512-
if ($object === null) {
513-
throw new EncoderNodeException('Object cannot be null');
514-
}
515-
}
516-
return $this->_loadObject($object);
544+
public function variableExists($id) {
545+
return $this->variables->variableExists($id);
517546
}
518547

519548
/**
520-
* @return string Returns read-only value of "_objectClassName()"
549+
* @param array $nodeDataArray
550+
* @return bool
521551
*
522-
* @see _objectClassName();
552+
* @see EncoderNodeVariable::variablesAreValidWithData()
523553
*/
524-
public function getObjectClassName() {
525-
return $this->_objectClassName();
554+
public function variablesAreValid($nodeDataArray) {
555+
return $this->variables->variablesAreValidWithData($nodeDataArray);
526556
}
527557

558+
/**
559+
* @param string $name
560+
* @param mixed $value
561+
* @return mixed
562+
*
563+
* @see EncoderNodeVariable::processValue()
564+
*/
565+
public function processValue($name, $value) {
566+
return $this->variables->processValue($name, $value);
567+
}
528568

529569
/**
530-
* @param $name
531-
* @param array $parameters
570+
* @param string $name Variable name you want to apply the parameters to
571+
* @param array $parameters Array of all the information required for the several methods needing it
532572
* @return bool|mixed
573+
*
574+
* @see EncoderNodeVariable::applyToSetter()
533575
*/
534576
public function applyToVariable($name, $parameters) {
535577
$variable = $this->getVariable($name);
@@ -540,77 +582,80 @@ public function applyToVariable($name, $parameters) {
540582
return $variable->applyToSetter($parameters);
541583
}
542584

543-
public function addVariable(EncoderNodeVariable $variable) {
544-
return $this->variables->addNodeVariable($variable);
545-
}
546585

547-
/**
548-
* @param $variable
549-
* @param $options
550-
* @return bool
551-
*/
552-
public function alterVariable($variable, $options) {
553-
return $this->variables->alterVariable($variable, $options);
586+
587+
588+
589+
590+
protected function _loadObject($object) {
591+
throw new EncoderNodeException('Must be overwritten by subclasses');
554592
}
555593

556594
/**
557-
* @param $variable
558-
* @return EncoderNodeVariable
595+
* @return string Returns the full class name that will be required for this EncoderNode
559596
*/
560-
public function getVariable($variable) {
561-
return $this->variables->getVariable($variable);
597+
protected function _objectClassName() {
598+
return $this->classPrepend() . '\\' . $this->_objectFileName();
562599
}
563600

564601
/**
565-
* @param $id
566-
* @return null|EncoderNodeVariable
602+
* @return string Returns the file name of the class required for this EncoderNode
567603
*/
568-
public function getVariableById($id) {
569-
return $this->variables->getVariableById($id);
604+
protected function _objectFileName() {
605+
return $this->_getNodeIsolatedClassName();
570606
}
571607

572608
/**
573-
* @param $type
574-
* @return EncoderNodeVariable[]
609+
* Will load a certain file
610+
*
611+
* @param string|null $object Name of the object. Leave empty if you want to trigger the "_objectFileName" method
612+
* @return mixed
613+
*
614+
* @see _objectFileName()
575615
*/
576-
public function getVariablesSetterActionByType($type) {
577-
return $this->variables->getVariablesSetterActionByType($type);
616+
public function loadObject($object = null) {
617+
if ($object === null) {
618+
$object = $this->_objectFileName();
619+
if ($object === null) {
620+
throw new EncoderNodeException('Object for loading cannot be null');
621+
}
622+
}
623+
return $this->_loadObject($object);
578624
}
579625

580626
/**
581-
* @param $type
582-
* @return EncoderNodeVariable[]
627+
* @return string Returns read-only value of "_objectClassName()"
628+
*
629+
* @see _objectClassName();
583630
*/
584-
public function getVariablesGetterActionByType($type) {
585-
return $this->variables->getVariablesGetterActionByType($type);
631+
public function getObjectClassName() {
632+
return $this->_objectClassName();
586633
}
587634

635+
636+
637+
588638
/**
589-
* @return EncoderNodeVariable[]
639+
* Figure out which node type is going to be used. It is very handy to extend this method from a node because it
640+
* allows for more control over the type next to the default behavior
641+
*
642+
* @param object $parent A variable with the parent object so you can get some information from there next to the
643+
* processed node data
644+
* @param array $nodeData Processed node data
645+
* @return null|string Returns null is no type is found in the node data. Otherwise returns the type extracted from
646+
* the node data
590647
*/
591-
public function getAlwaysExecutedVariables() {
592-
return $this->variables->getAlwaysExecutedVariables();
648+
public function getObjectType($parent, $nodeData) {
649+
return isset($nodeData['type']) ? $nodeData['type'] : null;
593650
}
594651

595652
/**
596-
* @param bool $order
597-
* @return EncoderNodeVariable[]
598-
* @throws \Exception
653+
* Retrieve a node type based on its node type name
654+
*
655+
* @param string $nodeTypeName
656+
* @return EncoderNode|null Returns the EncoderNode object for the requested type. Null if if no type is found
599657
*/
600-
public function getVariables($order = true) {
601-
return $this->variables->getVariables($order);
602-
}
603-
public function variableExists($id) {
604-
return $this->variables->variableExists($id);
605-
}
606-
public function variablesAreValid($nodeDataArray) {
607-
return $this->variables->variablesAreValidWithData($nodeDataArray);
608-
}
609-
public function processValue($name, $value) {
610-
return $this->variables->processValue($name, $value);
611-
}
612-
613-
public function getObjectType($parent, $nodeData) {
614-
return isset($nodeData['type']) ? $nodeData['type'] : null;
658+
public function getType($nodeTypeName) {
659+
return EncoderNode::getNodeType($this->getNodeName(), $nodeTypeName);
615660
}
616661
}

src/PE/nodes/EncoderNodeVariableCollection.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,42 @@ protected function _getVariablesActionByType($type, $actionMethod) {
3333
return $variables;
3434
}
3535

36+
/**
37+
* @param EncoderNodeVariable $variable
38+
* @return EncoderNodeVariable
39+
*/
3640
public function addNodeVariable(EncoderNodeVariable $variable) {
3741
$this->_cachedAlwaysExecutedVariables = null;
3842
$variable = parent::addVariable($variable);
3943
return $variable;
4044
}
45+
46+
/**
47+
* You cannot use this method, use "addNodeVariable" instead
48+
*
49+
* @param Variable $variable
50+
* @return void
51+
*/
4152
public function addVariable(Variable $variable) {
4253
throw new EncoderNodeVariableException('Use "addNodeVariable" to add variables');
4354
}
4455

56+
/**
57+
* @param $dataArray
58+
* @return bool Returns true if all requirements are met
59+
*/
4560
public function variablesAreValidWithData($dataArray) {
4661
$variables = $this->getVariables();
4762
$unique = array();
4863
foreach ($dataArray as $data) {
4964
foreach ($variables as $variable) {
5065
$variableId = $variable->getId();
5166
if ($variableId !== null && array_key_exists($variableId, $data)) {
52-
$variableValue = $data[$variableId];
5367
if ($variable->mustBeUnique()) {
5468
if (!isset($unique[$variableId])) {
5569
$unique[$variableId] = array();
5670
}
71+
$variableValue = $data[$variableId];
5772
if (array_search($variableValue, $unique[$variableId]) !== false) {
5873
throw new EncoderNodeVariableException(sprintf('Variable "%s" must be unique but value "%s" is given at least twice', $variableId, $variableValue));
5974
}
@@ -78,4 +93,14 @@ public function getAlwaysExecutedVariables() {
7893
$this->_cachedAlwaysExecutedVariables = $variables;
7994
return $variables;
8095
}
96+
97+
/**
98+
* Overridden method so the returned data type corresponds with this class
99+
*
100+
* @param bool $order
101+
* @return EncoderNodeVariable[]
102+
*/
103+
public function getVariables($order = true) {
104+
return parent::getVariables($order);
105+
}
81106
}

src/PE/variables/Variable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function getOrder() {
169169
/**
170170
* Processes a value based on the set data type of the variable
171171
* @param $value
172-
* @return bool
172+
* @return mixed
173173
*/
174174
public function processValue($value) {
175175
$type = $this->getType();

0 commit comments

Comments
 (0)