PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
5.3 KiB

  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: exceptions.php,v 1.14 2006/12/20 11:17:13 pp11 Exp $
  7. */
  8. /**#@+
  9. * Include required SimpleTest files
  10. */
  11. require_once dirname(__FILE__) . '/invoker.php';
  12. require_once dirname(__FILE__) . '/expectation.php';
  13. /**#@-*/
  14. /**
  15. * Extension that traps exceptions and turns them into
  16. * an error message. PHP5 only.
  17. * @package SimpleTest
  18. * @subpackage UnitTester
  19. */
  20. class SimpleExceptionTrappingInvoker extends SimpleInvokerDecorator {
  21. /**
  22. * Stores the invoker to be wrapped.
  23. * @param SimpleInvoker $invoker Test method runner.
  24. */
  25. function SimpleExceptionTrappingInvoker($invoker) {
  26. $this->SimpleInvokerDecorator($invoker);
  27. }
  28. /**
  29. * Invokes a test method whilst trapping expected
  30. * exceptions. Any left over unthrown exceptions
  31. * are then reported as failures.
  32. * @param string $method Test method to call.
  33. */
  34. function invoke($method) {
  35. $trap = SimpleTest::getContext()->get('SimpleExceptionTrap');
  36. $trap->clear();
  37. try {
  38. parent::invoke($method);
  39. } catch (Exception $exception) {
  40. if (! $trap->isExpected($this->getTestCase(), $exception)) {
  41. $this->getTestCase()->exception($exception);
  42. }
  43. $trap->clear();
  44. $this->_invoker->getTestCase()->tearDown();
  45. }
  46. if ($message = $trap->getOutstanding()) {
  47. $this->getTestCase()->fail($message);
  48. }
  49. }
  50. }
  51. /**
  52. * Tests exceptions either by type or the exact
  53. * exception. This could be improved to accept
  54. * a pattern expectation to test the error
  55. * message, but that will have to come later.
  56. * @package SimpleTest
  57. * @subpackage UnitTester
  58. */
  59. class ExceptionExpectation extends SimpleExpectation {
  60. private $expected;
  61. /**
  62. * Sets up the conditions to test against.
  63. * If the expected value is a string, then
  64. * it will act as a test of the class name.
  65. * An exception as the comparison will
  66. * trigger an identical match. Writing this
  67. * down now makes it look doubly dumb. I hope
  68. * come up with a better scheme later.
  69. * @param mixed $expected A class name or an actual
  70. * exception to compare with.
  71. * @param string $message Message to display.
  72. */
  73. function __construct($expected, $message = '%s') {
  74. $this->expected = $expected;
  75. parent::__construct($message);
  76. }
  77. /**
  78. * Carry out the test.
  79. * @param Exception $compare Value to check.
  80. * @return boolean True if matched.
  81. */
  82. function test($compare) {
  83. if (is_string($this->expected)) {
  84. return ($compare instanceof $this->expected);
  85. }
  86. if (get_class($compare) != get_class($this->expected)) {
  87. return false;
  88. }
  89. return $compare->getMessage() == $this->expected->getMessage();
  90. }
  91. /**
  92. * Create the message to display describing the test.
  93. * @param Exception $compare Exception to match.
  94. * @return string Final message.
  95. */
  96. function testMessage($compare) {
  97. if (is_string($this->expected)) {
  98. return "Exception [" . $this->describeException($compare) .
  99. "] should be type [" . $this->expected . "]";
  100. }
  101. return "Exception [" . $this->describeException($compare) .
  102. "] should match [" .
  103. $this->describeException($this->expected) . "]";
  104. }
  105. /**
  106. * Summary of an Exception object.
  107. * @param Exception $compare Exception to describe.
  108. * @return string Text description.
  109. */
  110. protected function describeException($exception) {
  111. return get_class($exception) . ": " . $exception->getMessage();
  112. }
  113. }
  114. /**
  115. * Stores expected exceptions for when they
  116. * get thrown. Saves the irritating try...catch
  117. * block.
  118. * @package SimpleTest
  119. * @subpackage UnitTester
  120. */
  121. class SimpleExceptionTrap {
  122. private $expected;
  123. private $message;
  124. /**
  125. * Clears down the queue ready for action.
  126. */
  127. function __construct() {
  128. $this->clear();
  129. }
  130. /**
  131. * Sets up an expectation of an exception.
  132. * This has the effect of intercepting an
  133. * exception that matches.
  134. * @param SimpleExpectation $expected Expected exception to match.
  135. * @param string $message Message to display.
  136. * @access public
  137. */
  138. function expectException($expected = false, $message = '%s') {
  139. if ($expected === false) {
  140. $expected = new AnythingExpectation();
  141. }
  142. if (! SimpleExpectation::isExpectation($expected)) {
  143. $expected = new ExceptionExpectation($expected);
  144. }
  145. $this->expected = $expected;
  146. $this->message = $message;
  147. }
  148. /**
  149. * Compares the expected exception with any
  150. * in the queue. Issues a pass or fail and
  151. * returns the state of the test.
  152. * @param SimpleTestCase $test Test case to send messages to.
  153. * @param Exception $exception Exception to compare.
  154. * @return boolean False on no match.
  155. */
  156. function isExpected($test, $exception) {
  157. if ($this->expected) {
  158. return $test->assert($this->expected, $exception, $this->message);
  159. }
  160. return false;
  161. }
  162. /**
  163. * Tests for any left over exception.
  164. * @return string/false The failure message or false if none.
  165. */
  166. function getOutstanding() {
  167. return sprintf($this->message, 'Failed to trap exception');
  168. }
  169. /**
  170. * Discards the contents of the error queue.
  171. */
  172. function clear() {
  173. $this->expected = false;
  174. $this->message = false;
  175. }
  176. }
  177. ?>