PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/phpunit/phpunit/src/Util/Log/JSON.php

https://gitlab.com/jhonn/rest
PHP | 283 lines | 128 code | 23 blank | 132 comment | 6 complexity | f13bd1e46ad82b364f9985621ab6dd4b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2014, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Util_Log
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.0.0
  44. */
  45. if (!defined('JSON_PRETTY_PRINT')) {
  46. define('JSON_PRETTY_PRINT', 128);
  47. }
  48. /**
  49. * A TestListener that generates JSON messages.
  50. *
  51. * @package PHPUnit
  52. * @subpackage Util_Log
  53. * @author Sebastian Bergmann <sebastian@phpunit.de>
  54. * @copyright 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
  55. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  56. * @link http://www.phpunit.de/
  57. * @since Class available since Release 3.0.0
  58. */
  59. class PHPUnit_Util_Log_JSON extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
  60. {
  61. /**
  62. * @var string
  63. */
  64. protected $currentTestSuiteName = '';
  65. /**
  66. * @var string
  67. */
  68. protected $currentTestName = '';
  69. /**
  70. * @var boolean
  71. * @access private
  72. */
  73. protected $currentTestPass = true;
  74. /**
  75. * An error occurred.
  76. *
  77. * @param PHPUnit_Framework_Test $test
  78. * @param Exception $e
  79. * @param float $time
  80. */
  81. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  82. {
  83. $this->writeCase(
  84. 'error',
  85. $time,
  86. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  87. $e->getMessage(),
  88. $test
  89. );
  90. $this->currentTestPass = false;
  91. }
  92. /**
  93. * A failure occurred.
  94. *
  95. * @param PHPUnit_Framework_Test $test
  96. * @param PHPUnit_Framework_AssertionFailedError $e
  97. * @param float $time
  98. */
  99. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  100. {
  101. $this->writeCase(
  102. 'fail',
  103. $time,
  104. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  105. $e->getMessage(),
  106. $test
  107. );
  108. $this->currentTestPass = false;
  109. }
  110. /**
  111. * Incomplete test.
  112. *
  113. * @param PHPUnit_Framework_Test $test
  114. * @param Exception $e
  115. * @param float $time
  116. */
  117. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  118. {
  119. $this->writeCase(
  120. 'error',
  121. $time,
  122. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  123. 'Incomplete Test: ' . $e->getMessage(),
  124. $test
  125. );
  126. $this->currentTestPass = false;
  127. }
  128. /**
  129. * Risky test.
  130. *
  131. * @param PHPUnit_Framework_Test $test
  132. * @param Exception $e
  133. * @param float $time
  134. * @since Method available since Release 4.0.0
  135. */
  136. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  137. {
  138. $this->writeCase(
  139. 'error',
  140. $time,
  141. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  142. 'Risky Test: ' . $e->getMessage(),
  143. $test
  144. );
  145. $this->currentTestPass = false;
  146. }
  147. /**
  148. * Skipped test.
  149. *
  150. * @param PHPUnit_Framework_Test $test
  151. * @param Exception $e
  152. * @param float $time
  153. */
  154. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  155. {
  156. $this->writeCase(
  157. 'error',
  158. $time,
  159. PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
  160. 'Skipped Test: ' . $e->getMessage(),
  161. $test
  162. );
  163. $this->currentTestPass = false;
  164. }
  165. /**
  166. * A testsuite started.
  167. *
  168. * @param PHPUnit_Framework_TestSuite $suite
  169. */
  170. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  171. {
  172. $this->currentTestSuiteName = $suite->getName();
  173. $this->currentTestName = '';
  174. $this->write(
  175. array(
  176. 'event' => 'suiteStart',
  177. 'suite' => $this->currentTestSuiteName,
  178. 'tests' => count($suite)
  179. )
  180. );
  181. }
  182. /**
  183. * A testsuite ended.
  184. *
  185. * @param PHPUnit_Framework_TestSuite $suite
  186. */
  187. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  188. {
  189. $this->currentTestSuiteName = '';
  190. $this->currentTestName = '';
  191. }
  192. /**
  193. * A test started.
  194. *
  195. * @param PHPUnit_Framework_Test $test
  196. */
  197. public function startTest(PHPUnit_Framework_Test $test)
  198. {
  199. $this->currentTestName = PHPUnit_Util_Test::describe($test);
  200. $this->currentTestPass = true;
  201. $this->write(
  202. array(
  203. 'event' => 'testStart',
  204. 'suite' => $this->currentTestSuiteName,
  205. 'test' => $this->currentTestName
  206. )
  207. );
  208. }
  209. /**
  210. * A test ended.
  211. *
  212. * @param PHPUnit_Framework_Test $test
  213. * @param float $time
  214. */
  215. public function endTest(PHPUnit_Framework_Test $test, $time)
  216. {
  217. if ($this->currentTestPass) {
  218. $this->writeCase('pass', $time, array(), '', $test);
  219. }
  220. }
  221. /**
  222. * @param string $status
  223. * @param float $time
  224. * @param array $trace
  225. * @param string $message
  226. * @param PHPUnit_Framework_TestCase|null $test
  227. */
  228. protected function writeCase($status, $time, array $trace = array(), $message = '', $test = null)
  229. {
  230. $output = '';
  231. // take care of TestSuite producing error (e.g. by running into exception) as TestSuite doesn't have hasOutput
  232. if ($test !== null && method_exists($test, 'hasOutput') && $test->hasOutput()) {
  233. $output = $test->getActualOutput();
  234. }
  235. $this->write(
  236. array(
  237. 'event' => 'test',
  238. 'suite' => $this->currentTestSuiteName,
  239. 'test' => $this->currentTestName,
  240. 'status' => $status,
  241. 'time' => $time,
  242. 'trace' => $trace,
  243. 'message' => PHPUnit_Util_String::convertToUtf8($message),
  244. 'output' => $output,
  245. )
  246. );
  247. }
  248. /**
  249. * @param string $buffer
  250. */
  251. public function write($buffer)
  252. {
  253. array_walk_recursive($buffer, function (&$input) {
  254. if (is_string($input)) {
  255. $input = PHPUnit_Util_String::convertToUtf8($input);
  256. }
  257. });
  258. parent::write(json_encode($buffer, JSON_PRETTY_PRINT));
  259. }
  260. }