phpunit5-loggers.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <?php
  2. // @codingStandardsIgnoreStart
  3. namespace {
  4. /*
  5. * This file is part of PHPUnit.
  6. *
  7. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. if (!class_exists('PHPUnit_Util_String')) {
  13. /**
  14. * String helpers.
  15. */
  16. class PHPUnit_Util_String
  17. {
  18. /**
  19. * Converts a string to UTF-8 encoding.
  20. *
  21. * @param string $string
  22. *
  23. * @return string
  24. */
  25. public static function convertToUtf8($string)
  26. {
  27. return mb_convert_encoding($string, 'UTF-8');
  28. }
  29. /**
  30. * Checks a string for UTF-8 encoding.
  31. *
  32. * @param string $string
  33. *
  34. * @return bool
  35. */
  36. protected static function isUtf8($string)
  37. {
  38. $length = strlen($string);
  39. for ($i = 0; $i < $length; $i++) {
  40. if (ord($string[$i]) < 0x80) {
  41. $n = 0;
  42. } elseif ((ord($string[$i]) & 0xE0) == 0xC0) {
  43. $n = 1;
  44. } elseif ((ord($string[$i]) & 0xF0) == 0xE0) {
  45. $n = 2;
  46. } elseif ((ord($string[$i]) & 0xF0) == 0xF0) {
  47. $n = 3;
  48. } else {
  49. return false;
  50. }
  51. for ($j = 0; $j < $n; $j++) {
  52. if ((++$i == $length) || ((ord($string[$i]) & 0xC0) != 0x80)) {
  53. return false;
  54. }
  55. }
  56. }
  57. return true;
  58. }
  59. }
  60. }
  61. /*
  62. * This file is part of PHPUnit.
  63. *
  64. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  65. *
  66. * For the full copyright and license information, please view the LICENSE
  67. * file that was distributed with this source code.
  68. */
  69. /**
  70. * A TestListener that generates JSON messages.
  71. */
  72. if (!class_exists('PHPUnit_Util_Log_JSON')) {
  73. class PHPUnit_Util_Log_JSON extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
  74. {
  75. /**
  76. * @var string
  77. */
  78. protected $currentTestSuiteName = '';
  79. /**
  80. * @var string
  81. */
  82. protected $currentTestName = '';
  83. /**
  84. * @var bool
  85. */
  86. protected $currentTestPass = true;
  87. /**
  88. * An error occurred.
  89. *
  90. * @param PHPUnit_Framework_Test $test
  91. * @param Exception $e
  92. * @param float $time
  93. */
  94. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  95. {
  96. $this->writeCase(
  97. 'error',
  98. $time,
  99. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  100. PHPUnit_Framework_TestFailure::exceptionToString($e),
  101. $test
  102. );
  103. $this->currentTestPass = false;
  104. }
  105. /**
  106. * A warning occurred.
  107. *
  108. * @param PHPUnit_Framework_Test $test
  109. * @param PHPUnit_Framework_Warning $e
  110. * @param float $time
  111. */
  112. public function addWarning(PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time)
  113. {
  114. $this->writeCase(
  115. 'warning',
  116. $time,
  117. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  118. PHPUnit_Framework_TestFailure::exceptionToString($e),
  119. $test
  120. );
  121. $this->currentTestPass = false;
  122. }
  123. /**
  124. * A failure occurred.
  125. *
  126. * @param PHPUnit_Framework_Test $test
  127. * @param PHPUnit_Framework_AssertionFailedError $e
  128. * @param float $time
  129. */
  130. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  131. {
  132. $this->writeCase(
  133. 'fail',
  134. $time,
  135. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  136. PHPUnit_Framework_TestFailure::exceptionToString($e),
  137. $test
  138. );
  139. $this->currentTestPass = false;
  140. }
  141. /**
  142. * Incomplete test.
  143. *
  144. * @param PHPUnit_Framework_Test $test
  145. * @param Exception $e
  146. * @param float $time
  147. */
  148. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  149. {
  150. $this->writeCase(
  151. 'error',
  152. $time,
  153. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  154. 'Incomplete Test: ' . $e->getMessage(),
  155. $test
  156. );
  157. $this->currentTestPass = false;
  158. }
  159. /**
  160. * Risky test.
  161. *
  162. * @param PHPUnit_Framework_Test $test
  163. * @param Exception $e
  164. * @param float $time
  165. */
  166. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  167. {
  168. $this->writeCase(
  169. 'error',
  170. $time,
  171. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  172. 'Risky Test: ' . $e->getMessage(),
  173. $test
  174. );
  175. $this->currentTestPass = false;
  176. }
  177. /**
  178. * Skipped test.
  179. *
  180. * @param PHPUnit_Framework_Test $test
  181. * @param Exception $e
  182. * @param float $time
  183. */
  184. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  185. {
  186. $this->writeCase(
  187. 'error',
  188. $time,
  189. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  190. 'Skipped Test: ' . $e->getMessage(),
  191. $test
  192. );
  193. $this->currentTestPass = false;
  194. }
  195. /**
  196. * A testsuite started.
  197. *
  198. * @param PHPUnit_Framework_TestSuite $suite
  199. */
  200. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  201. {
  202. $this->currentTestSuiteName = $suite->getName();
  203. $this->currentTestName = '';
  204. $this->write(
  205. [
  206. 'event' => 'suiteStart',
  207. 'suite' => $this->currentTestSuiteName,
  208. 'tests' => count($suite)
  209. ]
  210. );
  211. }
  212. /**
  213. * A testsuite ended.
  214. *
  215. * @param PHPUnit_Framework_TestSuite $suite
  216. */
  217. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  218. {
  219. $this->currentTestSuiteName = '';
  220. $this->currentTestName = '';
  221. }
  222. /**
  223. * A test started.
  224. *
  225. * @param PHPUnit_Framework_Test $test
  226. */
  227. public function startTest(PHPUnit_Framework_Test $test)
  228. {
  229. $this->currentTestName = PHPUnit_Util_Test::describe($test);
  230. $this->currentTestPass = true;
  231. $this->write(
  232. [
  233. 'event' => 'testStart',
  234. 'suite' => $this->currentTestSuiteName,
  235. 'test' => $this->currentTestName
  236. ]
  237. );
  238. }
  239. /**
  240. * A test ended.
  241. *
  242. * @param PHPUnit_Framework_Test $test
  243. * @param float $time
  244. */
  245. public function endTest(PHPUnit_Framework_Test $test, $time)
  246. {
  247. if ($this->currentTestPass) {
  248. $this->writeCase('pass', $time, [], '', $test);
  249. }
  250. }
  251. /**
  252. * @param string $status
  253. * @param float $time
  254. * @param array $trace
  255. * @param string $message
  256. * @param PHPUnit_Framework_TestCase|null $test
  257. */
  258. protected function writeCase($status, $time, array $trace = [], $message = '', $test = null)
  259. {
  260. $output = '';
  261. // take care of TestSuite producing error (e.g. by running into exception) as TestSuite doesn't have hasOutput
  262. if ($test !== null && method_exists($test, 'hasOutput') && $test->hasOutput()) {
  263. $output = $test->getActualOutput();
  264. }
  265. $this->write(
  266. [
  267. 'event' => 'test',
  268. 'suite' => $this->currentTestSuiteName,
  269. 'test' => $this->currentTestName,
  270. 'status' => $status,
  271. 'time' => $time,
  272. 'trace' => $trace,
  273. 'message' => PHPUnit_Util_String::convertToUtf8($message),
  274. 'output' => $output,
  275. ]
  276. );
  277. }
  278. /**
  279. * @param string $buffer
  280. */
  281. public function write($buffer)
  282. {
  283. array_walk_recursive(
  284. $buffer, function (&$input) {
  285. if (is_string($input)) {
  286. $input = PHPUnit_Util_String::convertToUtf8($input);
  287. }
  288. }
  289. );
  290. parent::write(json_encode($buffer, JSON_PRETTY_PRINT));
  291. }
  292. }
  293. }
  294. /*
  295. * This file is part of PHPUnit.
  296. *
  297. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  298. *
  299. * For the full copyright and license information, please view the LICENSE
  300. * file that was distributed with this source code.
  301. */
  302. if (!class_exists('PHPUnit_Util_Log_TAP')) {
  303. /**
  304. * A TestListener that generates a logfile of the
  305. * test execution using the Test Anything Protocol (TAP).
  306. */
  307. class PHPUnit_Util_Log_TAP extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
  308. {
  309. /**
  310. * @var int
  311. */
  312. protected $testNumber = 0;
  313. /**
  314. * @var int
  315. */
  316. protected $testSuiteLevel = 0;
  317. /**
  318. * @var bool
  319. */
  320. protected $testSuccessful = true;
  321. /**
  322. * Constructor.
  323. *
  324. * @param mixed $out
  325. *
  326. * @throws PHPUnit_Framework_Exception
  327. */
  328. public function __construct($out = null)
  329. {
  330. parent::__construct($out);
  331. $this->write("TAP version 13\n");
  332. }
  333. /**
  334. * An error occurred.
  335. *
  336. * @param PHPUnit_Framework_Test $test
  337. * @param Exception $e
  338. * @param float $time
  339. */
  340. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  341. {
  342. $this->writeNotOk($test, 'Error');
  343. }
  344. /**
  345. * A warning occurred.
  346. *
  347. * @param PHPUnit_Framework_Test $test
  348. * @param PHPUnit_Framework_Warning $e
  349. * @param float $time
  350. */
  351. public function addWarning(PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time)
  352. {
  353. $this->writeNotOk($test, 'Warning');
  354. }
  355. /**
  356. * A failure occurred.
  357. *
  358. * @param PHPUnit_Framework_Test $test
  359. * @param PHPUnit_Framework_AssertionFailedError $e
  360. * @param float $time
  361. */
  362. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  363. {
  364. $this->writeNotOk($test, 'Failure');
  365. $message = explode(
  366. "\n",
  367. PHPUnit_Framework_TestFailure::exceptionToString($e)
  368. );
  369. $diagnostic = [
  370. 'message' => $message[0],
  371. 'severity' => 'fail'
  372. ];
  373. if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
  374. $cf = $e->getComparisonFailure();
  375. if ($cf !== null) {
  376. $diagnostic['data'] = [
  377. 'got' => $cf->getActual(),
  378. 'expected' => $cf->getExpected()
  379. ];
  380. }
  381. }
  382. $yaml = new Symfony\Component\Yaml\Dumper;
  383. $this->write(
  384. sprintf(
  385. " ---\n%s ...\n",
  386. $yaml->dump($diagnostic, 2, 2)
  387. )
  388. );
  389. }
  390. /**
  391. * Incomplete test.
  392. *
  393. * @param PHPUnit_Framework_Test $test
  394. * @param Exception $e
  395. * @param float $time
  396. */
  397. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  398. {
  399. $this->writeNotOk($test, '', 'TODO Incomplete Test');
  400. }
  401. /**
  402. * Risky test.
  403. *
  404. * @param PHPUnit_Framework_Test $test
  405. * @param Exception $e
  406. * @param float $time
  407. */
  408. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  409. {
  410. $this->write(
  411. sprintf(
  412. "ok %d - # RISKY%s\n",
  413. $this->testNumber,
  414. $e->getMessage() != '' ? ' ' . $e->getMessage() : ''
  415. )
  416. );
  417. $this->testSuccessful = false;
  418. }
  419. /**
  420. * Skipped test.
  421. *
  422. * @param PHPUnit_Framework_Test $test
  423. * @param Exception $e
  424. * @param float $time
  425. */
  426. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  427. {
  428. $this->write(
  429. sprintf(
  430. "ok %d - # SKIP%s\n",
  431. $this->testNumber,
  432. $e->getMessage() != '' ? ' ' . $e->getMessage() : ''
  433. )
  434. );
  435. $this->testSuccessful = false;
  436. }
  437. /**
  438. * A testsuite started.
  439. *
  440. * @param PHPUnit_Framework_TestSuite $suite
  441. */
  442. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  443. {
  444. $this->testSuiteLevel++;
  445. }
  446. /**
  447. * A testsuite ended.
  448. *
  449. * @param PHPUnit_Framework_TestSuite $suite
  450. */
  451. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  452. {
  453. $this->testSuiteLevel--;
  454. if ($this->testSuiteLevel == 0) {
  455. $this->write(sprintf("1..%d\n", $this->testNumber));
  456. }
  457. }
  458. /**
  459. * A test started.
  460. *
  461. * @param PHPUnit_Framework_Test $test
  462. */
  463. public function startTest(PHPUnit_Framework_Test $test)
  464. {
  465. $this->testNumber++;
  466. $this->testSuccessful = true;
  467. }
  468. /**
  469. * A test ended.
  470. *
  471. * @param PHPUnit_Framework_Test $test
  472. * @param float $time
  473. */
  474. public function endTest(PHPUnit_Framework_Test $test, $time)
  475. {
  476. if ($this->testSuccessful === true) {
  477. $this->write(
  478. sprintf(
  479. "ok %d - %s\n",
  480. $this->testNumber,
  481. PHPUnit_Util_Test::describe($test)
  482. )
  483. );
  484. }
  485. $this->writeDiagnostics($test);
  486. }
  487. /**
  488. * @param PHPUnit_Framework_Test $test
  489. * @param string $prefix
  490. * @param string $directive
  491. */
  492. protected function writeNotOk(PHPUnit_Framework_Test $test, $prefix = '', $directive = '')
  493. {
  494. $this->write(
  495. sprintf(
  496. "not ok %d - %s%s%s\n",
  497. $this->testNumber,
  498. $prefix != '' ? $prefix . ': ' : '',
  499. PHPUnit_Util_Test::describe($test),
  500. $directive != '' ? ' # ' . $directive : ''
  501. )
  502. );
  503. $this->testSuccessful = false;
  504. }
  505. /**
  506. * @param PHPUnit_Framework_Test $test
  507. */
  508. private function writeDiagnostics(PHPUnit_Framework_Test $test)
  509. {
  510. if (!$test instanceof PHPUnit_Framework_TestCase) {
  511. return;
  512. }
  513. if (!$test->hasOutput()) {
  514. return;
  515. }
  516. foreach (explode("\n", trim($test->getActualOutput())) as $line) {
  517. $this->write(
  518. sprintf(
  519. "# %s\n",
  520. $line
  521. )
  522. );
  523. }
  524. }
  525. }
  526. }
  527. }
  528. // @codingStandardsIgnoreEnd