PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/win32-phpunit.php

https://github.com/sezuan/core
PHP | 347 lines | 279 code | 47 blank | 21 comment | 37 complexity | 1d9989dc0cf7324fb75fdc586ade0a77 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. OC_PHPUnit_Loader::checkIncludePath();
  3. OC_PHPUnit_Loader::detectPHPUnitVersionId();
  4. //load PHPUnit
  5. switch (OC_PHPUnit_Loader::$PHPUnitVersionId) {
  6. case "36": {
  7. OC_PHPUnit_Loader::load36();
  8. break;
  9. }
  10. case "37": {
  11. OC_PHPUnit_Loader::load37();
  12. break;
  13. }
  14. }
  15. //load custom implementation of the PHPUnit_TextUI_ResultPrinter
  16. switch (OC_PHPUnit_Loader::$PHPUnitVersionId) {
  17. case "36":
  18. case "37": {
  19. class OC_PHPUnit_TextUI_ResultPrinter extends PHPUnit_TextUI_ResultPrinter
  20. {
  21. function __construct()
  22. {
  23. parent::__construct('php://stderr');
  24. }
  25. public function printResult(PHPUnit_Framework_TestResult $result)
  26. {
  27. $this->printHeader();
  28. $this->printFooter($result);
  29. }
  30. protected function writeProgress($progress)
  31. {
  32. //ignore
  33. }
  34. }
  35. break;
  36. }
  37. }
  38. //loading of OC_PHPUnit_TextUI_Command
  39. switch (OC_PHPUnit_Loader::$PHPUnitVersionId) {
  40. case "36":
  41. case "37": {
  42. class OC_PHPUnit_TextUI_Command extends PHPUnit_TextUI_Command
  43. {
  44. public static function main($exit = TRUE)
  45. {
  46. $command = new OC_PHPUnit_TextUI_Command();
  47. $command->run($_SERVER['argv'], $exit);
  48. }
  49. protected function handleArguments(array $argv)
  50. {
  51. parent::handleArguments($argv);
  52. $this->arguments['listeners'][] = new OC_PHPUnit_Framework_TestListener();
  53. $this->arguments['printer'] = new OC_PHPUnit_TextUI_ResultPrinter();
  54. }
  55. protected function createRunner()
  56. {
  57. $coverage_Filter = new PHP_CodeCoverage_Filter();
  58. $coverage_Filter->addFileToBlacklist(__FILE__);
  59. $runner = new PHPUnit_TextUI_TestRunner($this->arguments['loader'], $coverage_Filter);
  60. return $runner;
  61. }
  62. }
  63. break;
  64. }
  65. }
  66. class OC_PHPUnit_Loader
  67. {
  68. const SUCCESS_EXIT = 0;
  69. const FAILURE_EXIT = 1;
  70. const EXCEPTION_EXIT = 2;
  71. public static $PHPUnitVersionId;
  72. /**
  73. * @return void
  74. */
  75. public static function checkIncludePath()
  76. {
  77. //check include path
  78. $PHPUnitParentDirectory = self::getPHPUnitParentDirectory();
  79. if (is_null($PHPUnitParentDirectory)) {
  80. echo "Cannot find PHPUnit in include path (" . ini_get('include_path') . ")";
  81. exit(OC_PHPUnit_Loader::FAILURE_EXIT);
  82. }
  83. }
  84. /**
  85. * @return null | string
  86. */
  87. private static function getPHPUnitParentDirectory()
  88. {
  89. $pathArray = explode(PATH_SEPARATOR, ini_get('include_path'));
  90. foreach ($pathArray as $path)
  91. {
  92. if (file_exists($path . DIRECTORY_SEPARATOR . 'PHPUnit/')) {
  93. return $path;
  94. }
  95. }
  96. return null;
  97. }
  98. /**
  99. * @return void
  100. */
  101. public static function detectPHPUnitVersionId()
  102. {
  103. require_once 'PHPUnit/Runner/Version.php';
  104. $PHPUnitVersion = PHPUnit_Runner_Version::id();
  105. if ($PHPUnitVersion === "@package_version@") {
  106. self::$PHPUnitVersionId = "37";
  107. }
  108. else if (version_compare($PHPUnitVersion, '3.7.0') >= 0) {
  109. self::$PHPUnitVersionId = "37";
  110. }
  111. else if (version_compare($PHPUnitVersion, '3.6.0') >= 0) {
  112. self::$PHPUnitVersionId = "36";
  113. }
  114. else if (version_compare($PHPUnitVersion, '3.6.0') >= 0) {
  115. echo "unsupported PHPUnit version: $PHPUnitVersion";
  116. exit(OC_PHPUnit_Loader::FAILURE_EXIT);
  117. }
  118. }
  119. /**
  120. * @return void
  121. */
  122. public static function load37()
  123. {
  124. require 'PHPUnit/Autoload.php';
  125. }
  126. /**
  127. * @return void
  128. */
  129. public static function load36()
  130. {
  131. define('PHPUnit_MAIN_METHOD', 'OC_PHPUnit_TextUI_Command::main');
  132. require 'PHPUnit/Autoload.php';
  133. }
  134. }
  135. class OC_PHPUnit_Framework_TestListener implements PHPUnit_Framework_TestListener
  136. {
  137. private $isSummaryTestCountPrinted = false;
  138. public static function printEvent($eventName, $params = array())
  139. {
  140. self::printText("\n[$eventName");
  141. foreach ($params as $key => $value) {
  142. self::printText(" $key='$value'");
  143. }
  144. self::printText("]\n");
  145. }
  146. public static function printText($text)
  147. {
  148. file_put_contents('php://stderr', $text);
  149. }
  150. private static function getMessage(Exception $e)
  151. {
  152. $message = "";
  153. if (strlen(get_class($e)) != 0) {
  154. $message = $message . get_class($e);
  155. }
  156. if (strlen($message) != 0 && strlen($e->getMessage()) != 0) {
  157. $message = $message . " : ";
  158. }
  159. $message = $message . $e->getMessage();
  160. return self::escapeValue($message);
  161. }
  162. private static function getDetails(Exception $e)
  163. {
  164. return self::escapeValue($e->getTraceAsString());
  165. }
  166. public static function getValueAsString($value)
  167. {
  168. if (is_null($value)) {
  169. return "null";
  170. }
  171. else if (is_bool($value)) {
  172. return $value == true ? "true" : "false";
  173. }
  174. else if (is_array($value) || is_string($value)) {
  175. $valueAsString = print_r($value, true);
  176. if (strlen($valueAsString) > 10000) {
  177. return null;
  178. }
  179. return $valueAsString;
  180. }
  181. else if (is_scalar($value)){
  182. return print_r($value, true);
  183. }
  184. return null;
  185. }
  186. private static function escapeValue($text) {
  187. $text = str_replace("|", "||", $text);
  188. $text = str_replace("'", "|'", $text);
  189. $text = str_replace("\n", "|n", $text);
  190. $text = str_replace("\r", "|r", $text);
  191. $text = str_replace("]", "|]", $text);
  192. return $text;
  193. }
  194. public static function getFileName($className)
  195. {
  196. $reflectionClass = new ReflectionClass($className);
  197. $fileName = $reflectionClass->getFileName();
  198. return $fileName;
  199. }
  200. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  201. {
  202. self::printEvent("testFailed", array(
  203. "name" => $test->getName(),
  204. "message" => self::getMessage($e),
  205. "details" => self::getDetails($e)
  206. ));
  207. }
  208. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  209. {
  210. $params = array(
  211. "name" => $test->getName(),
  212. "message" => self::getMessage($e),
  213. "details" => self::getDetails($e)
  214. );
  215. if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
  216. $comparisonFailure = $e->getComparisonFailure();
  217. if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure) {
  218. $actualResult = $comparisonFailure->getActual();
  219. $expectedResult = $comparisonFailure->getExpected();
  220. $actualString = self::getValueAsString($actualResult);
  221. $expectedString = self::getValueAsString($expectedResult);
  222. if (!is_null($actualString) && !is_null($expectedString)) {
  223. $params['actual'] = self::escapeValue($actualString);
  224. $params['expected'] = self::escapeValue($expectedString);
  225. }
  226. }
  227. }
  228. self::printEvent("testFailed", $params);
  229. }
  230. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  231. {
  232. self::printEvent("testIgnored", array(
  233. "name" => $test->getName(),
  234. "message" => self::getMessage($e),
  235. "details" => self::getDetails($e)
  236. ));
  237. }
  238. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  239. {
  240. self::printEvent("testIgnored", array(
  241. "name" => $test->getName(),
  242. "message" => self::getMessage($e),
  243. "details" => self::getDetails($e)
  244. ));
  245. }
  246. public function startTest(PHPUnit_Framework_Test $test)
  247. {
  248. $testName = $test->getName();
  249. $params = array(
  250. "name" => $testName
  251. );
  252. if ($test instanceof PHPUnit_Framework_TestCase) {
  253. $className = get_class($test);
  254. $fileName = self::getFileName($className);
  255. $params['locationHint'] = "php_qn://$fileName::\\$className::$testName";
  256. }
  257. self::printEvent("testStarted", $params);
  258. }
  259. public function endTest(PHPUnit_Framework_Test $test, $time)
  260. {
  261. self::printEvent("testFinished", array(
  262. "name" => $test->getName(),
  263. "duration" => (int)(round($time, 2) * 1000)
  264. ));
  265. }
  266. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  267. {
  268. if (!$this->isSummaryTestCountPrinted) {
  269. $this->isSummaryTestCountPrinted = true;
  270. //print tests count
  271. self::printEvent("testCount", array(
  272. "count" => count($suite)
  273. ));
  274. }
  275. $suiteName = $suite->getName();
  276. if (empty($suiteName)) {
  277. return;
  278. }
  279. $params = array(
  280. "name" => $suiteName,
  281. );
  282. if (class_exists($suiteName, false)) {
  283. $fileName = self::getFileName($suiteName);
  284. $params['locationHint'] = "php_qn://$fileName::\\$suiteName";
  285. }
  286. self::printEvent("testSuiteStarted", $params);
  287. }
  288. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  289. {
  290. $suiteName = $suite->getName();
  291. if (empty($suiteName)) {
  292. return;
  293. }
  294. self::printEvent("testSuiteFinished",
  295. array(
  296. "name" => $suite->getName()
  297. ));
  298. }
  299. }
  300. OC_PHPUnit_TextUI_Command::main();