Skip to content

Commit 7b2734c

Browse files
committed
Full coverage!
1 parent 9d2fb08 commit 7b2734c

File tree

3 files changed

+148
-59
lines changed

3 files changed

+148
-59
lines changed

tests/PE/Tests/Nodes/EncoderNodeVariableTest.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,40 +142,4 @@ public function testObjectGetter() {
142142
$this->assertTrue($variable->hasObjectGetter());
143143
$this->assertEquals($variable, $objectGetter->getVariable());
144144
}
145-
146-
public function testProcessValue() {
147-
$variable = $this->variable();
148-
$objectSetter = $variable->getObjectSetter();
149-
$this->assertEquals('test', $objectSetter->processValue('test'));
150-
151-
$variable->setType(EncoderNodeVariable::TYPE_BOOL);
152-
$this->assertEquals(true, $objectSetter->processValue(1));
153-
$this->assertEquals(true, $objectSetter->processValue('1'));
154-
$this->assertEquals(true, $objectSetter->processValue('true'));
155-
$this->assertEquals(false, $objectSetter->processValue(0));
156-
$this->assertEquals(false, $objectSetter->processValue('0'));
157-
$this->assertEquals(false, $objectSetter->processValue('false'));
158-
$this->assertEquals(false, $objectSetter->processValue('abc'));
159-
160-
$variable->setType(EncoderNodeVariable::TYPE_STRING);
161-
$this->assertEquals('1', $objectSetter->processValue(1));
162-
$this->assertEquals('string', $objectSetter->processValue('string'));
163-
164-
$variable->setType(EncoderNodeVariable::TYPE_ARRAY);
165-
$this->assertEquals(array(), $objectSetter->processValue(json_encode(array())));
166-
$this->assertEquals(array('hello' => 'world'), $objectSetter->processValue(json_encode(array('hello' => 'world'))));
167-
}
168-
169-
public function testProcessValueArrayException() {
170-
$this->setExpectedException('PE\\Exceptions\\VariableTypeException', 'The set data type is array but the value cannot be processed');
171-
$variable = $this->variable();
172-
$variable->setType(EncoderNodeVariable::TYPE_ARRAY);
173-
$variable->getObjectSetter()->processValue(array());
174-
}
175-
public function testProcessValueUnknownTypeException() {
176-
$this->setExpectedException('PE\\Exceptions\\VariableTypeException', 'Can\'t process value "string" because the data type "unknown" isn\'t recognized.');
177-
$variable = $this->variable();
178-
$variable->setType('unknown');
179-
$variable->getObjectSetter()->processValue('string');
180-
}
181145
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace PE\Tests\Variables\Types;
4+
5+
use PE\Nodes\EncoderNodeVariable;
6+
use PE\Tests\Samples;
7+
use PE\Variables\Types\ObjectGetter;
8+
9+
class ObjectGetterTest extends Samples
10+
{
11+
12+
protected function setUp()
13+
{
14+
parent::setUp();
15+
$this->_peApp = new ObjectGetter();
16+
}
17+
18+
/**
19+
* @return ObjectGetter
20+
*/
21+
protected function objectGetter() {
22+
return $this->_peApp;
23+
}
24+
25+
public function testConstructor()
26+
{
27+
$setter = new ObjectGetter('test');
28+
$this->assertNotNull($setter);
29+
$this->assertTrue($setter instanceof ObjectGetter);
30+
}
31+
32+
public function testGetMethod() {
33+
$objectSetter = new ObjectGetter('method');
34+
$this->assertEquals('method', $objectSetter->getMethod());
35+
36+
$objectSetter = new ObjectGetter();
37+
$objectSetter->setVariable(new EncoderNodeVariable('variableId'));
38+
$this->assertEquals('getVariableId', $objectSetter->getMethod());
39+
}
40+
41+
public function testEncodeWithoutVariableGetterMethod() {
42+
$this->setExpectedException('\\PE\\Exceptions\\VariableTypeException', 'Method "getNonExistent" does not exist for class "PE\Tests\Variables\Types\ObjectGetterTestObject" does not exist');
43+
44+
$objectGetter = $this->objectGetter();
45+
$objectGetter->setVariable(new EncoderNodeVariable('non-existent'));
46+
$objectGetter->apply(new ObjectGetterTestObject());
47+
}
48+
49+
public function testMustBeUnique() {
50+
$objectGetter = $this->objectGetter();
51+
52+
$this->assertFalse($objectGetter->mustBeUnique());
53+
54+
$this->assertFalse($objectGetter->mustBeUnique(false));
55+
$this->assertFalse($objectGetter->mustBeUnique());
56+
$this->assertTrue($objectGetter->mustBeUnique(true));
57+
$this->assertTrue($objectGetter->mustBeUnique());
58+
}
59+
60+
public function testAlwaysExecute() {
61+
$objectGetter = $this->objectGetter();
62+
63+
$this->assertFalse($objectGetter->alwaysExecute());
64+
65+
$this->assertFalse($objectGetter->alwaysExecute(false));
66+
$this->assertFalse($objectGetter->alwaysExecute());
67+
$this->assertTrue($objectGetter->alwaysExecute(true));
68+
$this->assertTrue($objectGetter->alwaysExecute());
69+
}
70+
}
71+
72+
class ObjectGetterTestObject {
73+
74+
}
75+
?>

