pear_test_case.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * adapter for SimpleTest to use PEAR PHPUnit test cases
  4. * @package SimpleTest
  5. * @subpackage Extensions
  6. * @version $Id: pear_test_case.php 1836 2008-12-21 00:02:26Z edwardzyang $
  7. */
  8. /**#@+
  9. * include SimpleTest files
  10. */
  11. require_once(dirname(__FILE__) . '/../dumper.php');
  12. require_once(dirname(__FILE__) . '/../compatibility.php');
  13. require_once(dirname(__FILE__) . '/../test_case.php');
  14. require_once(dirname(__FILE__) . '/../expectation.php');
  15. /**#@-*/
  16. /**
  17. * Adapter for PEAR PHPUnit test case to allow
  18. * legacy PEAR test cases to be used with SimpleTest.
  19. * @package SimpleTest
  20. * @subpackage Extensions
  21. */
  22. class PHPUnit_TestCase extends SimpleTestCase {
  23. private $_loosely_typed;
  24. /**
  25. * Constructor. Sets the test name.
  26. * @param $label Test name to display.
  27. * @public
  28. */
  29. function __construct($label = false) {
  30. parent::__construct($label);
  31. $this->_loosely_typed = false;
  32. }
  33. /**
  34. * Will test straight equality if set to loose
  35. * typing, or identity if not.
  36. * @param $first First value.
  37. * @param $second Comparison value.
  38. * @param $message Message to display.
  39. * @public
  40. */
  41. function assertEquals($first, $second, $message = "%s", $delta = 0) {
  42. if ($this->_loosely_typed) {
  43. $expectation = new EqualExpectation($first);
  44. } else {
  45. $expectation = new IdenticalExpectation($first);
  46. }
  47. $this->assert($expectation, $second, $message);
  48. }
  49. /**
  50. * Passes if the value tested is not null.
  51. * @param $value Value to test against.
  52. * @param $message Message to display.
  53. * @public
  54. */
  55. function assertNotNull($value, $message = "%s") {
  56. parent::assert(new TrueExpectation(), isset($value), $message);
  57. }
  58. /**
  59. * Passes if the value tested is null.
  60. * @param $value Value to test against.
  61. * @param $message Message to display.
  62. * @public
  63. */
  64. function assertNull($value, $message = "%s") {
  65. parent::assert(new TrueExpectation(), !isset($value), $message);
  66. }
  67. /**
  68. * Identity test tests for the same object.
  69. * @param $first First object handle.
  70. * @param $second Hopefully the same handle.
  71. * @param $message Message to display.
  72. * @public
  73. */
  74. function assertSame($first, $second, $message = "%s") {
  75. $dumper = new SimpleDumper();
  76. $message = sprintf(
  77. $message,
  78. "[" . $dumper->describeValue($first) .
  79. "] and [" . $dumper->describeValue($second) .
  80. "] should reference the same object");
  81. return $this->assert(
  82. new TrueExpectation(),
  83. SimpleTestCompatibility::isReference($first, $second),
  84. $message);
  85. }
  86. /**
  87. * Inverted identity test.
  88. * @param $first First object handle.
  89. * @param $second Hopefully a different handle.
  90. * @param $message Message to display.
  91. * @public
  92. */
  93. function assertNotSame($first, $second, $message = "%s") {
  94. $dumper = new SimpleDumper();
  95. $message = sprintf(
  96. $message,
  97. "[" . $dumper->describeValue($first) .
  98. "] and [" . $dumper->describeValue($second) .
  99. "] should not be the same object");
  100. return $this->assert(
  101. new falseExpectation(),
  102. SimpleTestCompatibility::isReference($first, $second),
  103. $message);
  104. }
  105. /**
  106. * Sends pass if the test condition resolves true,
  107. * a fail otherwise.
  108. * @param $condition Condition to test true.
  109. * @param $message Message to display.
  110. * @public
  111. */
  112. function assertTrue($condition, $message = "%s") {
  113. parent::assert(new TrueExpectation(), $condition, $message);
  114. }
  115. /**
  116. * Sends pass if the test condition resolves false,
  117. * a fail otherwise.
  118. * @param $condition Condition to test false.
  119. * @param $message Message to display.
  120. * @public
  121. */
  122. function assertFalse($condition, $message = "%s") {
  123. parent::assert(new FalseExpectation(), $condition, $message);
  124. }
  125. /**
  126. * Tests a regex match. Needs refactoring.
  127. * @param $pattern Regex to match.
  128. * @param $subject String to search in.
  129. * @param $message Message to display.
  130. * @public
  131. */
  132. function assertRegExp($pattern, $subject, $message = "%s") {
  133. $this->assert(new PatternExpectation($pattern), $subject, $message);
  134. }
  135. /**
  136. * Tests the type of a value.
  137. * @param $value Value to take type of.
  138. * @param $type Hoped for type.
  139. * @param $message Message to display.
  140. * @public
  141. */
  142. function assertType($value, $type, $message = "%s") {
  143. parent::assert(new TrueExpectation(), gettype($value) == strtolower($type), $message);
  144. }
  145. /**
  146. * Sets equality operation to act as a simple equal
  147. * comparison only, allowing a broader range of
  148. * matches.
  149. * @param $loosely_typed True for broader comparison.
  150. * @public
  151. */
  152. function setLooselyTyped($loosely_typed) {
  153. $this->_loosely_typed = $loosely_typed;
  154. }
  155. /**
  156. * For progress indication during
  157. * a test amongst other things.
  158. * @return Usually one.
  159. * @public
  160. */
  161. function countTestCases() {
  162. return $this->getSize();
  163. }
  164. /**
  165. * Accessor for name, normally just the class
  166. * name.
  167. * @public
  168. */
  169. function getName() {
  170. return $this->getLabel();
  171. }
  172. /**
  173. * Does nothing. For compatibility only.
  174. * @param $name Dummy
  175. * @public
  176. */
  177. function setName($name) {
  178. }
  179. }
  180. ?>