Guest User

Untitled

a guest
Apr 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. <?php
  2.  
  3. class PHPUnit_Extensions_TestListener_GrowlTestListener
  4. implements PHPUnit_Framework_Testlistener
  5. {
  6. const TEST_RESULT_COLOR_RED = 'red';
  7. const TEST_RESULT_COLOR_YELLOW = 'yellow';
  8. const TEST_RESULT_COLOR_GREEN = 'green';
  9.  
  10. private $_errors = array();
  11. private $_failures = array();
  12. private $_incompletes = array();
  13. private $_skips = array();
  14. private $_tests = array();
  15. private $_suites = array();
  16. private $_endedSuites = 0;
  17. private $_assertionCount = 0;
  18. private $_startTime = 0;
  19.  
  20. private $_successPicturePath = null;
  21. private $_incompletePicturePath = null;
  22. private $_failurePicturePath = null;
  23.  
  24. /**
  25. * @param string $successPicturePath
  26. * @param string $incompletePicturePath
  27. * @param string $failurePicturePath
  28. */
  29. public function __construct($successPicturePath, $incompletePicturePath,
  30. $failurePicturePath)
  31. {
  32. $this->_successPicturePath = $successPicturePath;
  33. $this->_incompletePicturePath = $incompletePicturePath;
  34. $this->_failurePicturePath = $failurePicturePath;
  35. }
  36.  
  37. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  38. {
  39. $this->_errors[] = $test->getName();
  40. }
  41.  
  42. public function addFailure(PHPUnit_Framework_Test $test,
  43. PHPUnit_Framework_AssertionFailedError $e, $time)
  44. {
  45. $this->_failures[] = $test->getName();
  46. }
  47.  
  48. public function addIncompleteTest(PHPUnit_Framework_Test $test,
  49. Exception $e, $time)
  50. {
  51. $this->_incompletes[] = $test->getName();
  52. }
  53.  
  54. public function addSkippedTest(PHPUnit_Framework_Test $test,
  55. Exception $e, $time)
  56. {
  57. $this->_skips[] = $test->getName();
  58. }
  59.  
  60. public function startTest(PHPUnit_Framework_Test $test)
  61. {
  62.  
  63. }
  64.  
  65. public function endTest(PHPUnit_Framework_Test $test, $time)
  66. {
  67. $this->_tests[] = array('name' => $test->getName(),
  68. 'assertions' => $test->getNumAssertions()
  69. );
  70. $this->_assertionCount+= $test->getNumAssertions();
  71. }
  72.  
  73. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  74. {
  75. if (count($this->_suites) === 0) {
  76. PHP_Timer::start();
  77. }
  78. $this->_suites[] = $suite->getName();
  79. }
  80.  
  81. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  82. {
  83. $this->_endedSuites++;
  84.  
  85. if (count($this->_suites) <= $this->_endedSuites)
  86. {
  87. $testTime = PHP_Timer::secondsToTimeString(
  88. PHP_Timer::stop());
  89.  
  90. if ($this->_isGreenTestResult()) {
  91. $resultColor = self::TEST_RESULT_COLOR_GREEN;
  92. }
  93. if ($this->_isRedTestResult()) {
  94. $resultColor = self::TEST_RESULT_COLOR_RED;
  95. }
  96. if ($this->_isYellowTestResult()) {
  97. $resultColor = self::TEST_RESULT_COLOR_YELLOW;
  98. }
  99.  
  100. $suiteCount = count($this->_suites);
  101. $testCount = count($this->_tests);
  102. $failureCount = count($this->_failures);
  103. $errorCount = count($this->_errors);
  104. $incompleteCount = count($this->_incompletes);
  105. $skipCount = count($this->_skips);
  106.  
  107. $resultMessage = '';
  108.  
  109. if ($suiteCount > 1) {
  110. $resultMessage.= "Suites: {$suiteCount}, ";
  111. }
  112. $resultMessage.= "Tests: {$testCount}, ";
  113. $resultMessage.= "Assertions: {$this->_assertionCount}";
  114.  
  115. if ($failureCount > 0) {
  116. $resultMessage.= ", Failures: {$failureCount}";
  117. }
  118.  
  119. if ($errorCount > 0) {
  120. $resultMessage.= ", Errors: {$errorCount}";
  121. }
  122.  
  123. if ($incompleteCount > 0) {
  124. $resultMessage.= ", Incompletes: {$incompleteCount}";
  125. }
  126.  
  127. if ($skipCount > 0) {
  128. $resultMessage.= ", Skips: {$skipCount}";
  129. }
  130. $resultMessage.= " in {$testTime}.";
  131. $this->_growlnotify($resultColor, $resultMessage);
  132. }
  133. }
  134.  
  135. /**
  136. * @param string $resultColor
  137. * @param string $message
  138. * @param string $sender The name of the application that sends the notification
  139. * @throws RuntimeException When growlnotify is not available
  140. */
  141. private function _growlnotify($resultColor, $message = null, $sender = 'PHPUnit')
  142. {
  143. if ($this->_isGrowlnotifyAvailable() === false) {
  144. throw new RuntimeException('The growlnotify tool is not available');
  145. }
  146. $notificationImage = $this->_getNotificationImageByResultColor(
  147. $resultColor);
  148. $command = "growlnotify -w -s -m '{$message}' "
  149. . "-n '{$sender}' "
  150. . "-p 2 --image {$notificationImage}";
  151. exec($command, $response, $return);
  152. }
  153.  
  154. /**
  155. * @return boolean
  156. */
  157. private function _isGrowlnotifyAvailable()
  158. {
  159. exec('growlnotify -v', $reponse, $status);
  160. return ($status === 0);
  161. }
  162.  
  163. /**
  164. * @param string $color
  165. * @return string
  166. */
  167. private function _getNotificationImageByResultColor($color)
  168. {
  169. switch ($color) {
  170. case self::TEST_RESULT_COLOR_RED:
  171. return $this->_failurePicturePath;
  172. break;
  173. case self::TEST_RESULT_COLOR_GREEN:
  174. return $this->_successPicturePath;
  175. break;
  176. default:
  177. return $this->_incompletePicturePath;
  178. }
  179. }
  180.  
  181. /**
  182. * @return boolean
  183. */
  184. private function _isGreenTestResult()
  185. {
  186. return count($this->_errors) === 0 &&
  187. count($this->_failures) === 0 &&
  188. count($this->_incompletes) === 0 &&
  189. count($this->_skips) === 0;
  190. }
  191.  
  192. /**
  193. * @return boolean
  194. */
  195. private function _isRedTestResult()
  196. {
  197. return count($this->_errors) > 0 ||
  198. count($this->_failures) > 0;
  199. }
  200.  
  201. /**
  202. * @return boolean
  203. */
  204. private function _isYellowTestResult()
  205. {
  206. return count($this->_errors) === 0 &&
  207. count($this->_failures) === 0 &&
  208. (count($this->_incompletes) > 0 ||
  209. count($this->_skips) > 0);
  210. }
  211. }
Add Comment
Please, Sign In to add comment