MockDao.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @name eolinker ams open source,eolinker开源版本
  4. * @link https://www.eolinker.com/
  5. * @package eolinker
  6. * @author www.eolinker.com 广州银云信息科技有限公司 ©2015-2018
  7. * eoLinker是目前全球领先、国内最大的在线API接口管理平台,提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能,旨在解决由于前后端分离导致的开发效率低下问题。
  8. * 如在使用的过程中有任何问题,欢迎加入用户讨论群进行反馈,我们将会以最快的速度,最好的服务态度为您解决问题。
  9. *
  10. * eoLinker AMS开源版的开源协议遵循Apache License 2.0,如需获取最新的eolinker开源版以及相关资讯,请访问:https://www.eolinker.com/#/os/download
  11. *
  12. * 官方网站:https://www.eolinker.com/
  13. * 官方博客以及社区:http://blog.eolinker.com/
  14. * 使用教程以及帮助:http://help.eolinker.com/
  15. * 商务合作邮箱:market@eolinker.com
  16. * 用户讨论QQ群:284421832
  17. */
  18. class MockDao
  19. {
  20. /**
  21. * 获取api的成功示例数据
  22. */
  23. public function getSuccessResult(&$project_id, &$api_uri, &$request_type)
  24. {
  25. $db = getDatabase();
  26. $result = $db->prepareExecute("SELECT eo_api.apiSuccessMock FROM eo_api WHERE eo_api.projectID = ? AND eo_api.apiURI = ? AND eo_api.apiRequestType = ? AND eo_api.removed = 0 ORDER BY eo_api.apiUpdateTime DESC;", array(
  27. $project_id,
  28. $api_uri,
  29. $request_type
  30. ));
  31. if (empty($result)) {
  32. $result = $this->getRestfulMock($project_id, $api_uri, $request_type);
  33. if ($result) {
  34. return $result['apiSuccessMock'];
  35. } else
  36. return FALSE;
  37. } else
  38. return $result['apiSuccessMock'];
  39. }
  40. /**
  41. * 获取api的失败数据
  42. */
  43. public function getFailureResult(&$project_id, &$api_uri, &$request_type)
  44. {
  45. $db = getDatabase();
  46. $result = $db->prepareExecute("SELECT eo_api.apiFailureMock FROM eo_api WHERE eo_api.projectID = ? AND eo_api.apiURI = ? AND eo_api.apiRequestType = ? AND eo_api.removed = 0 ORDER BY eo_api.apiUpdateTime DESC;", array(
  47. $project_id,
  48. $api_uri,
  49. $request_type
  50. ));
  51. if (empty($result)) {
  52. $result = $this->getRestfulMock($project_id, $api_uri, $request_type);
  53. if ($result) {
  54. return $result['apiFailureMock'];
  55. } else
  56. return FALSE;
  57. } else
  58. return $result['apiFailureMock'];
  59. }
  60. /**
  61. * 获取高级mock结果
  62. * @param $project_id
  63. * @param $api_uri
  64. * @param $request_type
  65. * @return bool
  66. */
  67. public function getMockResult(&$project_id, &$api_uri, &$request_type)
  68. {
  69. $db = getDatabase();
  70. $result = $db->prepareExecute('SELECT eo_api.mockResult FROM eo_api WHERE eo_api.projectID = ? AND eo_api.apiURI = ? AND eo_api.apiRequestType = ? AND eo_api.removed = 0 ORDER BY eo_api.apiUpdateTime DESC;', array(
  71. $project_id,
  72. $api_uri,
  73. $request_type
  74. ));
  75. if (empty($result)) {
  76. $result = $this->getRestfulMock($project_id, $api_uri, $request_type);
  77. if ($result) {
  78. return $result['mockResult'];
  79. } else {
  80. return FALSE;
  81. }
  82. } else {
  83. return $result['mockResult'];
  84. }
  85. }
  86. /**
  87. * 获取restful的mock数据
  88. */
  89. public function getRestfulMock(&$project_id, &$api_uri, &$request_type)
  90. {
  91. $db = getDatabase();
  92. $result = $db->prepareExecuteAll('SELECT eo_api.apiURI,eo_api.apiID,eo_api.apiSuccessMock,eo_api.apiFailureMock,eo_api.mockResult FROM eo_api WHERE eo_api.projectID = ? AND eo_api.removed = 0 AND eo_api.apiRequestType = ? ORDER BY eo_api.apiUpdateTime DESC;', array(
  93. $project_id,
  94. $request_type
  95. ));
  96. if (empty($result)) {
  97. return FALSE;
  98. } else {
  99. foreach ($result as $param) {
  100. $msg = preg_replace('/\{[^\/]+\}/', '[^/]+', $param['apiURI']);
  101. $msg = str_replace("amp;", "", $msg);
  102. $msg = preg_replace('/:[^\/]+/', '[^/]+', $msg);
  103. $msg = preg_replace('/\//', '\/', $msg);
  104. $msg = preg_replace("/\?/", '\?', $msg);
  105. $msg = '/^' . $msg . '$/';
  106. $api_uri = str_replace("amp;", "", $api_uri);
  107. if (preg_match($msg, $api_uri)) {
  108. return $param;
  109. }
  110. }
  111. return FALSE;
  112. }
  113. }
  114. }