testresults.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * The testresults entry point of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package entries
  9. * @version 1
  10. * @link https://www.zentao.net
  11. */
  12. class testresultsEntry extends entry
  13. {
  14. /**
  15. * GET method.
  16. *
  17. * @param int $productID
  18. * @access public
  19. * @return string
  20. */
  21. public function get($caseID = 0)
  22. {
  23. if(!$caseID) return $this->sendError(400, 'Need case id.');
  24. $version = $this->param('version', 0);
  25. $runID = $this->param('runID', 0);
  26. $control = $this->loadController('testtask', 'results');
  27. $control->results($runID, $caseID, $version);
  28. $data = $this->getData();
  29. if(isset($data->status) and $data->status == 'success')
  30. {
  31. $results = array();
  32. foreach($data->data->results as $result)
  33. {
  34. $result->stepResults = array_values((array)$result->stepResults);
  35. $results[] = $result;
  36. }
  37. return $this->send(200, array('results' => $results));
  38. }
  39. if(isset($data->status) and $data->status == 'fail') return $this->sendError(zget($data, 'code', 400), $data->message);
  40. return $this->sendError(400, 'error');
  41. }
  42. /**
  43. * POST method.
  44. *
  45. * @param int $caseID
  46. * @access public
  47. * @return string
  48. */
  49. public function post($caseID = 0)
  50. {
  51. if(!$caseID) $caseID = $this->param('case');
  52. if(!$caseID) return $this->sendError(400, 'Need case id.');
  53. $control = $this->loadController('testtask', 'runCase');
  54. $case = $this->loadModel('testcase')->getByID($caseID);
  55. $taskID = $this->param('testtask', 0);
  56. $version = $this->param('version', $case->version);
  57. list($runID, $steps) = $this->getStepIDList($taskID, $caseID, $version);
  58. $this->setPost('case', $caseID);
  59. $this->setPost('version', $version);
  60. /* Set steps and expects. */
  61. if(isset($this->requestBody->steps))
  62. {
  63. $results = array();
  64. $reals = array();
  65. $count = 0;
  66. foreach($this->requestBody->steps as $index => $step)
  67. {
  68. /* When post data more than case steps, break after take useful data. */
  69. $count ++;
  70. if($count > count($steps)) break;
  71. $stepID = $steps[$index];
  72. $results[$stepID] = $step->result;
  73. $reals[$stepID] = $step->real;
  74. }
  75. $this->setPost('result', $results);
  76. $this->setPost('real', $reals);
  77. }
  78. $control->runCase($runID, $caseID, $version);
  79. $data = $this->getData();
  80. if(isset($data->result) and $data->result == 'fail') return $this->sendError(400, $data->message);
  81. return $this->send(200, array());
  82. }
  83. /**
  84. * Get steps key.
  85. *
  86. * @param int $taskID
  87. * @access public
  88. * @return array
  89. */
  90. public function getStepIDList($taskID, $caseID, $version)
  91. {
  92. if($taskID)
  93. {
  94. $run = $this->loadModel('testtask')->getRunByCase($taskID, $caseID);
  95. $run->case = $this->loadModel('testcase')->getById($caseID, $version);
  96. }
  97. else
  98. {
  99. $run = new stdclass();
  100. $run->id = 0;
  101. $run->case = $this->loadModel('testcase')->getById($caseID, $version);
  102. }
  103. $caseIDList = array();
  104. foreach($run->case->steps as $key => $step)
  105. {
  106. /* Unset step if step is a group. */
  107. if($step->type == 'group')
  108. {
  109. unset($run->case->steps[$key]);
  110. }
  111. $caseIDList[] = $step->id;
  112. }
  113. return array($run->id, $caseIDList);
  114. }
  115. }