PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/iic_db/INC/Vendor/PHPUnit/Framework/TestFailure.php

http://iiccms.googlecode.com/
PHP | 251 lines | 114 code | 28 blank | 109 comment | 24 complexity | 74cf27bb1cd617e6640c516c06668269 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2008, Sebastian Bergmann <sb@sebastian-bergmann.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. * @category Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: TestFailure.php 3164 2008-06-08 12:22:29Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 2.0.0
  45. */
  46. require_once 'PHPUnit/Framework.php';
  47. require_once 'PHPUnit/Util/Filter.php';
  48. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  49. if (!class_exists('PHPUnit_Framework_TestFailure', FALSE)) {
  50. /**
  51. * A TestFailure collects a failed test together with the caught exception.
  52. *
  53. * @category Testing
  54. * @package PHPUnit
  55. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  56. * @copyright 2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version Release: 3.3.9
  59. * @link http://www.phpunit.de/
  60. * @since Class available since Release 2.0.0
  61. */
  62. class PHPUnit_Framework_TestFailure
  63. {
  64. /**
  65. * @var PHPUnit_Framework_Test
  66. */
  67. protected $failedTest;
  68. /**
  69. * @var Exception
  70. */
  71. protected $thrownException;
  72. /**
  73. * Constructs a TestFailure with the given test and exception.
  74. *
  75. * @param PHPUnit_Framework_Test $failedTest
  76. * @param Exception $thrownException
  77. */
  78. public function __construct(PHPUnit_Framework_Test $failedTest, Exception $thrownException)
  79. {
  80. $this->failedTest = $failedTest;
  81. $this->thrownException = $thrownException;
  82. }
  83. /**
  84. * Returns a short description of the failure.
  85. *
  86. * @return string
  87. */
  88. public function toString()
  89. {
  90. return sprintf(
  91. '%s: %s',
  92. $this->failedTest,
  93. $this->thrownException->getMessage()
  94. );
  95. }
  96. /**
  97. * Returns a verbose description of the failure.
  98. *
  99. * @param bool $verbose
  100. * @return string
  101. * @since Method available since Release 3.2.0
  102. */
  103. public function toStringVerbose($verbose = FALSE)
  104. {
  105. return self::exceptionToString($this->thrownException, $verbose);
  106. }
  107. /**
  108. * Returns a verbose description for an exception.
  109. *
  110. * @param Exception $e
  111. * @param bool $verbose
  112. * @return string
  113. * @since Method available since Release 3.2.0
  114. */
  115. public static function exceptionToString(Exception $e, $verbose = FALSE)
  116. {
  117. if ($e instanceof PHPUnit_Framework_SelfDescribing) {
  118. if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
  119. $comparisonFailure = $e->getComparisonFailure();
  120. $description = $e->getDescription();
  121. $message = $e->getCustomMessage();
  122. if ($message == '') {
  123. $buffer = '';
  124. } else {
  125. $buffer = $message . "\n";
  126. }
  127. if ($comparisonFailure !== NULL) {
  128. if ($comparisonFailure->identical()) {
  129. if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object) {
  130. $buffer .= "Failed asserting that two variables reference the same object.\n";
  131. } else {
  132. $buffer .= $comparisonFailure->toString() . "\n";
  133. }
  134. } else {
  135. if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Scalar) {
  136. $buffer .= sprintf(
  137. "Failed asserting that %s matches expected value %s.\n",
  138. PHPUnit_Util_Type::toString($comparisonFailure->getActual()),
  139. PHPUnit_Util_Type::toString($comparisonFailure->getExpected())
  140. );
  141. }
  142. else if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array ||
  143. $comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object ||
  144. $comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String) {
  145. $buffer .= sprintf(
  146. "Failed asserting that two %ss are equal.\n%s\n",
  147. strtolower(substr(get_class($comparisonFailure), 36)),
  148. $comparisonFailure->toString()
  149. );
  150. }
  151. if ($verbose &&
  152. !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array &&
  153. !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object &&
  154. !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String) {
  155. $buffer .= $comparisonFailure->toString() . "\n";
  156. }
  157. }
  158. } else {
  159. $buffer .= $e->toString();
  160. $equal = $buffer == $description;
  161. if (!empty($buffer)) {
  162. $buffer .= "\n";
  163. }
  164. if (!$equal) {
  165. $buffer .= $description . "\n";
  166. }
  167. }
  168. }
  169. else {
  170. $buffer = $e->toString();
  171. if (!empty($buffer)) {
  172. $buffer .= "\n";
  173. }
  174. }
  175. }
  176. else if ($e instanceof PHPUnit_Framework_Error) {
  177. $buffer = $e->getMessage() . "\n";
  178. }
  179. else {
  180. $buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
  181. }
  182. return $buffer;
  183. }
  184. /**
  185. * Gets the failed test.
  186. *
  187. * @return Test
  188. */
  189. public function failedTest()
  190. {
  191. return $this->failedTest;
  192. }
  193. /**
  194. * Gets the thrown exception.
  195. *
  196. * @return Exception
  197. */
  198. public function thrownException()
  199. {
  200. return $this->thrownException;
  201. }
  202. /**
  203. * Returns the exception's message.
  204. *
  205. * @return string
  206. */
  207. public function exceptionMessage()
  208. {
  209. return $this->thrownException()->getMessage();
  210. }
  211. /**
  212. * Returns TRUE if the thrown exception
  213. * is of type AssertionFailedError.
  214. *
  215. * @return boolean
  216. */
  217. public function isFailure()
  218. {
  219. return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);
  220. }
  221. }
  222. }
  223. ?>