@@ -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
78128class ObjectSetterTestObject {
0 commit comments