Skip to content

Commit 59dcae2

Browse files
committed
Added a test for optional variables
1 parent 921aa8f commit 59dcae2

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

tests/PE/Nodes/Specials/AccessorMethodActionTypeNodeNode.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ function __construct() {
1616
$this->addVariable(new EncoderNodeVariable('node', array(
1717
'setterAction' => array(
1818
'type' => EncoderNodeVariable::ACTION_TYPE_NODE,
19-
'method' => 'addThingToNode',
19+
'method' => 'addNodeToSpecial',
2020
'variables' => array(ActionVariable::SETTER_NAME)
2121
),
2222
'getterAction' => array(
2323
'type' => EncoderNodeVariable::ACTION_TYPE_NODE,
24-
'method' => 'getThingFromNode',
25-
'variables' => array(ActionVariable::SETTER_NAME)
24+
'method' => 'getNodeFromSpecial',
25+
'variables' => array(ActionVariable::GETTER_NAME)
2626
)
2727
)));
2828
}
2929

30-
public function addThingToNode($data, $setterName) {
30+
public function addNodeToSpecial($data, $setterName) {
3131
$data['special'] = $data[$setterName];
3232
return $data;
3333
}
3434

35-
public function getThingFromNode($data, $getterName) {
35+
public function getNodeFromSpecial($data, $getterName) {
3636
$data['special'] = $data[$getterName] . ' getter';
3737
return $data;
3838
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace PE\Nodes\Specials;
4+
5+
use PE\Nodes\EncoderNode;
6+
use PE\Nodes\EncoderNodeVariable;
7+
8+
class OptionalVariablesNode extends EncoderNode {
9+
10+
function __construct() {
11+
parent::__construct('optional-variables', 'optional-variables', '\\PE\\Samples\\Specials');
12+
13+
$this->addVariable(new EncoderNodeVariable('name'));
14+
$this->addVariable(new EncoderNodeVariable('other-variable'));
15+
}
16+
}

tests/PE/Nodes/Specials/RequiredConstructorVariablesNode.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace PE\Nodes\Specials;
44

5-
use PE\Enums\ActionVariable;
65
use PE\Nodes\EncoderNode;
7-
use PE\Nodes\EncoderNodeChild;
86
use PE\Nodes\EncoderNodeVariable;
97

108
class RequiredConstructorVariablesNode extends EncoderNode {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace PE\Samples\Specials;
4+
5+
class OptionalVariables {
6+
7+
private $name;
8+
private $otherVariable;
9+
10+
function __construct() {
11+
}
12+
13+
function setName($value) {
14+
$this->name = $value;
15+
}
16+
function getName() {
17+
return $this->name;
18+
}
19+
20+
function setOtherVariable($value) {
21+
$this->otherVariable = $value;
22+
}
23+
function getOtherVariable() {
24+
return $this->otherVariable;
25+
}
26+
}

tests/PE/Tests/EncoderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PE\Samples\Farm\Buildings\House;
99
use PE\Samples\Loader\ClassLoader;
1010
use PE\Samples\Specials\AddAfterDecodeParent;
11+
use PE\Samples\Specials\OptionalVariables;
1112
use PE\Samples\Specials\RequiredConstructorVariables;
1213
use PE\Samples\Specials\AccessorMethodActionTypeNode;
1314
use PE\Samples\Specials\SingleChild;
@@ -223,6 +224,20 @@ public function testDecodeObjectWithRequiredConstructorVariables() {
223224
$this->assertTrue($obj->getOptional());
224225
}
225226

227+
public function testDecodeObjectWithOptionalVariables() {
228+
$this->addOptionalVariablesNode();
229+
$decoded = $this->encoder()->decode(array(
230+
'optional-variables' => array(
231+
'name' => 'Hello world',
232+
'other-variable' => 'other hello world'
233+
)
234+
));
235+
/** @var OptionalVariables $obj */
236+
$obj = $decoded['optional-variables'];
237+
$this->assertEquals('Hello world', $obj->getName());
238+
$this->assertEquals('other hello world', $obj->getOtherVariable());
239+
}
240+
226241
public function testDecodeWithSetAfterChildrenFalse() {
227242
$this->addAddAfterDecodeNodes();
228243

tests/PE/Tests/Samples.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PE\Nodes\Specials\HasDefaultTypeNode;
2929
use PE\Nodes\Specials\HasDefaultTypeTypeNode;
3030
use PE\Nodes\Specials\NonArrayGetterMethodOnPurposeNode;
31+
use PE\Nodes\Specials\OptionalVariablesNode;
3132
use PE\Nodes\Specials\RequiredConstructorVariablesNode;
3233
use PE\Nodes\Specials\SingleChildNode;
3334
use PE\Samples\Erroneous\EncoderNodeLoader;
@@ -49,6 +50,7 @@
4950
use PE\Samples\Specials\HasDefaultTypeType;
5051
use PE\Samples\Specials\NonArrayGetterMethodOnPurpose;
5152
use PE\Samples\Specials\AccessorMethodActionTypeNode;
53+
use PE\Samples\Specials\OptionalVariables;
5254
use PE\Samples\Specials\SingleChild;
5355

5456
class Samples extends AbstractPETest
@@ -203,6 +205,13 @@ public function addNonArrayGetterMethodNode() {
203205
return EncoderNode::addNode(new NonArrayGetterMethodNode());
204206
}
205207

208+
public function getOptionalVariables() {
209+
return new OptionalVariables();
210+
}
211+
public function addOptionalVariablesNode() {
212+
return EncoderNode::addNode(new OptionalVariablesNode());
213+
}
214+
206215

207216
public function getHasDefaultType() {
208217
return new HasDefaultType();

0 commit comments

Comments
 (0)