unit_tester.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: unit_tester.php 1723 2008-04-08 00:34:10Z lastcraft $
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/test_case.php');
  12. require_once(dirname(__FILE__) . '/dumper.php');
  13. /**#@-*/
  14. /**
  15. * Standard unit test class for day to day testing
  16. * of PHP code XP style. Adds some useful standard
  17. * assertions.
  18. * @package SimpleTest
  19. * @subpackage UnitTester
  20. */
  21. class UnitTestCase extends SimpleTestCase {
  22. /**
  23. * Creates an empty test case. Should be subclassed
  24. * with test methods for a functional test case.
  25. * @param string $label Name of test case. Will use
  26. * the class name if none specified.
  27. * @access public
  28. */
  29. function UnitTestCase($label = false) {
  30. if (! $label) {
  31. $label = get_class($this);
  32. }
  33. $this->SimpleTestCase($label);
  34. }
  35. /**
  36. * Called from within the test methods to register
  37. * passes and failures.
  38. * @param boolean $result Pass on true.
  39. * @param string $message Message to display describing
  40. * the test state.
  41. * @return boolean True on pass
  42. * @access public
  43. */
  44. function assertTrue($result, $message = false) {
  45. return $this->assert(new TrueExpectation(), $result, $message);
  46. }
  47. /**
  48. * Will be true on false and vice versa. False
  49. * is the PHP definition of false, so that null,
  50. * empty strings, zero and an empty array all count
  51. * as false.
  52. * @param boolean $result Pass on false.
  53. * @param string $message Message to display.
  54. * @return boolean True on pass
  55. * @access public
  56. */
  57. function assertFalse($result, $message = '%s') {
  58. return $this->assert(new FalseExpectation(), $result, $message);
  59. }
  60. /**
  61. * Will be true if the value is null.
  62. * @param null $value Supposedly null value.
  63. * @param string $message Message to display.
  64. * @return boolean True on pass
  65. * @access public
  66. */
  67. function assertNull($value, $message = '%s') {
  68. $dumper = &new SimpleDumper();
  69. $message = sprintf(
  70. $message,
  71. '[' . $dumper->describeValue($value) . '] should be null');
  72. return $this->assertTrue(! isset($value), $message);
  73. }
  74. /**
  75. * Will be true if the value is set.
  76. * @param mixed $value Supposedly set value.
  77. * @param string $message Message to display.
  78. * @return boolean True on pass.
  79. * @access public
  80. */
  81. function assertNotNull($value, $message = '%s') {
  82. $dumper = &new SimpleDumper();
  83. $message = sprintf(
  84. $message,
  85. '[' . $dumper->describeValue($value) . '] should not be null');
  86. return $this->assertTrue(isset($value), $message);
  87. }
  88. /**
  89. * Type and class test. Will pass if class
  90. * matches the type name or is a subclass or
  91. * if not an object, but the type is correct.
  92. * @param mixed $object Object to test.
  93. * @param string $type Type name as string.
  94. * @param string $message Message to display.
  95. * @return boolean True on pass.
  96. * @access public
  97. */
  98. function assertIsA($object, $type, $message = '%s') {
  99. return $this->assert(
  100. new IsAExpectation($type),
  101. $object,
  102. $message);
  103. }
  104. /**
  105. * Type and class mismatch test. Will pass if class
  106. * name or underling type does not match the one
  107. * specified.
  108. * @param mixed $object Object to test.
  109. * @param string $type Type name as string.
  110. * @param string $message Message to display.
  111. * @return boolean True on pass.
  112. * @access public
  113. */
  114. function assertNotA($object, $type, $message = '%s') {
  115. return $this->assert(
  116. new NotAExpectation($type),
  117. $object,
  118. $message);
  119. }
  120. /**
  121. * Will trigger a pass if the two parameters have
  122. * the same value only. Otherwise a fail.
  123. * @param mixed $first Value to compare.
  124. * @param mixed $second Value to compare.
  125. * @param string $message Message to display.
  126. * @return boolean True on pass
  127. * @access public
  128. */
  129. function assertEqual($first, $second, $message = '%s') {
  130. return $this->assert(
  131. new EqualExpectation($first),
  132. $second,
  133. $message);
  134. }
  135. /**
  136. * Will trigger a pass if the two parameters have
  137. * a different value. Otherwise a fail.
  138. * @param mixed $first Value to compare.
  139. * @param mixed $second Value to compare.
  140. * @param string $message Message to display.
  141. * @return boolean True on pass
  142. * @access public
  143. */
  144. function assertNotEqual($first, $second, $message = '%s') {
  145. return $this->assert(
  146. new NotEqualExpectation($first),
  147. $second,
  148. $message);
  149. }
  150. /**
  151. * Will trigger a pass if the if the first parameter
  152. * is near enough to the second by the margin.
  153. * @param mixed $first Value to compare.
  154. * @param mixed $second Value to compare.
  155. * @param mixed $margin Fuzziness of match.
  156. * @param string $message Message to display.
  157. * @return boolean True on pass
  158. * @access public
  159. */
  160. function assertWithinMargin($first, $second, $margin, $message = '%s') {
  161. return $this->assert(
  162. new WithinMarginExpectation($first, $margin),
  163. $second,
  164. $message);
  165. }
  166. /**
  167. * Will trigger a pass if the two parameters differ
  168. * by more than the margin.
  169. * @param mixed $first Value to compare.
  170. * @param mixed $second Value to compare.
  171. * @param mixed $margin Fuzziness of match.
  172. * @param string $message Message to display.
  173. * @return boolean True on pass
  174. * @access public
  175. */
  176. function assertOutsideMargin($first, $second, $margin, $message = '%s') {
  177. return $this->assert(
  178. new OutsideMarginExpectation($first, $margin),
  179. $second,
  180. $message);
  181. }
  182. /**
  183. * Will trigger a pass if the two parameters have
  184. * the same value and same type. Otherwise a fail.
  185. * @param mixed $first Value to compare.
  186. * @param mixed $second Value to compare.
  187. * @param string $message Message to display.
  188. * @return boolean True on pass
  189. * @access public
  190. */
  191. function assertIdentical($first, $second, $message = '%s') {
  192. return $this->assert(
  193. new IdenticalExpectation($first),
  194. $second,
  195. $message);
  196. }
  197. /**
  198. * Will trigger a pass if the two parameters have
  199. * the different value or different type.
  200. * @param mixed $first Value to compare.
  201. * @param mixed $second Value to compare.
  202. * @param string $message Message to display.
  203. * @return boolean True on pass
  204. * @access public
  205. */
  206. function assertNotIdentical($first, $second, $message = '%s') {
  207. return $this->assert(
  208. new NotIdenticalExpectation($first),
  209. $second,
  210. $message);
  211. }
  212. /**
  213. * Will trigger a pass if both parameters refer
  214. * to the same object. Fail otherwise.
  215. * @param mixed $first Object reference to check.
  216. * @param mixed $second Hopefully the same object.
  217. * @param string $message Message to display.
  218. * @return boolean True on pass
  219. * @access public
  220. */
  221. function assertReference(&$first, &$second, $message = '%s') {
  222. $dumper = &new SimpleDumper();
  223. $message = sprintf(
  224. $message,
  225. '[' . $dumper->describeValue($first) .
  226. '] and [' . $dumper->describeValue($second) .
  227. '] should reference the same object');
  228. return $this->assertTrue(
  229. SimpleTestCompatibility::isReference($first, $second),
  230. $message);
  231. }
  232. /**
  233. * Will trigger a pass if both parameters refer
  234. * to different objects. Fail otherwise. The objects
  235. * have to be identical though.
  236. * @param mixed $first Object reference to check.
  237. * @param mixed $second Hopefully not the same object.
  238. * @param string $message Message to display.
  239. * @return boolean True on pass
  240. * @access public
  241. */
  242. function assertClone(&$first, &$second, $message = '%s') {
  243. $dumper = &new SimpleDumper();
  244. $message = sprintf(
  245. $message,
  246. '[' . $dumper->describeValue($first) .
  247. '] and [' . $dumper->describeValue($second) .
  248. '] should not be the same object');
  249. $identical = &new IdenticalExpectation($first);
  250. return $this->assertTrue(
  251. $identical->test($second) &&
  252. ! SimpleTestCompatibility::isReference($first, $second),
  253. $message);
  254. }
  255. /**
  256. * @deprecated
  257. */
  258. function assertCopy(&$first, &$second, $message = "%s") {
  259. $dumper = &new SimpleDumper();
  260. $message = sprintf(
  261. $message,
  262. "[" . $dumper->describeValue($first) .
  263. "] and [" . $dumper->describeValue($second) .
  264. "] should not be the same object");
  265. return $this->assertFalse(
  266. SimpleTestCompatibility::isReference($first, $second),
  267. $message);
  268. }
  269. /**
  270. * Will trigger a pass if the Perl regex pattern
  271. * is found in the subject. Fail otherwise.
  272. * @param string $pattern Perl regex to look for including
  273. * the regex delimiters.
  274. * @param string $subject String to search in.
  275. * @param string $message Message to display.
  276. * @return boolean True on pass
  277. * @access public
  278. */
  279. function assertPattern($pattern, $subject, $message = '%s') {
  280. return $this->assert(
  281. new PatternExpectation($pattern),
  282. $subject,
  283. $message);
  284. }
  285. /**
  286. * @deprecated
  287. */
  288. function assertWantedPattern($pattern, $subject, $message = '%s') {
  289. return $this->assertPattern($pattern, $subject, $message);
  290. }
  291. /**
  292. * Will trigger a pass if the perl regex pattern
  293. * is not present in subject. Fail if found.
  294. * @param string $pattern Perl regex to look for including
  295. * the regex delimiters.
  296. * @param string $subject String to search in.
  297. * @param string $message Message to display.
  298. * @return boolean True on pass
  299. * @access public
  300. */
  301. function assertNoPattern($pattern, $subject, $message = '%s') {
  302. return $this->assert(
  303. new NoPatternExpectation($pattern),
  304. $subject,
  305. $message);
  306. }
  307. /**
  308. * @deprecated
  309. */
  310. function assertNoUnwantedPattern($pattern, $subject, $message = '%s') {
  311. return $this->assertNoPattern($pattern, $subject, $message);
  312. }
  313. /**
  314. * @deprecated
  315. */
  316. function swallowErrors() {
  317. $context = &SimpleTest::getContext();
  318. $queue = &$context->get('SimpleErrorQueue');
  319. $queue->clear();
  320. }
  321. /**
  322. * @deprecated
  323. */
  324. function assertNoErrors($message = '%s') {
  325. $context = &SimpleTest::getContext();
  326. $queue = &$context->get('SimpleErrorQueue');
  327. return $queue->assertNoErrors($message);
  328. }
  329. /**
  330. * @deprecated
  331. */
  332. function assertError($expected = false, $message = '%s') {
  333. $context = &SimpleTest::getContext();
  334. $queue = &$context->get('SimpleErrorQueue');
  335. return $queue->assertError($this->_coerceExpectation($expected), $message);
  336. }
  337. /**
  338. * Prepares for an error. If the error mismatches it
  339. * passes through, otherwise it is swallowed. Any
  340. * left over errors trigger failures.
  341. * @param SimpleExpectation/string $expected The error to match.
  342. * @param string $message Message on failure.
  343. * @access public
  344. */
  345. function expectError($expected = false, $message = '%s') {
  346. $context = &SimpleTest::getContext();
  347. $queue = &$context->get('SimpleErrorQueue');
  348. $queue->expectError($this->_coerceExpectation($expected), $message);
  349. }
  350. /**
  351. * Prepares for an exception. If the error mismatches it
  352. * passes through, otherwise it is swallowed. Any
  353. * left over errors trigger failures.
  354. * @param SimpleExpectation/Exception $expected The error to match.
  355. * @param string $message Message on failure.
  356. * @access public
  357. */
  358. function expectException($expected = false, $message = '%s') {
  359. $context = &SimpleTest::getContext();
  360. $queue = &$context->get('SimpleExceptionTrap');
  361. // :HACK: Directly substituting in seems to cause a segfault with
  362. // Zend Optimizer on some systems
  363. $line = $this->getAssertionLine();
  364. $queue->expectException($expected, $message . $line);
  365. }
  366. /**
  367. * Creates an equality expectation if the
  368. * object/value is not already some type
  369. * of expectation.
  370. * @param mixed $expected Expected value.
  371. * @return SimpleExpectation Expectation object.
  372. * @access private
  373. */
  374. function _coerceExpectation($expected) {
  375. if (!$expected) {
  376. return new TrueExpectation();
  377. }
  378. if (SimpleTestCompatibility::isA($expected, 'SimpleExpectation')) {
  379. return $expected;
  380. }
  381. return new EqualExpectation(
  382. is_string($expected) ? str_replace('%', '%%', $expected) : $expected);
  383. }
  384. /**
  385. * @deprecated
  386. */
  387. function assertErrorPattern($pattern, $message = '%s') {
  388. return $this->assertError(new PatternExpectation($pattern), $message);
  389. }
  390. }
  391. ?>