Skip to content

Commit 2251508

Browse files
committed
The first unit tests works as expected
1 parent b98e3e8 commit 2251508

File tree

7 files changed

+81
-38
lines changed

7 files changed

+81
-38
lines changed

src/PE/Encoder.php

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use PE\Enums\ActionVariable;
1212
use PE\Exceptions\EncoderException;
1313
use PE\Options\EncoderOptions;
14+
use PE\Variables\Types\NodeAccessor;
15+
use PE\Variables\Types\ObjectAccessor;
1416

1517
/**
1618
* Class Encoder
@@ -141,19 +143,20 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
141143
}
142144

143145
$preNodeStaticOptions = array(
144-
ActionVariable::SETTER_NODE_DATA => $nodeDataItem,
145-
ActionVariable::SETTER_NODE => $type,
146-
ActionVariable::SETTER_PARENT => $parentObject
146+
NodeAccessor::VARIABLE_NODE => $type,
147+
NodeAccessor::VARIABLE_PARENT => $parentObject
147148
);
148149
// call node methods. It can be useful when you want to change the outcome of the node data in the node
149150
// that does not have a certain setter but is used in other ways
150151
$nodeMethodVariables = $variableCollection->getPreNodeSetterVariables();
151-
foreach ($nodeMethodVariables as $variableId => $nodeMethodVariable) {
152-
$preNodeSetter = $nodeMethodVariable->getPreNodeSetter();
152+
foreach ($nodeMethodVariables as $objectSetterVariable) {
153+
$variableId = $objectSetterVariable->getId();
154+
$preNodeSetter = $objectSetterVariable->getPreNodeSetter();
153155
if (isset($nodeDataItem[$variableId]) || $preNodeSetter->alwaysExecute()) {
154156
$setterOptions = array_merge($preNodeStaticOptions, array(
155-
ActionVariable::SETTER_NAME => $variableId,
156-
ActionVariable::SETTER_VALUE => $nodeDataItem[$variableId]
157+
NodeAccessor::VARIABLE_NODE_DATA => $nodeDataItem,
158+
NodeAccessor::VARIABLE_NAME => $variableId,
159+
NodeAccessor::VARIABLE_VALUE => $nodeDataItem[$variableId]
157160
));
158161
if ($newNode = $preNodeSetter->apply($setterOptions)) {
159162
$nodeDataItem = $newNode;
@@ -188,7 +191,8 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
188191
throw new EncoderException(sprintf('Variable "%s" for "%s" does not exist but is required to create an object for node "%s" (Node type: "%s") at index "%s"', $processedVariable, $nodeClassName, $nodeName, $type->getNodeName(), $nodeIndex));
189192
}
190193
$requiredValue = $nodeDataItem[$processedVariable];
191-
$processedRequiredValue = $type->processValue($processedVariable, $requiredValue);
194+
$objectSetter = $variableCollection->getVariableById($processedVariable)->getObjectSetter();
195+
$processedRequiredValue = $objectSetter->processValue($requiredValue);
192196
if ($processedRequiredValue === null) {
193197
throw new EncoderException(sprintf('Variable "%s" for "%s" cannot process its value (%s). Presumably because the NodeType does not recognize the variable', $processedVariable, $nodeClassName, $requiredValue));
194198
}
@@ -208,16 +212,44 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
208212
$parentNode->addChildrenToObject($nodeName, $parentObject, array($nodeInstance));
209213
}
210214

211-
foreach ($nodeDataItem as $name => $value) {
212-
$type->applyToVariable($name, array(
213-
ActionVariable::SETTER_NODE_DATA => $nodeDataItem,
214-
ActionVariable::SETTER_OBJECT => $nodeInstance,
215-
ActionVariable::SETTER_PARENT => $parentObject,
216-
ActionVariable::SETTER_NAME => $name,
217-
ActionVariable::SETTER_VALUE => $value
218-
));
215+
$objectStaticOptions = array_merge($preNodeStaticOptions, array(
216+
ObjectAccessor::VARIABLE_NODE_DATA => $nodeDataItem,
217+
ObjectAccessor::VARIABLE_OBJECT => $nodeInstance,
218+
));
219+
220+
$objectSetterVariables = $variableCollection->getObjectSetterVariables();
221+
foreach ($objectSetterVariables as $objectSetterVariable) {
222+
$variableId = $objectSetterVariable->getId();
223+
$variableIsset = isset($nodeDataItem[$variableId]);
224+
$objectSetter = $objectSetterVariable->getObjectSetter();
225+
if ($variableIsset || $objectSetter->alwaysExecute()) {
226+
$objectSetter->apply($nodeInstance, $variableIsset ? $nodeDataItem[$variableId] : null);
227+
}
219228
}
220229

230+
231+
/*$setterOptionsStatic = array(
232+
ActionVariable::SETTER_NODE_DATA => $nodeDataItem,
233+
ActionVariable::SETTER_OBJECT => $nodeInstance,
234+
ActionVariable::SETTER_PARENT => $parentObject
235+
);
236+
237+
$nodeMethodVariables = $type->getVariables();
238+
foreach ($nodeMethodVariables as $nodeMethodVariable) {
239+
$variableId = $nodeMethodVariable->getId();
240+
$variableIsset = isset($nodeDataItem[$variableId]);
241+
if (($variableIsset || $nodeMethodVariable->alwaysExecute()) && $nodeMethodVariable->setterIsActive()) {
242+
$setterOptions = array_merge($setterOptionsStatic, array(
243+
ActionVariable::SETTER_NAME => $variableId,
244+
ActionVariable::SETTER_VALUE => $variableIsset ? $nodeDataItem[$variableId] : null
245+
));
246+
$type->applyToVariable($variableId, $setterOptions);
247+
}
248+
}*/
249+
250+
251+
252+
221253
if (!$addAfterDecode && $addAfterAttributes) {
222254
$parentNode->addChildrenToObject($nodeName, $parentObject, array($nodeInstance));
223255
}