tests/PE/Tests/Variables/Types/ObjectSetterTest.php

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,89 @@ protected function setUp()
1919
/**
2020
* @return ObjectSetter
2121
*/
22-
protected function objectSetter()
23-
{
22+
protected function objectSetter() {
2423
return $this->_peApp;
2524
}
2625

26+
protected function objectSetterWithVariable() {
27+
$objectSetter = $this->objectSetter();
28+
$objectSetter->setVariable(new EncoderNodeVariable('var'));
29+
return $objectSetter;
30+
}
31+
2732
public function testConstructor()
2833
{
2934
$setter = new ObjectSetter('test');
3035
$this->assertNotNull($setter);
3136
$this->assertTrue($setter instanceof ObjectSetter);
3237
}
3338

39+
public function testGetMethod() {
40+
$objectSetter = new ObjectSetter('method');
41+
$this->assertEquals('method', $objectSetter->getMethod());
42+
43+
$objectSetter = new ObjectSetter();
44+
$objectSetter->setVariable(new EncoderNodeVariable('variableId'));
45+
$this->assertEquals('setVariableId', $objectSetter->getMethod());
46+
}
47+
48+
public function testEncodeWithoutVariableGetterMethod() {
49+
$this->setExpectedException('\\PE\\Exceptions\\VariableTypeException', 'Method "setNonExistent" does not exist for class "PE\Tests\Variables\Types\ObjectSetterTestObject');
50+
51+
$objectSetter = $this->objectSetter();
52+
$objectSetter->setVariable(new EncoderNodeVariable('non-existent'));
53+
$objectSetter->apply(new ObjectSetterTestObject(), 'value');
54+
}
55+
56+
public function testApplyToSetterObjectWithSetterMethod() {
57+
$node = $this->getEncoderNodeVariableApplyToSetterNode();
58+
59+
$object = $this->getEncoderNodeVariableApplyToSetter();
60+
$collection = $node->getVariableCollection();
61+
$var = $collection->getVariableById('var');
62+
63+
$this->assertTrue($var->getObjectSetter()->apply($object, 'test'));
64+
$this->assertEquals('test', $object->getVar());
65+
}
66+
67+
public function testProcessValue() {
68+
$objectSetter = $this->objectSetterWithVariable();
69+
$variable = $objectSetter->getVariable();
70+
$this->assertEquals('test', $objectSetter->processValue('test'));
71+
72+
$variable->setType(EncoderNodeVariable::TYPE_BOOL);
73+
$this->assertEquals(true, $objectSetter->processValue(1));
74+
$this->assertEquals(true, $objectSetter->processValue('1'));
75+
$this->assertEquals(true, $objectSetter->processValue('true'));
76+
$this->assertEquals(false, $objectSetter->processValue(0));
77+
$this->assertEquals(false, $objectSetter->processValue('0'));
78+
$this->assertEquals(false, $objectSetter->processValue('false'));
79+
$this->assertEquals(false, $objectSetter->processValue('abc'));
80+
81+
$variable->setType(EncoderNodeVariable::TYPE_STRING);
82+
$this->assertEquals('1', $objectSetter->processValue(1));
83+
$this->assertEquals('string', $objectSetter->processValue('string'));
84+
85+
$variable->setType(EncoderNodeVariable::TYPE_ARRAY);
86+
$this->assertEquals(array(), $objectSetter->processValue(json_encode(array())));
87+
$this->assertEquals(array('hello' => 'world'), $objectSetter->processValue(json_encode(array('hello' => 'world'))));
88+
}
89+
90+
public function testProcessValueArrayException() {
91+
$this->setExpectedException('PE\\Exceptions\\VariableTypeException', 'The set data type is array but the value cannot be processed');
92+
$objectSetter = $this->objectSetterWithVariable();
93+
$variable = $objectSetter->getVariable();
94+
$variable->setType(EncoderNodeVariable::TYPE_ARRAY);
95+
$objectSetter->processValue(array());
96+
}
97+
public function testProcessValueUnknownTypeException() {
98+
$this->setExpectedException('PE\\Exceptions\\VariableTypeException', 'Can\'t process value "string" because the data type "unknown" isn\'t recognized.');
99+
$objectSetter = $this->objectSetterWithVariable();
100+
$variable = $objectSetter->getVariable();
101+
$variable->setType('unknown');
102+
$objectSetter->processValue('string');
103+
}
104+
34105
public function testMustBeUnique() {
35106
$objectSetter = $this->objectSetter();
36107

@@ -52,27 +123,6 @@ public function testAlwaysExecute() {
52123
$this->assertTrue($objectSetter->alwaysExecute(true));
53124
$this->assertTrue($objectSetter->alwaysExecute());
54125
}
55-
56-
public function testEncodeWithoutVariableGetterMethod()
57-
{
58-
$this->setExpectedException('\\PE\\Exceptions\\VariableTypeException', 'Method "setNonExistent" does not exist for class "PE\Tests\Variables\Types\ObjectSetterTestObject');
59-
60-
$objectSetter = $this->objectSetter();
61-
$objectSetter->setVariable(new EncoderNodeVariable('non-existent'));
62-
63-
$this->objectSetter()->apply(new ObjectSetterTestObject(), 'value');
64-
}
65-
66-
public function testApplyToSetterObjectWithSetterMethod() {
67-
$node = $this->getEncoderNodeVariableApplyToSetterNode();
68-
69-
$object = $this->getEncoderNodeVariableApplyToSetter();
70-
$collection = $node->getVariableCollection();
71-
$var = $collection->getVariableById('var');
72-
73-
$this->assertTrue($var->getObjectSetter()->apply($object, 'test'));
74-
$this->assertEquals('test', $object->getVar());
75-
}
76126
}
77127

78128
class ObjectSetterTestObject {

0 commit comments

Comments
 (0)