TestRelationHelper.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/6/23
  6. * Time: 下午10:21
  7. */
  8. define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
  9. require_once(BASE_ROOT_PATH . '/fooder.php');
  10. require_once(BASE_ROOT_PATH . '/helper/relation_helper.php');
  11. require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
  12. require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
  13. class testRelationHelper extends PHPUnit_Framework_TestCase
  14. {
  15. public static function setUpBeforeClass()
  16. {
  17. Base::run_util();
  18. }
  19. public function testUpContactsA()
  20. {
  21. //36490
  22. $contacts = array('13911129868','13911129869','13911129870','13911129871','13911129867','15618951806','18612396521');
  23. relation_helper::onUpContacts(36490,'13911129867','江海苗',$contacts);
  24. }
  25. public function testUpContactsB()
  26. {
  27. //36490
  28. $contacts = array('13911129867','13911129869','13911129870','13911129872');
  29. relation_helper::onUpContacts(36511,'18612396521','秦梦洁',$contacts);
  30. }
  31. public function testOnLogin()
  32. {
  33. relation_helper::onLogin(36490);
  34. relation_helper::onLogin(36511);
  35. }
  36. public function testUpContactsC()
  37. {
  38. try
  39. {
  40. $contacts = '%5B%22178-0211-8910%22%2C%22182-6227-3057%22%2C%22189-6195-0030%22%5D';
  41. $contacts = session_helper::parse_contacts($contacts);
  42. relation_helper::onUpContacts(36511,'18612396521','秦梦洁',$contacts);
  43. } catch (Exception $ex) {
  44. echo $ex->getMessage();
  45. }
  46. }
  47. public function testMakeType()
  48. {
  49. $reg = '/make_type=(\d+)/i';
  50. $ret = preg_match($reg, '发送红包,红包单号: xxxxxx make_type=6', $arr);
  51. }
  52. public function testFollower()
  53. {
  54. $follower = relation_helper::follower(36490);
  55. if(!empty($follower)) {
  56. //$follower = self::separate_page($follower,$pages);
  57. $members = Model('member')->field('*')->where(array('member_id' => array('in',$follower)))->limit(false)->select();
  58. $desc = array();
  59. foreach ($members as $val) {
  60. $info = new member_info($val);
  61. }
  62. //return self::outsuccess(array('followers' => $follower,'mem_desc' => , 'mobile_page' => mobile_page($pages)));
  63. }
  64. }
  65. public function testDate()
  66. {
  67. $title = strftime("熊猫美妆%m月%d日红包风云榜",1469030400);
  68. }
  69. public function testSort()
  70. {
  71. $mod = Model('member_relation');
  72. $item = $mod->field('*')->where(array('member_mobile' => '13911129867'))->find();
  73. $mobiles = unserialize($item['unbuild_mobiles']);
  74. // perfor_start();
  75. // for($i = 0; $i < 10000; ++$i) {
  76. // sort($mobiles);
  77. // }
  78. // perfor_end("testSort","string sort");
  79. foreach ($mobiles as $mobile) {
  80. $iMobiles[] = intval($mobile);
  81. }
  82. perfor_start();
  83. for($i = 0; $i < 10000; ++$i) {
  84. sort($iMobiles);
  85. }
  86. perfor_end("testSort","int sort");
  87. $len = count($iMobiles);
  88. perfor_start();
  89. for($i = 0; $i < 10000; ++$i) {
  90. $pos = $i % $len;
  91. $val = $iMobiles[$pos];
  92. //algorithm::array_erase($iMobiles,$pos);
  93. $pos = algorithm::lower_bonud($iMobiles,$val);
  94. //algorithm::array_insert($iMobiles,$pos,$val);
  95. }
  96. perfor_end("testSort","int sort");
  97. performance_helper::instance()->format_log();
  98. }
  99. public function testArrayInsert()
  100. {
  101. $x = array(1,3,5,9,10);
  102. $pos = algorithm::lower_bonud($x,3);
  103. $posx = algorithm::upper_bound($x,3);
  104. $y = algorithm::array_insert($x,$pos,0);
  105. $pos = algorithm::lower_bonud($x,4);
  106. $posx = algorithm::upper_bound($x,4);
  107. //$find = algorithm_helper::binary_search($x,0);
  108. }
  109. public function testLowerbound()
  110. {
  111. //$x = array(1,3,5,9,10);
  112. $x = array(39625);
  113. $find = algorithm::binary_search($x,0);
  114. $find = algorithm::binary_search($x,39625);
  115. $pos = algorithm::lower_bonud($x,39625);
  116. $pos = algorithm::lower_bonud($x,2);
  117. $pos = algorithm::lower_bonud($x,3);
  118. $pos = algorithm::lower_bonud($x,14);
  119. }
  120. public function testUpper()
  121. {
  122. $x = array(1,3,5,9,10);
  123. $pos = algorithm::upper_bound($x,0);
  124. $pos = algorithm::upper_bound($x,2);
  125. $pos = algorithm::upper_bound($x,3);
  126. $pos = algorithm::upper_bound($x,14);
  127. }
  128. public function testRelation()
  129. {
  130. perfor_start();
  131. for($i = 0; $i < 10000; ++$i) {
  132. $mod = Model('member_relation');
  133. $mod->findByMobile('13911129867');
  134. }
  135. perfor_end("testRelation","10000 findByMobile");
  136. $mod = Model('member_relation');
  137. $item = $mod->findByMobile('13911129867');
  138. perfor_start();
  139. for($i = 0; $i < 10000; ++$i) {
  140. unserialize($item['unbuild_mobiles']);
  141. }
  142. perfor_end("testRelation","10000 unserialize");
  143. perfor_start();
  144. for($i = 0; $i < 10000; ++$i) {
  145. $relation = new \relation\mem_relation(36490);
  146. //$relation->pass_subscribe();
  147. }
  148. perfor_end("testRelation","10000 pass_subscribe");
  149. performance_helper::instance()->format_log();
  150. }
  151. public function testInitInvitors()
  152. {
  153. $path = BASE_DATA_PATH . "/mobile/invitors.txt";
  154. $file = fopen($path,'r');
  155. if(!is_resource($file)) {
  156. Log::record(__METHOD__ . " file is not exist",Log::ERR);
  157. return false;
  158. }
  159. while(!feof($file)) {
  160. $line = fgets($file);
  161. $kv = preg_split('/,/',$line);
  162. if(count($kv) != 2) continue;
  163. $key = trim($kv[0]);
  164. $val = trim($kv[1]);
  165. $this->update_invitor($key,$val);
  166. }
  167. fclose($file);
  168. }
  169. private function update_invitor($invitor,$user)
  170. {
  171. if(empty($invitor)) return false;
  172. $mod = Model('member');
  173. $invitor_info = $mod->where(['member_mobile' => $invitor])->find();
  174. if(empty($invitor_info)) return false;
  175. $member_id = intval($invitor_info['member_id']);
  176. $user_info = $mod->where(['member_mobile' => $user])->find();
  177. if(empty($user_info)) return false;
  178. $inviter_id = intval($user_info['inviter_id']);
  179. if($inviter_id > 0) return false;
  180. $ret = $mod->where(['member_mobile' => $user])->update(['inviter_id' => $member_id]);
  181. if($ret == false) {
  182. Log::record(__METHOD__ . " update {$user} invitor id err",Log::ERR);
  183. }
  184. else {
  185. Log::record(__METHOD__ . " update {$user} invitor id success",Log::DEBUG);
  186. }
  187. }
  188. }