src/PE/Variables/Types/NodeAccessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public function apply($options) {
5959

6060
if (!count($parameters)) {
6161
// if there are no custom parameters at least send the value of the variable
62-
$parameters = array(self::VARIABLE_VALUE);
62+
$parameters = array(NodeAccessor::VARIABLE_VALUE);
6363
}
6464

6565
// prepend the required node data as the first variable
66-
array_unshift($parameters, self::VARIABLE_NODE_DATA);
66+
array_unshift($parameters, NodeAccessor::VARIABLE_NODE_DATA);
6767

6868
// using those parameters, call the node action method
69-
return $this->_callNodeAction($parameters[self::VARIABLE_NODE], $this->getMethod(), $this->_gatherAccessorParameters($options, $parameters));
69+
return $this->_callNodeAction($options[NodeAccessor::VARIABLE_NODE], $this->getMethod(), $this->_gatherAccessorParameters($options, $parameters));
7070
}
7171

7272
/**
@@ -81,7 +81,7 @@ public function apply($options) {
8181
protected function _gatherAccessorParameters($options, $parameters) {
8282
$actionVariables = array();
8383
foreach ($parameters as $parameter) {
84-
if (!isset($options[$parameter])) {
84+
if (!array_key_exists($parameter, $options)) {
8585
throw new VariableTypeException(sprintf('Action variable id "%s" is not known', $parameter));
8686
}
8787
array_push($actionVariables, $options[$parameter]);

src/PE/Variables/Types/ObjectAccessor.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ abstract class ObjectAccessor extends VariableType {
1616

1717
private $mustBeUnique;
1818

19+
const VARIABLE_NODE_DATA = 'node_node_data';
20+
const VARIABLE_NODE = 'node_encoder_node';
21+
const VARIABLE_NAME = 'node_name';
22+
const VARIABLE_VALUE = 'node_value';
23+
const VARIABLE_OBJECT = 'node_object';
24+
const VARIABLE_PARENT = 'node_parent';
25+
1926
const ACCESSOR_SETTER = 'setter';
2027
const ACCESSOR_GETTER = 'getter';
2128

src/PE/Variables/Types/ObjectSetter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace PE\Variables\Types;
77

88
use PE\Exceptions\VariableTypeException;
9+
use PE\Nodes\EncoderNodeVariable;
910
use PE\Variables\Variable;
1011

1112
/**
@@ -42,23 +43,23 @@ public function processValue($value) {
4243
$type = $this->getVariable()->getType();
4344
if ($type !== null) {
4445
switch ($type) {
45-
case Variable::TYPE_BOOL :
46+
case EncoderNodeVariable::TYPE_BOOL :
4647
if (is_string($value)) {
4748
return ($value === 'true' || $value === '1');
4849
}
4950
else {
5051
return (bool) $value;
5152
}
5253
break;
53-
case Variable::TYPE_ARRAY :
54+
case EncoderNodeVariable::TYPE_ARRAY :
5455
if (is_string($value)) {
5556
return (array) json_decode($value);
5657
}
5758
else {
5859
throw new VariableTypeException(sprintf('The set data type is array but the value cannot be processed'));
5960
}
6061
break;
61-
case Variable::TYPE_STRING :
62+
case EncoderNodeVariable::TYPE_STRING :
6263
return (string) $value;
6364
break;
6465
default :

src/PE/Variables/Types/VariableType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
namespace PE\Variables\Types;
7-
use PE\Variables\Variable;
7+
use PE\Nodes\EncoderNodeVariable;
88

99
/**
1010
* Class VariableType
@@ -17,7 +17,7 @@ abstract class VariableType {
1717
private $alwaysExecute;
1818

1919
/**
20-
* @var Variable
20+
* @var EncoderNodeVariable
2121
*/
2222
private $variable;
2323

