pear_test_case.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 1388 2006-11-10 20:59:59Z lastcraft $
  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. var $_loosely_typed;
  24. /**
  25. * Constructor. Sets the test name.
  26. * @param $label Test name to display.
  27. * @public
  28. */
  29. function PHPUnit_TestCase($label = false) {
  30. $this->SimpleTestCase($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. * In PHP5 the identity test tests for the same
  69. * object. This is a reference test in PHP4.
  70. * @param $first First object handle.
  71. * @param $second Hopefully the same handle.
  72. * @param $message Message to display.
  73. * @public
  74. */
  75. function assertSame(&$first, &$second, $message = "%s") {
  76. $dumper = &new SimpleDumper();
  77. $message = sprintf(
  78. $message,
  79. "[" . $dumper->describeValue($first) .
  80. "] and [" . $dumper->describeValue($second) .
  81. "] should reference the same object");
  82. return $this->assert(
  83. new TrueExpectation(),
  84. SimpleTestCompatibility::isReference($first, $second),
  85. $message);
  86. }
  87. /**
  88. * In PHP5 the identity test tests for the same
  89. * object. This is a reference test in PHP4.
  90. * @param $first First object handle.
  91. * @param $second Hopefully a different handle.
  92. * @param $message Message to display.
  93. * @public
  94. */
  95. function assertNotSame(&$first, &$second, $message = "%s") {
  96. $dumper = &new SimpleDumper();
  97. $message = sprintf(
  98. $message,
  99. "[" . $dumper->describeValue($first) .
  100. "] and [" . $dumper->describeValue($second) .
  101. "] should not be the same object");
  102. return $this->assert(
  103. new falseExpectation(),
  104. SimpleTestCompatibility::isReference($first, $second),
  105. $message);
  106. }
  107. /**
  108. * Sends pass if the test condition resolves true,
  109. * a fail otherwise.
  110. * @param $condition Condition to test true.
  111. * @param $message Message to display.
  112. * @public
  113. */
  114. function assertTrue($condition, $message = "%s") {
  115. parent::assert(new TrueExpectation(), $condition, $message);
  116. }
  117. /**
  118. * Sends pass if the test condition resolves false,
  119. * a fail otherwise.
  120. * @param $condition Condition to test false.
  121. * @param $message Message to display.
  122. * @public
  123. */
  124. function assertFalse($condition, $message = "%s") {
  125. parent::assert(new FalseExpectation(), $condition, $message);
  126. }
  127. /**
  128. * Tests a regex match. Needs refactoring.
  129. * @param $pattern Regex to match.
  130. * @param $subject String to search in.
  131. * @param $message Message to display.
  132. * @public
  133. */
  134. function assertRegExp($pattern, $subject, $message = "%s") {
  135. $this->assert(new PatternExpectation($pattern), $subject, $message);
  136. }
  137. /**
  138. * Tests the type of a value.
  139. * @param $value Value to take type of.
  140. * @param $type Hoped for type.
  141. * @param $message Message to display.
  142. * @public
  143. */
  144. function assertType($value, $type, $message = "%s") {
  145. parent::assert(new TrueExpectation(), gettype($value) == strtolower($type), $message);
  146. }
  147. /**
  148. * Sets equality operation to act as a simple equal
  149. * comparison only, allowing a broader range of
  150. * matches.
  151. * @param $loosely_typed True for broader comparison.
  152. * @public
  153. */
  154. function setLooselyTyped($loosely_typed) {
  155. $this->_loosely_typed = $loosely_typed;
  156. }
  157. /**
  158. * For progress indication during
  159. * a test amongst other things.
  160. * @return Usually one.
  161. * @public
  162. */
  163. function countTestCases() {
  164. return $this->getSize();
  165. }
  166. /**
  167. * Accessor for name, normally just the class
  168. * name.
  169. * @public
  170. */
  171. function getName() {
  172. return $this->getLabel();
  173. }
  174. /**
  175. * Does nothing. For compatibility only.
  176. * @param $name Dummy
  177. * @public
  178. */
  179. function setName($name) {
  180. }
  181. }
  182. ?>