PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/vendor/phpunit/phpunit/src/Util/Log/TeamCity.php

https://gitlab.com/Georgiy.Zhegusov/museum_documents
PHP | 409 lines | 228 code | 58 blank | 123 comment | 37 complexity | ed8fb5233e1ab132991631e5aa74dba0 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use SebastianBergmann\Comparator\ComparisonFailure;
  11. /**
  12. * A TestListener that generates a logfile of the test execution using the
  13. * TeamCity format (for use with PhpStorm, for instance).
  14. *
  15. * @since Class available since Release 5.0.0
  16. */
  17. class PHPUnit_Util_Log_TeamCity extends PHPUnit_TextUI_ResultPrinter
  18. {
  19. /**
  20. * @var bool
  21. */
  22. private $isSummaryTestCountPrinted = false;
  23. /**
  24. * @var string
  25. */
  26. private $startedTestName;
  27. /**
  28. * @var string
  29. */
  30. private $flowId;
  31. /**
  32. * @param string $progress
  33. */
  34. protected function writeProgress($progress)
  35. {
  36. }
  37. /**
  38. * @param PHPUnit_Framework_TestResult $result
  39. */
  40. public function printResult(PHPUnit_Framework_TestResult $result)
  41. {
  42. $this->printHeader();
  43. $this->printFooter($result);
  44. }
  45. /**
  46. * An error occurred.
  47. *
  48. * @param PHPUnit_Framework_Test $test
  49. * @param Exception $e
  50. * @param float $time
  51. */
  52. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  53. {
  54. $this->printEvent(
  55. 'testFailed',
  56. [
  57. 'name' => $test->getName(),
  58. 'message' => self::getMessage($e),
  59. 'details' => self::getDetails($e),
  60. ]
  61. );
  62. }
  63. /**
  64. * A warning occurred.
  65. *
  66. * @param PHPUnit_Framework_Test $test
  67. * @param PHPUnit_Framework_Warning $e
  68. * @param float $time
  69. *
  70. * @since Method available since Release 5.1.0
  71. */
  72. public function addWarning(PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time)
  73. {
  74. $this->printEvent(
  75. 'testFailed',
  76. [
  77. 'name' => $test->getName(),
  78. 'message' => self::getMessage($e),
  79. 'details' => self::getDetails($e)
  80. ]
  81. );
  82. }
  83. /**
  84. * A failure occurred.
  85. *
  86. * @param PHPUnit_Framework_Test $test
  87. * @param PHPUnit_Framework_AssertionFailedError $e
  88. * @param float $time
  89. */
  90. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  91. {
  92. $parameters = [
  93. 'name' => $test->getName(),
  94. 'message' => self::getMessage($e),
  95. 'details' => self::getDetails($e),
  96. ];
  97. if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
  98. $comparisonFailure = $e->getComparisonFailure();
  99. if ($comparisonFailure instanceof ComparisonFailure) {
  100. $expectedString = $comparisonFailure->getExpectedAsString();
  101. if (is_null($expectedString) || empty($expectedString)) {
  102. $expectedString = self::getPrimitiveValueAsString($comparisonFailure->getExpected());
  103. }
  104. $actualString = $comparisonFailure->getActualAsString();
  105. if (is_null($actualString) || empty($actualString)) {
  106. $actualString = self::getPrimitiveValueAsString($comparisonFailure->getActual());
  107. }
  108. if (!is_null($actualString) && !is_null($expectedString)) {
  109. $parameters['actual'] = $actualString;
  110. $parameters['expected'] = $expectedString;
  111. }
  112. }
  113. }
  114. $this->printEvent('testFailed', $parameters);
  115. }
  116. /**
  117. * Incomplete test.
  118. *
  119. * @param PHPUnit_Framework_Test $test
  120. * @param Exception $e
  121. * @param float $time
  122. */
  123. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  124. {
  125. $this->printIgnoredTest($test->getName(), $e);
  126. }
  127. /**
  128. * Risky test.
  129. *
  130. * @param PHPUnit_Framework_Test $test
  131. * @param Exception $e
  132. * @param float $time
  133. */
  134. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  135. {
  136. $this->addError($test, $e, $time);
  137. }
  138. /**
  139. * Skipped test.
  140. *
  141. * @param PHPUnit_Framework_Test $test
  142. * @param Exception $e
  143. * @param float $time
  144. */
  145. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  146. {
  147. $testName = $test->getName();
  148. if ($this->startedTestName != $testName) {
  149. $this->startTest($test);
  150. $this->printIgnoredTest($testName, $e);
  151. $this->endTest($test, $time);
  152. } else {
  153. $this->printIgnoredTest($testName, $e);
  154. }
  155. }
  156. public function printIgnoredTest($testName, Exception $e)
  157. {
  158. $this->printEvent(
  159. 'testIgnored',
  160. [
  161. 'name' => $testName,
  162. 'message' => self::getMessage($e),
  163. 'details' => self::getDetails($e),
  164. ]
  165. );
  166. }
  167. /**
  168. * A testsuite started.
  169. *
  170. * @param PHPUnit_Framework_TestSuite $suite
  171. */
  172. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  173. {
  174. if (stripos(ini_get('disable_functions'), 'getmypid') === false) {
  175. $this->flowId = getmypid();
  176. } else {
  177. $this->flowId = false;
  178. }
  179. if (!$this->isSummaryTestCountPrinted) {
  180. $this->isSummaryTestCountPrinted = true;
  181. $this->printEvent(
  182. 'testCount',
  183. ['count' => count($suite)]
  184. );
  185. }
  186. $suiteName = $suite->getName();
  187. if (empty($suiteName)) {
  188. return;
  189. }
  190. $parameters = ['name' => $suiteName];
  191. if (class_exists($suiteName, false)) {
  192. $fileName = self::getFileName($suiteName);
  193. $parameters['locationHint'] = "php_qn://$fileName::\\$suiteName";
  194. } else {
  195. $split = preg_split('/::/', $suiteName);
  196. if (count($split) == 2 && method_exists($split[0], $split[1])) {
  197. $fileName = self::getFileName($split[0]);
  198. $parameters['locationHint'] = "php_qn://$fileName::\\$suiteName";
  199. $parameters['name'] = $split[1];
  200. }
  201. }
  202. $this->printEvent('testSuiteStarted', $parameters);
  203. }
  204. /**
  205. * A testsuite ended.
  206. *
  207. * @param PHPUnit_Framework_TestSuite $suite
  208. */
  209. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  210. {
  211. $suiteName = $suite->getName();
  212. if (empty($suiteName)) {
  213. return;
  214. }
  215. $parameters = ['name' => $suiteName];
  216. if (!class_exists($suiteName, false)) {
  217. $split = preg_split('/::/', $suiteName);
  218. if (count($split) == 2 && method_exists($split[0], $split[1])) {
  219. $parameters['name'] = $split[1];
  220. }
  221. }
  222. $this->printEvent('testSuiteFinished', $parameters);
  223. }
  224. /**
  225. * A test started.
  226. *
  227. * @param PHPUnit_Framework_Test $test
  228. */
  229. public function startTest(PHPUnit_Framework_Test $test)
  230. {
  231. $testName = $test->getName();
  232. $this->startedTestName = $testName;
  233. $params = ['name' => $testName];
  234. if ($test instanceof PHPUnit_Framework_TestCase) {
  235. $className = get_class($test);
  236. $fileName = self::getFileName($className);
  237. $params['locationHint'] = "php_qn://$fileName::\\$className::$testName";
  238. }
  239. $this->printEvent('testStarted', $params);
  240. }
  241. /**
  242. * A test ended.
  243. *
  244. * @param PHPUnit_Framework_Test $test
  245. * @param float $time
  246. */
  247. public function endTest(PHPUnit_Framework_Test $test, $time)
  248. {
  249. parent::endTest($test, $time);
  250. $this->printEvent(
  251. 'testFinished',
  252. [
  253. 'name' => $test->getName(),
  254. 'duration' => (int) (round($time, 2) * 1000)
  255. ]
  256. );
  257. }
  258. /**
  259. * @param string $eventName
  260. * @param array $params
  261. */
  262. private function printEvent($eventName, $params = [])
  263. {
  264. $this->write("\n##teamcity[$eventName");
  265. if ($this->flowId) {
  266. $params['flowId'] = $this->flowId;
  267. }
  268. foreach ($params as $key => $value) {
  269. $escapedValue = self::escapeValue($value);
  270. $this->write(" $key='$escapedValue'");
  271. }
  272. $this->write("]\n");
  273. }
  274. /**
  275. * @param Exception $e
  276. *
  277. * @return string
  278. */
  279. private static function getMessage(Exception $e)
  280. {
  281. $message = '';
  282. if (!$e instanceof PHPUnit_Framework_Exception) {
  283. if (strlen(get_class($e)) != 0) {
  284. $message = $message . get_class($e);
  285. }
  286. if (strlen($message) != 0 && strlen($e->getMessage()) != 0) {
  287. $message = $message . ' : ';
  288. }
  289. }
  290. return $message . $e->getMessage();
  291. }
  292. /**
  293. * @param Exception $e
  294. *
  295. * @return string
  296. */
  297. private static function getDetails(Exception $e)
  298. {
  299. $stackTrace = PHPUnit_Util_Filter::getFilteredStacktrace($e);
  300. $previous = $e->getPrevious();
  301. while ($previous) {
  302. $stackTrace .= "\nCaused by\n" .
  303. PHPUnit_Framework_TestFailure::exceptionToString($previous) . "\n" .
  304. PHPUnit_Util_Filter::getFilteredStacktrace($previous);
  305. $previous = $previous->getPrevious();
  306. }
  307. return ' ' . str_replace("\n", "\n ", $stackTrace);
  308. }
  309. /**
  310. * @param mixed $value
  311. *
  312. * @return string
  313. */
  314. private static function getPrimitiveValueAsString($value)
  315. {
  316. if (is_null($value)) {
  317. return 'null';
  318. } elseif (is_bool($value)) {
  319. return $value == true ? 'true' : 'false';
  320. } elseif (is_scalar($value)) {
  321. return print_r($value, true);
  322. }
  323. return;
  324. }
  325. /**
  326. * @param $text
  327. *
  328. * @return string
  329. */
  330. private static function escapeValue($text)
  331. {
  332. $text = str_replace('|', '||', $text);
  333. $text = str_replace("'", "|'", $text);
  334. $text = str_replace("\n", '|n', $text);
  335. $text = str_replace("\r", '|r', $text);
  336. $text = str_replace(']', '|]', $text);
  337. $text = str_replace('[', '|[', $text);
  338. return $text;
  339. }
  340. /**
  341. * @param string $className
  342. *
  343. * @return string
  344. */
  345. private static function getFileName($className)
  346. {
  347. $reflectionClass = new ReflectionClass($className);
  348. $fileName = $reflectionClass->getFileName();
  349. return $fileName;
  350. }
  351. }