@@ -32,15 +32,15 @@ public function getMethod() {
3232
/**
3333
* Sets the Variable the type is connected to
3434
*
35-
* @param Variable $variable
35+
* @param EncoderNodeVariable $variable
3636
*/
3737
public function setVariable($variable) {
3838
$this->variable = $variable;
3939
}
4040
/**
4141
* Gets the Variable the type is connected to
4242
*
43-
* @return Variable
43+
* @return EncoderNodeVariable
4444
*/
4545
public function getVariable() {
4646
return $this->variable;

tests/PE/Nodes/Specials/VariableTypesNode.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ function __construct() {
4747
}
4848

4949
public function preNodeRequiredSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, $parent) {
50-
pr('pre required');
51-
pr($nodeData);
50+
print_r('pre required');
51+
print_r($nodeData);
5252
return $nodeData;
5353
}
5454
public function postNodeRequiredSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
55-
pr('post required');
55+
print_r('post required');
5656
return $nodeData;
5757
}
5858

5959
public function preNodeOptionalSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, $parent) {
60-
pr('pre optional');
61-
pr($nodeData);
60+
print_r('pre optional');
61+
print_r($nodeData);
6262
return $nodeData;
6363
}
6464
public function postNodeOptionsSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
65-
pr('post optional');
65+
print_r('post optional');
6666
return $nodeData;
6767
}
6868
}

tests/PE/Tests/EncoderTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PE\Samples\Specials\RequiredConstructorVariables;
1313
use PE\Samples\Specials\AccessorMethodActionTypeNode;
1414
use PE\Samples\Specials\SingleChild;
15+
use PE\Samples\Specials\VariableTypes;
1516

1617
class EncoderTest extends Samples {
1718

@@ -241,15 +242,17 @@ public function testDecodeObjectWithOptionalVariables() {
241242
public function testDecodeObjectAllVariableTypes() {
242243
$this->addVariableTypesNode();
243244
$decoded = $this->encoder()->decode(array(
244-
'variable-types' => array(
245+
'variable-type' => array(
245246
'required' => 'Hello world',
246247
'optional' => 'Hello other world'
247248
)
248249
));
249-
/** @var OptionalVariables $obj */
250-
$obj = $decoded['optional-variables'];
251-
$this->assertEquals('Hello world', $obj->getName());
252-
$this->assertEquals('other hello world', $obj->getOtherVariable());
250+
251+
print_r($decoded);
252+
/** @var VariableTypes $obj */
253+
$obj = $decoded['variable-type'];
254+
$this->assertEquals('Hello world', $obj->getRequired());
255+
$this->assertEquals('Hello other world', $obj->getOptional());
253256
}
254257

255258
public function testDecodeWithSetAfterChildrenFalse() {

0 commit comments

Comments
 (0)