Skip to content

Commit 9d2fb08

Browse files
committed
Added coverage for everything but the variable types
1 parent 0efb5c6 commit 9d2fb08

13 files changed

+276
-159
lines changed

src/PE/Encoder.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,6 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
202202

203203
$objectSetter = $objectSetterVariable->getObjectSetter();
204204
$processedRequiredValue = $objectSetter->processValue($requiredValue);
205-
if ($processedRequiredValue === null) {
206-
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));
207-
}
208205

209206
$requiredVariableValues[$processedVariable] = $processedRequiredValue;
210207
unset($nodeDataItem[$processedVariable]);
@@ -276,7 +273,7 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
276273

277274
$nodeIndex++;
278275
}
279-
$proxyNode->variablesAreValid($decodedChildren, true);
276+
$variableCollection->objectVariablesAreValidWithData($decodedChildren, true);
280277

281278
if ($isSingleNode) {
282279
return $objects[0];
@@ -344,14 +341,7 @@ protected function _encode($object, EncoderNode $node, EncoderOptions $options,
344341

345342
$optionNodeIndex = $node->getNodeName() . '[' . $childObjectIterationIndex . ']';
346343

347-
// get all the variables values from the object
348344
$attributesRaw = array();
349-
foreach ($objectGetterVariables as $objectGetterVariable) {
350-
$variableId = $objectGetterVariable->getId();
351-
352-
$objectGetter = $objectGetterVariable->getObjectGetter();
353-
$attributesRaw[$variableId] = $objectGetter->apply($object);
354-
}
355345

356346
$postNodeStaticOptions = array(
357347
NodeAccessor::VARIABLE_NODE => $node,
@@ -361,6 +351,30 @@ protected function _encode($object, EncoderNode $node, EncoderOptions $options,
361351
NodeAccessor::VARIABLE_NODE_ITERATION_INDEX => $nodeIterationIndex,
362352
NodeAccessor::VARIABLE_CHILD_OBJECT_ITERATION_INDEX => $childObjectIterationIndex,
363353
);
354+
355+
$preNodeGetterVariables = $variableCollection->getPreNodeGetterVariables();
356+
foreach ($preNodeGetterVariables as $preNodeGetterVariable) {
357+
$variableId = $preNodeGetterVariable->getId();
358+
$postNodeGetter = $preNodeGetterVariable->getPreNodeGetter();
359+
$actionOptions = array_merge($postNodeStaticOptions, array(
360+
NodeAccessor::VARIABLE_NODE_DATA => $attributesRaw,
361+
NodeAccessor::VARIABLE_NAME => $variableId,
362+
));
363+
if ($newAttributeData = $postNodeGetter->apply($actionOptions)) {
364+
if (is_array($newAttributeData)) {
365+
$attributesRaw = $newAttributeData;
366+
}
367+
}
368+
}
369+
370+
// get all the variables values from the object
371+
foreach ($objectGetterVariables as $objectGetterVariable) {
372+
$variableId = $objectGetterVariable->getId();
373+
374+
$objectGetter = $objectGetterVariable->getObjectGetter();
375+
$attributesRaw[$variableId] = $objectGetter->apply($object);
376+
}
377+
364378
$postNodeGetterVariables = $variableCollection->getPostNodeGetterVariables();
365379
foreach ($postNodeGetterVariables as $postNodeGetterVariable) {
366380
$variableId = $postNodeGetterVariable->getId();

src/PE/Nodes/EncoderNode.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -498,29 +498,6 @@ public function getVariableCollection() {
498498
return $this->variables;
499499
}
500500

501-
/**
502-
* @param array $nodeDataArray
503-
* @param bool $throwErrorIfFails Set to true if you want it to throw an error if it fails
504-
* @return bool Returns true if all requirements are met
505-
*
506-
* @see EncoderNodeVariable::variablesAreValidWithData()
507-
*/
508-
public function variablesAreValid($nodeDataArray, $throwErrorIfFails = false) {
509-
return $this->variables->objectVariablesAreValidWithData($nodeDataArray, $throwErrorIfFails);
510-
}
511-
512-
/**
513-
* @param NodeAccessor $nodeAccessor NodeAccessor you want to apply to
514-
* @param array $parameters Array of all the information required for the several methods needing it
515-
* @return bool|mixed
516-
*
517-
* @see EncoderNodeVariable::applyToSetter()
518-
*/
519-
public function applyToNode(NodeAccessor $nodeAccessor, $parameters) {
520-
$parameters[NodeAccessor::VARIABLE_NODE] = $this;
521-
return $nodeAccessor->apply($parameters);
522-
}
523-
524501

525502

526503

src/PE/Nodes/EncoderNodeChild.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class EncoderNodeChild {
2323
*/
2424
private $getter;
2525

26+
/**
27+
* @var bool
28+
*/
2629
private $isArray = true;
2730

2831
function __construct($nodeName, NodeChildSetter $setter = null, NodeChildGetter $getter = null) {
@@ -68,10 +71,13 @@ public function getGetter() {
6871
return $this->getter;
6972
}
7073

71-
// @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?
74+
/**
75+
* @param null $bool
76+
* @return bool
77+
*/
7278
public function isArray($bool = null) {
7379
if ($bool !== null) {
74-
$this->isArray = $bool;
80+
$this->isArray = (bool) $bool;
7581
}
7682
return $this->isArray;
7783
}

src/PE/Nodes/EncoderNodeVariable.php

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -277,66 +277,4 @@ public function hasObjectGetter() {
277277
public function getObjectGetter() {
278278
return $this->objectGetter;
279279
}
280-
281-
282-
283-
/**
284-
* Call the setter action of the node. The setter action type must be set to "node", otherwise it will not execute.
285-
*
286-
* @param EncoderNode $node The EncoderNode of which you wish to call the setter action from
287-
* @param array $options All the available options
288-
* @return mixed|null Returns null if setter action type is not "node". Returns an array with the possibly altered node data
289-
*/
290-
/*public function callNodeSetterAction(EncoderNode $node, $options) {
291-
if ($this->getSetterActionType() != self::ACTION_TYPE_NODE) {
292-
return null;
293-
}
294-
$required = array(
295-
self::ACTION_VARIABLE_SETTER_NODE_DATA
296-
);
297-
$variables = $this->_setupNodeActionVariables($this->getSetterAction(), $required, $options);
298-
return $this->_callNodeAction($node, $this->getSetterActionMethod(), $variables);
299-
}*/
300-
301-
/**
302-
* Call the getter action of the node. The getter action type must be set to "node", otherwise it will not execute.
303-
*
304-
* @param EncoderNode $node The EncoderNode of which you wish to call the getter action from
305-
* @param array $options All the available options
306-
* @return mixed|null Returns null if getter action type is not "node". Returns an array with the possibly altered node data
307-
*/
308-
/*public function callNodeGetterAction(EncoderNode $node, $options) {
309-
if ($this->getGetterActionType() != self::ACTION_TYPE_NODE) {
310-
return null;
311-
}
312-
$required = array(
313-
self::ACTION_VARIABLE_GETTER_NODE_DATA
314-
);
315-
$variables = $this->_setupNodeActionVariables($this->getGetterAction(), $required, $options);
316-
return $this->_callNodeAction($node, $this->getGetterActionMethod(), $variables);
317-
}*/
318-
319-
/**
320-
* Prepares the variables so they can be used
321-
*
322-
* @param array $action Action object
323-
* @param array $required Will be used to determine if options are missing or not
324-
* @param array $options All available options. If options are missing an error will be thrown
325-
* @return array The prepared array of variables
326-
*/
327-
/*protected function _setupNodeActionVariables($action, $required, $options) {
328-
329-
$variablesNames = array_merge($required, isset($action['variables']) ? $action['variables'] : array());
330-
331-
$variables = array();
332-
foreach ($variablesNames as $variableName) {
333-
if (array_key_exists($variableName, $options)) {
334-
$variables[] = $options[$variableName];
335-
}
336-
else {
337-
throw new EncoderNodeVariableException(sprintf('Action variable "%s" is not known', $variableName));
338-
}
339-
}
340-
return $variables;
341-
}*/
342280
}

src/PE/Nodes/EncoderNodeVariableCollection.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PE\Nodes;
44

55
use PE\Exceptions\EncoderNodeVariableCollectionException;
6-
use PE\Exceptions\EncoderNodeVariableException;
76
use PE\Variables\Types\NodeAccessor;
87
use PE\Variables\Types\ObjectAccessor;
98

@@ -28,10 +27,10 @@ function __construct() {
2827
*/
2928
public function addVariable(EncoderNodeVariable $variable) {
3029
$id = $variable->getId();
31-
if (!$this->variableExists($id)) {
32-
$this->variables[$id] = $variable;
30+
if ($this->variableExists($id)) {
31+
throw new EncoderNodeVariableCollectionException(sprintf('Trying to add a EncoderNodeVariable but a variable with id "%s" already exists', $id));
3332
}
34-
$this->_cacheHardReset();
33+
$this->variables[$id] = $variable;
3534
return $variable;
3635
}
3736

@@ -208,22 +207,6 @@ protected function _cache($method, $parameters = null, $value = false) {
208207
return $this->_cache[$method][$parameters];
209208
}
210209

211-
/**
212-
* @param string $method
213-
* @return bool
214-
*/
215-
protected function _cacheReset($method) {
216-
if (array_key_exists($method, $this->_cache)) {
217-
unset($this->_cache[$method]);
218-
return true;
219-
}
220-
return false;
221-
}
222-
223-
protected function _cacheHardReset() {
224-
$this->_cache = array();
225-
}
226-
227210

228211
/**
229212
* @param $dataArray
@@ -244,7 +227,7 @@ public function objectVariablesAreValidWithData($dataArray, $throwErrorIfFails =
244227
$variableValue = $data[$variableId];
245228
if (array_search($variableValue, $unique[$variableId]) !== false) {
246229
if ($throwErrorIfFails) {
247-
throw new EncoderNodeVariableException(sprintf('Variable "%s" must be unique but value "%s" is given at least twice', $variableId, $variableValue));
230+
throw new EncoderNodeVariableCollectionException(sprintf('Variable "%s" must be unique but value "%s" is given at least twice', $variableId, $variableValue));
248231
}
249232
else {
250233
return false;

tests/PE/Nodes/Specials/VariableTypesNode.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PE\Variables\Types\NodeAccessor;
99
use PE\Variables\Types\PostNodeGetter;
1010
use PE\Variables\Types\PostNodeSetter;
11+
use PE\Variables\Types\PreNodeGetter;
1112
use PE\Variables\Types\PreNodeSetter;
1213

1314
class VariableTypesNode extends EncoderNode {
@@ -30,6 +31,13 @@ function __construct() {
3031
NodeAccessor::VARIABLE_OBJECT,
3132
NodeAccessor::VARIABLE_PARENT
3233
)));
34+
35+
$required->preNodeGetter(new PreNodeGetter('preNodeRequiredGetter', array(
36+
NodeAccessor::VARIABLE_NODE,
37+
NodeAccessor::VARIABLE_NAME,
38+
NodeAccessor::VARIABLE_OBJECT,
39+
NodeAccessor::VARIABLE_PARENT
40+
)));
3341
$required->postNodeGetter(new PostNodeGetter('postNodeRequiredGetter', array(
3442
NodeAccessor::VARIABLE_NODE,
3543
NodeAccessor::VARIABLE_NAME,
@@ -38,6 +46,7 @@ function __construct() {
3846
NodeAccessor::VARIABLE_PARENT
3947
)));
4048

49+
4150
$optional = $this->addVariable(new EncoderNodeVariable('optional'));
4251
$optional->preNodeSetter(new PreNodeSetter('preNodeOptionalSetter', array(
4352
NodeAccessor::VARIABLE_NODE,
@@ -52,6 +61,13 @@ function __construct() {
5261
NodeAccessor::VARIABLE_OBJECT,
5362
NodeAccessor::VARIABLE_PARENT
5463
)));
64+
65+
$optional->preNodeGetter(new PreNodeGetter('preNodeOptionalGetter', array(
66+
NodeAccessor::VARIABLE_NODE,
67+
NodeAccessor::VARIABLE_NAME,
68+
NodeAccessor::VARIABLE_OBJECT,
69+
NodeAccessor::VARIABLE_PARENT
70+
)));
5571
$optional->postNodeGetter(new PostNodeGetter('postNodeOptionalGetter', array(
5672
NodeAccessor::VARIABLE_NODE,
5773
NodeAccessor::VARIABLE_NAME,
@@ -69,18 +85,30 @@ public function postNodeRequiredSetter($nodeData, VariableTypesNode $variableTyp
6985
$nodeData['required'] = $nodeData['required'] . ' | setter post';
7086
return $nodeData;
7187
}
88+
89+
public function preNodeRequiredGetter($nodeData, VariableTypesNode $variableTypesNode, $name, VariableTypes $variableTypes, $parent) {
90+
$variableTypes->setOptional($variableTypes->getOptional() . ' | required pre');
91+
$nodeData['pre-required'] = 'getter pre';
92+
return $nodeData;
93+
}
7294
public function postNodeRequiredGetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
7395
$nodeData['required'] = $nodeData['required'] . ' | getter post';
7496
return $nodeData;
7597
}
7698

7799

78100
public function preNodeOptionalSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, $parent) {
79-
$nodeData['optional'] = $nodeData['optional'] . ' | setter pre';
101+
$nodeData['optional'] = $nodeData['optional'] . ' | setter pre';
80102
return $nodeData;
81103
}
82104
public function postNodeOptionalSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
83-
$nodeData['optional'] = $nodeData['optional'] . ' | setter post';
105+
$nodeData['optional'] = $nodeData['optional'] . ' | setter post';
106+
return $nodeData;
107+
}
108+
109+
public function preNodeOptionalGetter($nodeData, VariableTypesNode $variableTypesNode, $name, VariableTypes $variableTypes, $parent) {
110+
$variableTypes->setOptional($variableTypes->getOptional() . ' | optional pre');
111+
$nodeData['pre-optional'] = 'getter pre';
84112
return $nodeData;
85113
}
86114
public function postNodeOptionalGetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {

tests/PE/Tests/EncoderTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,14 @@ public function testDecodeObjectAllVariableTypes() {
251251
/** @var VariableTypes $obj */
252252
$obj = $decoded['variable-type'];
253253
$this->assertEquals('Hello world | setter pre', $obj->getRequired());
254-
$this->assertEquals('Hello other world | setter pre | setter post', $obj->getOptional());
254+
$this->assertEquals('Hello other world | setter pre | setter post', $obj->getOptional());
255255

256256
$encoded = $this->encoder()->encode($obj);
257257
$encodedProcessed = $encoded['processed'];
258258
$this->assertEquals('Hello world | setter pre | getter post', $encodedProcessed['variable-type']['required']);
259-
$this->assertEquals('Hello other world | setter pre | setter post | getter post', $encodedProcessed['variable-type']['optional']);
259+
$this->assertEquals('getter pre', $encodedProcessed['variable-type']['pre-required']);
260+
$this->assertEquals('Hello other world | setter pre | setter post | required pre | optional pre | getter post', $encodedProcessed['variable-type']['optional']);
261+
$this->assertEquals('getter pre', $encodedProcessed['variable-type']['pre-optional']);
260262
}
261263

262264
public function testDecodeWithSetAfterChildrenFalse() {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PE\Tests\Nodes\Children;
4+
5+
use PE\Nodes\Children\NodeChildGetter;
6+
use PE\Tests\Samples;
7+
8+
class NodeChildGetterTest extends Samples
9+
{
10+
11+
protected function setUp()
12+
{
13+
parent::setUp();
14+
$this->_peApp = new NodeChildGetter('method');
15+
}
16+
17+
/**
18+
* @return NodeChildGetter
19+
*/
20+
protected function nodeChildSetter()
21+
{
22+
return $this->_peApp;
23+
}
24+
25+
public function testConstructor()
26+
{
27+
$setter = new NodeChildGetter('test');
28+
$this->assertNotNull($setter);
29+
$this->assertTrue($setter instanceof NodeChildGetter);
30+
}
31+
32+
public function testGetMethod() {
33+
$childSetter = $this->nodeChildSetter();
34+
$this->assertEquals('method', $childSetter->getMethod());
35+
}
36+
}
37+
?>

tests/PE/Tests/Nodes/Children/NodeChildSetterTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function testConstructor()
2929
$this->assertTrue($setter instanceof NodeChildSetter);
3030
}
3131

32+
public function testGetMethod() {
33+
$childSetter = $this->nodeChildSetter();
34+
$this->assertEquals('method', $childSetter->getMethod());
35+
}
36+
3237
public function testSetAfterChildren() {
3338
$nodeChildSetter = $this->nodeChildSetter();
3439

0 commit comments

Comments
 (0)