/test/setup/tests/lang/VariableTest.php

https://github.com/tokushima/rhaco1 · PHP · 325 lines · 261 code · 64 blank · 0 comment · 3 complexity · 888c74a3a766ae01598fdd99482a4277 MD5 · raw file

  1. <?php
  2. Rhaco::import("lang.Variable");
  3. Rhaco::import("lang.ObjectUtil");
  4. Rhaco::import("util.UnitTest");
  5. Rhaco::import("Dummy1");
  6. Rhaco::import("Dummy2");
  7. Rhaco::import("Dummy3");
  8. Rhaco::import("Dummy4");
  9. Rhaco::import("Dummy5");
  10. class VariableTest extends UnitTest {
  11. function testMixin(){
  12. $obj = ObjectUtil::mixin(new Dummy1(),new Dummy2(),array("calc",'$a,$b','return $a + $b;'));
  13. if($this->assertTrue(is_object($obj),var_export($obj,true))){
  14. $obj->id = 1;
  15. $obj->value = "hoge";
  16. $obj->type = "rhaco";
  17. $this->assertEquals(">>>>1",$obj->id());
  18. $this->assertEquals("[hoge]",$obj->value());
  19. $this->assertEquals("<rhaco>",$obj->type());
  20. $this->assertEquals(3,$obj->calc(1,2));
  21. $this->assertTrue(Variable::istype("Dummy1",$obj));
  22. $this->assertFalse(Variable::istype("Dummy2",$obj));
  23. $this->assertTrue($obj->istype("Dummy1"));
  24. $this->assertTrue($obj->istype("Dummy2"));
  25. }
  26. $objmix = ObjectUtil::mixin($obj,new Dummy3());
  27. if($this->assertTrue(is_object($objmix),var_export($obj,true))){
  28. $this->assertEquals(">>>>1",$objmix->id());
  29. $this->assertEquals("[hoge]",$objmix->value());
  30. $this->assertEquals("<rhaco>",$objmix->type());
  31. $this->assertEquals(3,$objmix->calc(1,2));
  32. $this->assertEquals("hoge",$objmix->str());
  33. $this->assertTrue(Variable::istype("Dummy1",$objmix));
  34. $this->assertFalse(Variable::istype("Dummy2",$objmix));
  35. $this->assertTrue($objmix->istype("Dummy1"),"Dummy1");
  36. $this->assertTrue($objmix->istype("Dummy2"),"Dummy2");
  37. $this->assertTrue($objmix->istype("Dummy3"),"Dummy3");
  38. $this->assertTrue(method_exists($objmix,"str"));
  39. $this->assertEquals("hoge",$objmix->str());
  40. $objmix->setTest("AAA");
  41. $this->assertEquals("AAA",$objmix->test);
  42. $objmixfunc = ObjectUtil::mixin($objmix,array("setTest",'$a','$this->test = $a."B";'));
  43. $this->assertEquals("AAA",$objmixfunc->test);
  44. $objmixfunc->setTest("CCC");
  45. $this->assertEquals("CCCB",$objmixfunc->test);
  46. }
  47. }
  48. function testMixinAbbr(){
  49. $obj = V::mixin(new Dummy1(),new Dummy2(),array("calc",'$a,$b','return $a + $b;'));
  50. if($this->assertTrue(is_object($obj),var_export($obj,true))){
  51. $obj->id = 1;
  52. $obj->value = "hoge";
  53. $obj->type = "rhaco";
  54. $this->assertEquals(">>>>1",$obj->id());
  55. $this->assertEquals("[hoge]",$obj->value());
  56. $this->assertEquals("<rhaco>",$obj->type());
  57. $this->assertEquals(3,$obj->calc(1,2));
  58. $this->assertTrue(Variable::istype("Dummy1",$obj));
  59. $this->assertFalse(Variable::istype("Dummy2",$obj));
  60. $this->assertTrue($obj->istype("Dummy1"));
  61. $this->assertTrue($obj->istype("Dummy2"));
  62. }
  63. }
  64. function testHashConvObject(){
  65. $obj = new Dummy1();
  66. $obj->id = 100;
  67. $obj->value = "hogehoge";
  68. $this->assertEquals(100,$obj->id);
  69. $this->assertEquals("hogehoge",$obj->value);
  70. $hobj = ObjectUtil::hashConvObject(array("id"=>99,"value"=>"rhaco"),$obj);
  71. $this->assertEquals(-99,$hobj->id);
  72. $this->assertEquals("rhaco",$hobj->value);
  73. $this->assertEquals(-99,$obj->id);
  74. $this->assertEquals("rhaco",$obj->value);
  75. $obj = new Dummy1();
  76. $obj->id = 100;
  77. $obj->value = "hogehoge";
  78. $this->assertEquals(100,$obj->id);
  79. $this->assertEquals("hogehoge",$obj->value);
  80. $hobj = ObjectUtil::hashConvObject(array("id"=>99,"value"=>"rhaco"),$obj,false);
  81. $this->assertEquals(99,$hobj->id);
  82. $this->assertEquals("rhaco",$hobj->value);
  83. $this->assertEquals(99,$obj->id);
  84. $this->assertEquals("rhaco",$obj->value);
  85. $obj = new Dummy5();
  86. $this->assertEquals("",$obj->hoge);
  87. $hobj = ObjectUtil::hashConvObject(array("abc_def"=>"abc"),$obj);
  88. $this->assertEquals("abc",$hobj->hoge);
  89. $this->assertEquals("abc",$obj->hoge);
  90. }
  91. function testHashConvObjectAbbr(){
  92. $obj = new Dummy1();
  93. $obj->id = 100;
  94. $obj->value = "hogehoge";
  95. $this->assertEquals(100,$obj->id);
  96. $this->assertEquals("hogehoge",$obj->value);
  97. $hobj = V::ho(array("id"=>99,"value"=>"rhaco"),$obj);
  98. $this->assertEquals(-99,$hobj->id);
  99. $this->assertEquals("rhaco",$hobj->value);
  100. $this->assertEquals(-99,$obj->id);
  101. $this->assertEquals("rhaco",$obj->value);
  102. $obj = new Dummy1();
  103. $obj->id = 100;
  104. $obj->value = "hogehoge";
  105. $this->assertEquals(100,$obj->id);
  106. $this->assertEquals("hogehoge",$obj->value);
  107. $hobj = V::ho(array("id"=>99,"value"=>"rhaco"),$obj,false);
  108. $this->assertEquals(99,$hobj->id);
  109. $this->assertEquals("rhaco",$hobj->value);
  110. $this->assertEquals(99,$obj->id);
  111. $this->assertEquals("rhaco",$obj->value);
  112. }
  113. function testObjectConvHash(){
  114. $obj = new Dummy1();
  115. $obj->id = 100;
  116. $obj->value = "hogehoge";
  117. $hash = array("id"=>1,"value"=>"abc");
  118. $ohash = ObjectUtil::objectConvHash($obj,$hash);
  119. $this->assertEquals(array("id"=>100,"value"=>"hogehoge","test"=>"TEST"),$ohash);
  120. $this->assertEquals(array("id"=>1,"value"=>"abc"),$hash);
  121. $obj = new Dummy1();
  122. $obj->id = 100;
  123. $obj->value = "hogehoge";
  124. $hash = array("id"=>1,"value"=>"abc");
  125. $ohash = ObjectUtil::objectConvHash($obj,$hash,true);
  126. $this->assertEquals(array("id"=>100,"value"=>"hogehoge","test"=>"TEST","getid"=>100,"getvalue"=>"hogehoge--"),$ohash);
  127. $this->assertEquals(array("id"=>1,"value"=>"abc"),$hash);
  128. }
  129. function testObjectConvHashAbbr(){
  130. $obj = new Dummy1();
  131. $obj->id = 100;
  132. $obj->value = "hogehoge";
  133. $hash = array("id"=>1,"value"=>"abc");
  134. $ohash = V::oh($obj,$hash);
  135. $this->assertEquals(array("id"=>100,"value"=>"hogehoge","test"=>"TEST"),$ohash);
  136. $this->assertEquals(array("id"=>1,"value"=>"abc"),$hash);
  137. $obj = new Dummy1();
  138. $obj->id = 100;
  139. $obj->value = "hogehoge";
  140. $hash = array("id"=>1,"value"=>"abc");
  141. $ohash = V::oh($obj,$hash,true);
  142. $this->assertEquals(array("id"=>100,"value"=>"hogehoge","test"=>"TEST","getid"=>100,"getvalue"=>"hogehoge--"),$ohash);
  143. $this->assertEquals(array("id"=>1,"value"=>"abc"),$hash);
  144. }
  145. function testCopyProperties(){
  146. $obj1 = new Dummy1();
  147. $obj1->id = 100;
  148. $obj1->value = "hogehoge";
  149. $obj2 = new Dummy2();
  150. $obj2->id = 400;
  151. $obj2->type = "text";
  152. $this->assertEquals(400,$obj2->id);
  153. $this->assertEquals("text",$obj2->type);
  154. $obj = Variable::copyProperties($obj1,$obj2);
  155. $this->assertEquals(100,$obj1->id);
  156. $this->assertEquals("hogehoge",$obj1->value);
  157. $this->assertEquals(400,$obj2->id);
  158. $this->assertEquals("text",$obj2->type);
  159. $this->assertEquals(200,$obj->id);
  160. $this->assertEquals("text",$obj->type);
  161. $obj1 = new Dummy1();
  162. $obj1->id = 100;
  163. $obj1->value = "hogehoge";
  164. $obj2 = new Dummy2();
  165. $obj2->id = 400;
  166. $obj2->type = "text";
  167. $this->assertEquals(400,$obj2->id);
  168. $this->assertEquals("text",$obj2->type);
  169. $obj = Variable::copyProperties($obj1,$obj2,true);
  170. $this->assertEquals(100,$obj1->id);
  171. $this->assertEquals("hogehoge",$obj1->value);
  172. $this->assertEquals(400,$obj2->id);
  173. $this->assertEquals("text",$obj2->type);
  174. $this->assertEquals(100,$obj->id);
  175. $this->assertEquals("text",$obj->type);
  176. }
  177. function testCopyPropertiesAbbr(){
  178. $obj1 = new Dummy1();
  179. $obj1->id = 100;
  180. $obj1->value = "hogehoge";
  181. $obj2 = new Dummy2();
  182. $obj2->id = 400;
  183. $obj2->type = "text";
  184. $this->assertEquals(400,$obj2->id);
  185. $this->assertEquals("text",$obj2->type);
  186. $obj = V::cp($obj1,$obj2);
  187. $this->assertEquals(100,$obj1->id);
  188. $this->assertEquals("hogehoge",$obj1->value);
  189. $this->assertEquals(400,$obj2->id);
  190. $this->assertEquals("text",$obj2->type);
  191. $this->assertEquals(200,$obj->id);
  192. $this->assertEquals("text",$obj->type);
  193. $obj1 = new Dummy1();
  194. $obj1->id = 100;
  195. $obj1->value = "hogehoge";
  196. $obj2 = new Dummy2();
  197. $obj2->id = 400;
  198. $obj2->type = "text";
  199. $this->assertEquals(400,$obj2->id);
  200. $this->assertEquals("text",$obj2->type);
  201. $obj = V::cp($obj1,$obj2,true);
  202. $this->assertEquals(100,$obj1->id);
  203. $this->assertEquals("hogehoge",$obj1->value);
  204. $this->assertEquals(400,$obj2->id);
  205. $this->assertEquals("text",$obj2->type);
  206. $this->assertEquals(100,$obj->id);
  207. $this->assertEquals("text",$obj->type);
  208. }
  209. function testToSimpleTag(){
  210. $obj1 = new Dummy1();
  211. $obj1->id = 100;
  212. $obj1->value = "hogehoge";
  213. $tag = Variable::toSimpleTag("hoge",$obj1);
  214. $this->assertEquals('<hoge><id>100</id><value>hogehoge</value><test>TEST</test><getid>100</getid><getvalue>hogehoge--</getvalue></hoge>',$tag->get());
  215. $tag = Variable::toSimpleTag("hoge","hogehoge");
  216. $this->assertEquals('<hoge>hogehoge</hoge>',$tag->get());
  217. $tag = Variable::toSimpleTag("hoge",array("abc"=>123,"def"=>"AAA"));
  218. $this->assertEquals('<hoge><abc>123</abc><def>AAA</def></hoge>',$tag->get());
  219. $tag = Variable::toSimpleTag("hoge",array("abc"=>123,"def"=>"AAA","ghi"=>array("AAA"=>1,"BBB"=>2)));
  220. $this->assertEquals('<hoge><abc>123</abc><def>AAA</def><ghi><AAA>1</AAA><BBB>2</BBB></ghi></hoge>',$tag->get());
  221. $tag = Variable::toSimpleTag("tag",array("hoge"=>array("data"=>array(1,2,3))));
  222. $this->assertEquals('<tag><hoge><data>1</data><data>2</data><data>3</data></hoge></tag>',$tag->get());
  223. }
  224. function testToJson(){
  225. $this->assertEquals('{"id":0,"value":"","test":"TEST"}',Variable::toJson(new Dummy1()));
  226. $this->assertEquals('{"id":0,"type":""}',Variable::toJson(new Dummy2()));
  227. $this->assertEquals('{"id":0,"value":"","test":"TEST","getid":0,"getvalue":"--"}',Variable::toJson(new Dummy1(),true));
  228. $this->assertEquals('{"id":0,"type":"","getid":-100}',Variable::toJson(new Dummy2(),true));
  229. }
  230. function testToHttpQuery(){
  231. Rhaco::import("Dummy4");
  232. $list = array("A"=>"aaaa",
  233. "B"=>array("Ba"=>1,
  234. "Bb"=>array("Bb1"=>"cccc",
  235. "Bb2"=>array(0=>"dddd")
  236. ),
  237. "Bc"=>new Dummy4(),
  238. "Bd"=>true,
  239. "Be"=>null,
  240. ),
  241. );
  242. $this->assertEquals("rhaco[A]=aaaa&rhaco[B][Ba]=1&rhaco[B][Bb][Bb1]=cccc&rhaco[B][Bb][Bb2][0]=dddd&rhaco[B][Bc][aa]=1&rhaco[B][Bc][bb]=2&rhaco[B][Bd]=true&rhaco[B][Be]=&",
  243. Variable::toHttpQuery($list,"rhaco",true,true));
  244. $this->assertEquals("rhaco[A]=aaaa&rhaco[B][Ba]=1&rhaco[B][Bb][Bb1]=cccc&rhaco[B][Bb][Bb2][0]=dddd&rhaco[B][Bc][aa]=1&rhaco[B][Bc][bb]=2&rhaco[B][Bd]=true&",
  245. Variable::toHttpQuery($list,"rhaco",false,true));
  246. $this->assertEquals("rhaco[A]=aaaa&rhaco[B][Ba]=1&rhaco[B][Bb][Bb1]=cccc&rhaco[B][Bb][Bb2][0]=dddd&rhaco[B][Bc]=&rhaco[B][Bd]=true&rhaco[B][Be]=&",
  247. Variable::toHttpQuery($list,"rhaco"));
  248. $this->assertEquals("rhaco[A]=aaaa&rhaco[B][Ba]=1&rhaco[B][Bb][Bb1]=cccc&rhaco[B][Bb][Bb2][0]=dddd&rhaco[B][Bc]=&rhaco[B][Bd]=true&",
  249. Variable::toHttpQuery($list,"rhaco",false));
  250. }
  251. function testCopy(){
  252. Rhaco::import("Dummy4");
  253. $obj = new Dummy4();
  254. $obj->aa = 5;
  255. $copy = Variable::copy($obj);
  256. $copy->aa = 10;
  257. $this->assertEquals(10,$copy->aa);
  258. $this->assertEquals(5,$obj->aa);
  259. }
  260. }
  261. ?>