PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/Mage/Testlink/Connector.php

https://github.com/KNXSebastian/taf
PHP | 312 lines | 162 code | 24 blank | 126 comment | 43 complexity | ba1555d274556fc13c57e72683afe37a MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category tests
  22. * @package selenium
  23. * @subpackage Mage_Selenium
  24. * @author Magento Core Team <core@magentocommerce.com>
  25. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  26. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. */
  28. require_once 'class-IXR.php';
  29. class Mage_Testlink_Connector
  30. {
  31. /**
  32. * Default server url. Should be overriden in phpunit.xml
  33. * @var string
  34. */
  35. public static $SERVER_URL = "http://localhost//testlink/lib/api/xmlrpc.php";
  36. /**
  37. * @var IXR_Client
  38. */
  39. private $_client;
  40. /**
  41. * Key used for getting connection to xml-rpc of testlink (all test cases will be signed from the user whoes id is used)
  42. *
  43. * @var string|null
  44. */
  45. public static $devKey = null;
  46. /**
  47. * Map of status codes for sending to testlink
  48. *
  49. * @var array
  50. */
  51. public static $tcaseStatusCode = array(
  52. 'passed' => 'p',
  53. 'blocked' => 'b',
  54. 'failed' => 'f',
  55. 'wrong' => 'w',
  56. 'departed' => 'd'
  57. );
  58. /**
  59. * Constructor
  60. */
  61. public function __construct()
  62. {
  63. $this->_client = new IXR_Client(Mage_Testlink_Connector::$SERVER_URL);
  64. }
  65. /**
  66. * Generates the report for sending to testlink. Sends it to testlink and returns array with the result
  67. *
  68. * @param string $tcaseexternalid
  69. * @param string $tplanid
  70. * @param string $status
  71. * @param string $buildid
  72. * @param string $notes
  73. *
  74. * @return array
  75. */
  76. public function report($tcaseexternalid, $tplanid, $status, $buildid=null, $notes=null)
  77. {
  78. return $this->reportResult($tcaseexternalid, $tplanid, $buildid, null, $status,
  79. $notes, null, null, null, false, false);
  80. }
  81. /**
  82. * Send the result of test execution of the test case
  83. *
  84. * @param string $tcaseexternalid
  85. * @param string $tplanid
  86. * @param string $buildid
  87. * @param string $buildname
  88. * @param string $status
  89. * @param string $notes
  90. * @param string $bugid
  91. * @param string $customfields
  92. * @param string $platformname
  93. * @param bool $overwrite
  94. * @param bool $debug
  95. *
  96. * @return array
  97. */
  98. protected function reportResult($tcaseexternalid=null, $tplanid, $buildid=null, $buildname=null,
  99. $status, $notes=null, $bugid=null, $customfields=null, $platformname=null,
  100. $overwrite=false, $debug=false)
  101. {
  102. $this->_client->debug = $debug;
  103. $data = array();
  104. $data["devKey"] = Mage_Testlink_Connector::$devKey;
  105. $data["testplanid"] = $tplanid;
  106. if (!is_null($bugid)) {
  107. $data["bugid"] = $bugid;
  108. }
  109. if (!is_null($tcaseexternalid)) {
  110. $data["testcaseexternalid"] = $tcaseexternalid;
  111. }
  112. if (!is_null($buildid)) {
  113. $data["buildid"] = $buildid;
  114. } elseif (!is_null($buildname)) {
  115. $data["buildname"] = $buildname;
  116. }
  117. if (!is_null($notes)) {
  118. $data["notes"] = $notes;
  119. }
  120. $data["status"] = $status;
  121. if (!is_null($customfields)) {
  122. $data["customfields"] = $customfields;
  123. }
  124. if (!is_null($platformname)) {
  125. $data["platformname"] = $platformname;
  126. }
  127. if (!is_null($overwrite)) {
  128. $data["overwrite"] = $overwrite;
  129. }
  130. if ($this->_client->query('tl.reportTCResult', $data)) {
  131. $response = $this->_client->getResponse();
  132. } else {
  133. $response = null;
  134. }
  135. return $response;
  136. }
  137. /**
  138. * Performs and action for the executed test case (i.e. sets the status of test case)
  139. *
  140. * @param string $method
  141. * @param array $args
  142. *
  143. * @return array
  144. */
  145. protected function action($method, $args)
  146. {
  147. $args["devKey"] = Mage_Testlink_Connector::$devKey;
  148. if (!$this->_client->query("tl.{$method}", $args)) {
  149. $response = null;
  150. } else {
  151. $response = $this->_client->getResponse();
  152. }
  153. return $response;
  154. }
  155. /**
  156. * Gets project's id
  157. *
  158. * @param string $name
  159. *
  160. * @return string
  161. */
  162. public function getProject($name)
  163. {
  164. if (!is_numeric($name)) {
  165. $method = 'getProjects';
  166. $args = array();
  167. $projects = $this->action($method, $args);
  168. if (!empty($projects)) {
  169. foreach ($projects as $project) {
  170. if (isset($project["name"]) && ($project["name"] == $name)) {
  171. return $id = isset($project["id"]) ? $project["id"] : null;
  172. }
  173. }
  174. } else {
  175. return null;
  176. }
  177. } else {
  178. return $name;
  179. }
  180. }
  181. /**
  182. * Gets array of all tests plans in project
  183. *
  184. * @param string $project_id
  185. *
  186. * @return array
  187. */
  188. protected function getTestPlans($project_id)
  189. {
  190. $plans = array();
  191. if (is_numeric($project_id)) {
  192. $method = 'getProjectTestPlans';
  193. $args = array();
  194. $args["testprojectid"] = $project_id;
  195. $plans = $this->action($method, $args);
  196. }
  197. return $plans;
  198. }
  199. /**
  200. * Gets the last test plan from project or searches for test plan by name or id
  201. *
  202. * @param string $project_id
  203. * @param string|null $testPlan
  204. *
  205. * @return array
  206. */
  207. public function getTestPlan($project_id, $testPlan=null)
  208. {
  209. $plans = $this->getTestPlans($project_id);
  210. if (isset($testPlan) && !empty($plans)) {
  211. if (is_numeric($testPlan)) {
  212. foreach ($plans as $plan) {
  213. if (isset($plan['id']) && $plan['id'] == $testPlan) {
  214. return $plan;
  215. }
  216. }
  217. } else {
  218. foreach ($plans as $plan) {
  219. if (isset($plan['name']) && $plan['name'] == $testPlan) {
  220. return $plan;
  221. }
  222. }
  223. }
  224. } else {
  225. return $plan = (!empty($plans)) ? $plans[count($plans) - 1] : array();
  226. }
  227. }
  228. /**
  229. * Gets array of all builds from the test plan
  230. *
  231. * @param string $testplan_id
  232. *
  233. * @return array
  234. */
  235. protected function getBuilds($testplan_id)
  236. {
  237. $method = 'getBuildsForTestPlan';
  238. $args = array();
  239. $args["testplanid"] = $testplan_id;
  240. return $this->action($method, $args);
  241. }
  242. /**
  243. * Gets current build
  244. *
  245. * @param string $testplan_id
  246. * @param string|null $buildId
  247. *
  248. * @return array
  249. */
  250. public function getBuild($testplan_id, $buildId=null)
  251. {
  252. $builds = isset($testplan_id) ? $this->getBuilds($testplan_id) : array();
  253. if (!empty($builds)) {
  254. if (isset($buildId)) {
  255. foreach ($builds as $build) {
  256. if (is_numeric($buildId)) {
  257. if (isset($build['id']) && $build['id'] == $buildId) {
  258. return $build;
  259. } elseif (isset($build['name']) && $build['name'] == $buildId) {
  260. return $build;
  261. }
  262. } else {
  263. if (isset($build['name']) && $build['name'] == $buildId) {
  264. return $build;
  265. }
  266. }
  267. }
  268. } else {
  269. return $builds[count($builds) -1];
  270. }
  271. }
  272. return array();
  273. }
  274. /**
  275. * Gets available tests from the test plan in testlink
  276. *
  277. * @param string $testplan_id
  278. *
  279. * @return array
  280. */
  281. protected function getTests($testplan_id)
  282. {
  283. $method = 'getTestCasesForTestPlan';
  284. $args = array();
  285. $args["testplanid"] = $testplan_id;
  286. return $this->action($method, $args);
  287. }
  288. }